Skip to content

Commit 0bb3e80

Browse files
committed
First implementation
1 parent c601056 commit 0bb3e80

18 files changed

+4772
-2
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/vendor/
2+
.php_cs
3+
.php_cs.cache

.php_cs.dist

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
$finder = PhpCsFixer\Finder::create()
4+
->in(__DIR__);
5+
6+
return \Ely\CS\Config::create([
7+
// Disable it for const, 'cause ^7.0 compatibility
8+
'visibility_required' => ['property', 'method'],
9+
])->setFinder($finder);

README.md

+263-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,263 @@
1-
# php-code-style
2-
Set of PHP-CS-Fixer rules used in the development of Ely.by PHP projects
1+
# Ely.by PHP-CS-Fixer rules
2+
3+
Set of PHP-CS-Fixer rules used in development of Ely.by PHP projects. It's suited for PHP 7.1 and above.
4+
You can use it as a ready-made set of rules or [just some of them](#using-our-fixers).
5+
6+
## Installation
7+
8+
First of all install Ely.by PHP-CS-Fixer rules via composer with
9+
[PHP-CS-Fixer](https://github.com/FriendsOfPHP/PHP-CS-Fixer):
10+
11+
```sh
12+
composer require --dev friendsofphp/php-cs-fixer ely/php-code-style
13+
```
14+
15+
Then create file `.php-cs` with following contents:
16+
17+
```php
18+
<?php
19+
$finder = \PhpCsFixer\Finder::create()
20+
->in(__DIR__);
21+
22+
return \Ely\CS\Config::create()
23+
->setFinder($finder);
24+
```
25+
26+
And that's it. You can now find code style violations with following command:
27+
28+
```sh
29+
vendor/bin/php-cs-fixer --diff --dry-run -v fix
30+
```
31+
32+
And then completely fix them all with:
33+
34+
```sh
35+
vendor/bin/php-cs-fixer fix
36+
```
37+
38+
### Configuration
39+
40+
You can pass a custom set of rules to the `\Ely\CS\Config::create()` call. For example, it can be used to validate a
41+
project with PHP 7.0 compatibility:
42+
43+
```php
44+
<?php
45+
return \Ely\CS\Config::create([
46+
'visibility_required' => ['property', 'method'],
47+
])->setFinder($finder);
48+
```
49+
50+
## Code style
51+
52+
Our code style is based primarily on [PSR-2](https://www.php-fig.org/psr/psr-2/), while borrowing some ideas from
53+
[PSR-12](https://github.com/php-fig/fig-standards/blob/92b198bb/proposed/extended-coding-style-guide.md)
54+
with some changes.
55+
56+
### Example
57+
58+
This example encompasses some of the rules below as a quick overview:
59+
60+
```php
61+
<?php
62+
declare(strict_types=1);
63+
64+
namespace Vendor\Package;
65+
66+
use Vendor\Package\SomeNamespace\ClassA;
67+
68+
class Foo extends Bar implements FooInterface {
69+
use SomeTrait;
70+
71+
private const SAMPLE_1 = 123;
72+
private const SAMPLE_2 = 321;
73+
74+
public $field1;
75+
76+
public $field2;
77+
78+
public function sampleFunction(int $a, int $b = null): array {
79+
if ($a === $b) {
80+
$result = bar();
81+
} else {
82+
$result = BazClass::bar($this->field1, $this->field2);
83+
}
84+
85+
return $result;
86+
}
87+
88+
public function setToNull(): self {
89+
$this->field1 = null;
90+
return $this;
91+
}
92+
93+
}
94+
```
95+
96+
**Key differences:**
97+
98+
* Opening braces for classes MUST be **on the same line**.
99+
100+
* Opening braces for methods MUST be **on the next line**.
101+
102+
**Additional rules:**
103+
104+
* There MUST be one empty line before `return` statement, except when there is only one statement before it.
105+
106+
```php
107+
<?php
108+
109+
function a() {
110+
$a = '123';
111+
return $a . ' is a number';
112+
}
113+
114+
function b() {
115+
$a = '123';
116+
$b = 'is';
117+
118+
return $a . ' ' . $b . ' a number';
119+
}
120+
```
121+
122+
* There MUST be one blank line around class body, but there MUST be **no blank lines** around anonymous class body.
123+
124+
```php
125+
<?php
126+
class Test {
127+
128+
public function method() {
129+
$obj = new class extends Foo {
130+
public function overriddenMethod() {
131+
// code body
132+
}
133+
};
134+
}
135+
136+
}
137+
```
138+
139+
* Visibility MUST be declared for all methods, properties and constants.
140+
141+
* There MUST be one blank line after an each of `if`, `switch`, `for`, `foreach`, `while` and `do-while` bodies.
142+
143+
```php
144+
<?php
145+
if (true) {
146+
// some actions here
147+
}
148+
149+
echo 'the next statement is here';
150+
```
151+
152+
## Using our fixers
153+
154+
First of all, you must install Ely.by PHP-CS-Fixer package as described in the [installation chapter](#installation).
155+
After that you can enable our custom fixers with `registerCustomFixers` method:
156+
157+
```php
158+
<?php
159+
// Your Finder configuration
160+
161+
return \PhpCsFixer\Config::create()
162+
->registerCustomFixers(new \Ely\CS\Fixers());
163+
```
164+
165+
And then you'll be able to use our custom rules.
166+
167+
### `Ely/blank_line_around_class_body`
168+
169+
Ensure that a class body contains one blank line after its definition and before its end:
170+
171+
```diff
172+
--- Original
173+
+++ New
174+
@@ @@
175+
<?php
176+
class Test {
177+
+
178+
public function func() {
179+
$obj = new class extends Foo {
180+
+
181+
public $prop;
182+
+
183+
}
184+
}
185+
+
186+
}
187+
```
188+
189+
**Configuration:**
190+
191+
* `apply_to_anonymous_classes` - should this fixer be applied to anonymous classes? If it is set to `false`, than
192+
anonymous classes will be fixed to don't have empty lines around body. **Default**: `false`.
193+
194+
* `blank_lines_count` - adjusts an amount of the blank lines. **Default**: `1`.
195+
196+
### `Ely/blank_line_before_return`
197+
198+
This is extended version of the original `blank_line_before_statement` fixer. It applies only to `return` statements
199+
and only in cases, when on the current nesting level more than one statements.
200+
201+
```diff
202+
--- Original
203+
+++ New
204+
@@ @@
205+
<?php
206+
public function foo() {
207+
$a = 'this';
208+
$b = 'is';
209+
+
210+
return "$a $b awesome";
211+
}
212+
213+
public function bar() {
214+
$this->foo();
215+
return 'okay';
216+
}
217+
```
218+
219+
### `Ely/line_break_after_statements`
220+
221+
Ensures that there is one blank line above the next statements: `if`, `switch`, `for`, `foreach`, `while`
222+
and `do-while`.
223+
224+
```diff
225+
--- Original
226+
+++ New
227+
@@ @@
228+
<?php
229+
$a = 123;
230+
if ($a === 123) {
231+
// Do something here
232+
}
233+
+
234+
$b = [1, 2, 3];
235+
foreach ($b as $number) {
236+
if ($number === 3) {
237+
echo 'it is three!';
238+
}
239+
}
240+
+
241+
$c = 'next statement';
242+
```
243+
244+
### `Ely/new_with_braces`
245+
246+
This is the extended version of the original `new_with_braces` fixer. It allows you to remove braces around
247+
an anonymous class declaration in a case when said class constructor doesn't contain any arguments.
248+
249+
```diff
250+
--- Original
251+
+++ New
252+
@@ @@
253+
<?php
254+
-$a = new Foo;
255+
+$a = new Foo();
256+
-$b = new class() extends Foo {};
257+
+$b = new class extends Foo {};
258+
```
259+
260+
**Configuration:**
261+
262+
* `remove_for_anonymous_classes` - if its set to `true`, then braces for anonymous classes without constructor
263+
arguments will be removed. **Default**: `false`.

composer.json

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "ely/php-code-style",
3+
"description": "Set of PHP-CS-Fixer rules used in the development of Ely.by PHP projects",
4+
"keywords": ["php-cs-fixer", "code style"],
5+
"homepage": "https://github.com/elyby/php-code-style",
6+
"authors": [
7+
{
8+
"name": "Ely.by team",
9+
"email": "[email protected]"
10+
},
11+
{
12+
"name": "ErickSkrauch",
13+
"email": "[email protected]"
14+
}
15+
],
16+
"license": "Apache-2.0",
17+
"type": "library",
18+
"require": {
19+
"php": "^7.0",
20+
"friendsofphp/php-cs-fixer": "^2.11"
21+
},
22+
"require-dev": {
23+
"phpunit/phpunit": "^7.0"
24+
},
25+
"autoload": {
26+
"psr-4": {
27+
"Ely\\CS\\": "src/"
28+
}
29+
},
30+
"autoload-dev": {
31+
"psr-4": {
32+
"Ely\\CS\\Test\\": "tests/"
33+
},
34+
"files": [
35+
"vendor/friendsofphp/php-cs-fixer/tests/Test/Constraint/SameStringsConstraint.php"
36+
]
37+
}
38+
}

0 commit comments

Comments
 (0)