Skip to content

Commit 764bc02

Browse files
mfnbarryvdh
andauthored
[CODESTYLE] Replace phpcs with php-cs-fixer (barryvdh#1030)
* composer require --dev friendsofphp/php-cs-fixer:^2 * php-cs-fixer: ignore cache and custom local config file * php-cs-fixer: initial config * php-cs-fixer: replace commands in composer * php-cs-fixer: add PSR12 "as good as it currently gets" * php-cs-fixer: apply PSR12 to codebase * tests: add workaround to keep unused imports for snapshot testing * php-cs-fixer: apply no_unused_imports * php-cs-fixer: apply array_syntax short * php-cs-fixer: apply single_quote * php-cs-fixer: switch ordered_imports sort_algorithm to alpha Let's be opinionated here for consistency * php-cs-fixer: split between non-tests and tests and share config * php-cs-fixer: apply declare_strict_types for tests * php-cs-fixer: apply fully_qualified_strict_types * php-cs-fixer: apply space_after_semicolon * php-cs-fixer: apply trailing_comma_in_multiline_array * php-cs-fixer: apply trim_array_spaces * php-cs-fixer: apply unary_operator_spaces * php-cs-fixer: apply whitespace_after_comma_in_array * php-cs-fixer: apply native_function_invocation * php-cs-fixer: apply concat_space * grumphp: reflect we're using phpcsfixer now * composer remove --dev squizlabs/php_codesniffer * gha: simplify fix-style approach Not really necessary to remove packages and then manually prevent unrelated commits creeping in Co-authored-by: Barry vd. Heuvel <[email protected]>
1 parent a96add6 commit 764bc02

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+308
-215
lines changed

.github/workflows/fix-code-style.yml

+1-6
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,11 @@ jobs:
2323
tools: composer:v2
2424

2525
- name: Install dependencies
26-
run: |
27-
composer remove phpro/grumphp vimeo/psalm --no-update --dev
28-
composer update --prefer-dist --no-progress
26+
run: composer update --prefer-dist --no-progress
2927

3028
- run: composer fix-style
3129
continue-on-error: true
3230

33-
# Revert manual modifications so they don't get commited 💥
34-
- run: git checkout -- composer.json
35-
3631
- uses: stefanzweifel/git-auto-commit-action@v4
3732
with:
3833
commit_message: composer fix-style

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
.phpunit.result.cache
22

33
/.idea
4+
/.php_cs
5+
/.php_cs.cache
6+
/.php_cs.tests.cache
47
/composer.lock
58
/vendor

.php_cs.common.php

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
// Share common rules between non-test and test files
4+
return [
5+
// PSR12 from https://github.com/FriendsOfPHP/PHP-CS-Fixer/pull/4943
6+
'@PSR2' => true,
7+
'blank_line_after_opening_tag' => true,
8+
'braces' => [
9+
// Not-yet-implemented
10+
// 'allow_single_line_anonymous_class_with_empty_body' => true,
11+
],
12+
'compact_nullable_typehint' => true,
13+
'declare_equal_normalize' => true,
14+
'lowercase_cast' => true,
15+
'lowercase_static_reference' => true,
16+
'new_with_braces' => true,
17+
'no_blank_lines_after_class_opening' => true,
18+
'no_leading_import_slash' => true,
19+
'no_whitespace_in_blank_line' => true,
20+
'ordered_class_elements' => [
21+
'order' => [
22+
'use_trait',
23+
],
24+
],
25+
'ordered_imports' => [
26+
'imports_order' => [
27+
'class',
28+
'function',
29+
'const',
30+
],
31+
'sort_algorithm' => 'alpha',
32+
],
33+
'return_type_declaration' => true,
34+
'short_scalar_cast' => true,
35+
'single_blank_line_before_namespace' => true,
36+
'single_trait_insert_per_statement' => true,
37+
'ternary_operator_spaces' => true,
38+
'visibility_required' => [
39+
'elements' => [
40+
'const',
41+
'method',
42+
'property',
43+
],
44+
],
45+
46+
// Further quality-of-life improvements
47+
'array_syntax' => [
48+
'syntax' => 'short',
49+
],
50+
'concat_space' => [
51+
'spacing' => 'one',
52+
],
53+
'fully_qualified_strict_types' => true,
54+
'native_function_invocation' => [
55+
'include' => [],
56+
'strict' => true,
57+
],
58+
'no_unused_imports' => true,
59+
'single_quote' => true,
60+
'space_after_semicolon' => true,
61+
'trailing_comma_in_multiline_array' => true,
62+
'trim_array_spaces' => true,
63+
'unary_operator_spaces' => true,
64+
'whitespace_after_comma_in_array' => true,
65+
];

.php_cs.dist

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
require __DIR__ . '/vendor/autoload.php';
3+
4+
$finder = PhpCsFixer\Finder::create()
5+
->in(__DIR__)
6+
->exclude('tests');
7+
8+
$config = require __DIR__ . '/.php_cs.common.php';
9+
10+
return PhpCsFixer\Config::create()
11+
->setFinder($finder)
12+
->setRules($config)
13+
->setRiskyAllowed(true)
14+
->setCacheFile(__DIR__ . '/.php_cs.cache');

.php_cs.tests.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
require __DIR__ . '/vendor/autoload.php';
3+
4+
$finder = PhpCsFixer\Finder::create()
5+
->in(__DIR__ . '/tests')
6+
->exclude('__snapshots__');
7+
8+
$config = require __DIR__ . '/.php_cs.common.php';
9+
10+
// Additional rules for tests
11+
$config = array_merge(
12+
$config,
13+
[
14+
'declare_strict_types' => true,
15+
]
16+
);
17+
18+
return PhpCsFixer\Config::create()
19+
->setFinder($finder)
20+
->setRules($config)
21+
->setRiskyAllowed(true)
22+
->setCacheFile(__DIR__ . '/.php_cs.tests.cache');

composer.json

+9-3
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@
3131
"phpdocumentor/type-resolver": "^1.1.0"
3232
},
3333
"require-dev": {
34+
"friendsofphp/php-cs-fixer": "^2",
3435
"illuminate/config": "^6 || ^7 || ^8",
3536
"illuminate/view": "^6 || ^7 || ^8",
3637
"mockery/mockery": "^1.3",
3738
"orchestra/testbench": "^4 || ^5 || ^6",
3839
"phpro/grumphp": "^0.19.0",
3940
"spatie/phpunit-snapshot-assertions": "^1.4 || ^2.2 || ^3",
40-
"squizlabs/php_codesniffer": "^3.5",
4141
"vimeo/psalm": "^3.12"
4242
},
4343
"config": {
@@ -67,8 +67,14 @@
6767
"prefer-stable": true,
6868
"scripts": {
6969
"analyze": "psalm",
70-
"check-style": "phpcs -p --standard=PSR12 config/ resources/ src/ tests/ '--ignore=*__snapshots_*'",
71-
"fix-style": "phpcbf -p --standard=PSR12 config/ resources/ src/ tests/ '--ignore=*__snapshots_*'",
70+
"check-style": [
71+
"php-cs-fixer fix --diff --diff-format=udiff --dry-run",
72+
"php-cs-fixer fix --diff --diff-format=udiff --dry-run --config=.php_cs.tests.php"
73+
],
74+
"fix-style": [
75+
"php-cs-fixer fix",
76+
"php-cs-fixer fix --config=.php_cs.tests.php"
77+
],
7278
"test": "phpunit",
7379
"test-regenerate": "phpunit -d --update-snapshots"
7480
}

config/ide-helper.php

+19-20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
return array(
3+
return [
44

55
/*
66
|--------------------------------------------------------------------------
@@ -98,9 +98,9 @@
9898

9999
'include_helpers' => false,
100100

101-
'helper_files' => array(
101+
'helper_files' => [
102102
base_path() . '/vendor/laravel/framework/src/Illuminate/Support/helpers.php',
103-
),
103+
],
104104

105105
/*
106106
|--------------------------------------------------------------------------
@@ -115,9 +115,9 @@
115115
|
116116
*/
117117

118-
'model_locations' => array(
118+
'model_locations' => [
119119
'app',
120-
),
120+
],
121121

122122
/*
123123
|--------------------------------------------------------------------------
@@ -128,9 +128,9 @@
128128
|
129129
*/
130130

131-
'ignored_models' => array(
131+
'ignored_models' => [
132132

133-
),
133+
],
134134

135135
/*
136136
|--------------------------------------------------------------------------
@@ -141,12 +141,12 @@
141141
|
142142
*/
143143

144-
'extra' => array(
145-
'Eloquent' => array('Illuminate\Database\Eloquent\Builder', 'Illuminate\Database\Query\Builder'),
146-
'Session' => array('Illuminate\Session\Store'),
147-
),
144+
'extra' => [
145+
'Eloquent' => ['Illuminate\Database\Eloquent\Builder', 'Illuminate\Database\Query\Builder'],
146+
'Session' => ['Illuminate\Session\Store'],
147+
],
148148

149-
'magic' => array(),
149+
'magic' => [],
150150

151151
/*
152152
|--------------------------------------------------------------------------
@@ -158,9 +158,9 @@
158158
|
159159
*/
160160

161-
'interfaces' => array(
161+
'interfaces' => [
162162

163-
),
163+
],
164164

165165
/*
166166
|--------------------------------------------------------------------------
@@ -188,9 +188,9 @@
188188
| ),
189189
|
190190
*/
191-
'custom_db_types' => array(
191+
'custom_db_types' => [
192192

193-
),
193+
],
194194

195195
/*
196196
|--------------------------------------------------------------------------
@@ -226,10 +226,10 @@
226226
| Cast the given "real type" to the given "type".
227227
|
228228
*/
229-
'type_overrides' => array(
229+
'type_overrides' => [
230230
'integer' => 'int',
231231
'boolean' => 'bool',
232-
),
232+
],
233233

234234
/*
235235
|--------------------------------------------------------------------------
@@ -253,5 +253,4 @@
253253
|
254254
*/
255255
'force_fqn' => false,
256-
257-
);
256+
];

grumphp.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ grumphp:
55
triggered_by: [php]
66
metadata:
77
task: composer_script
8-
phpcs:
8+
phpcsfixer:
99
script: check-style
1010
triggered_by: [php]
1111
metadata:

src/Alias.php

+12-12
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111

1212
namespace Barryvdh\LaravelIdeHelper;
1313

14-
use Closure;
15-
use ReflectionClass;
1614
use Barryvdh\Reflection\DocBlock;
1715
use Barryvdh\Reflection\DocBlock\Context;
16+
use Barryvdh\Reflection\DocBlock\Serializer as DocBlockSerializer;
1817
use Barryvdh\Reflection\DocBlock\Tag\MethodTag;
18+
use Closure;
1919
use Illuminate\Config\Repository as ConfigRepository;
20-
use Barryvdh\Reflection\DocBlock\Serializer as DocBlockSerializer;
20+
use ReflectionClass;
2121

2222
class Alias
2323
{
@@ -31,12 +31,12 @@ class Alias
3131
protected $short;
3232
protected $namespace = '__root';
3333
protected $root = null;
34-
protected $classes = array();
35-
protected $methods = array();
36-
protected $usedMethods = array();
34+
protected $classes = [];
35+
protected $methods = [];
36+
protected $usedMethods = [];
3737
protected $valid = false;
38-
protected $magicMethods = array();
39-
protected $interfaces = array();
38+
protected $magicMethods = [];
39+
protected $interfaces = [];
4040
protected $phpdoc = null;
4141

4242
/** @var ConfigRepository */
@@ -50,7 +50,7 @@ class Alias
5050
* @param array $magicMethods
5151
* @param array $interfaces
5252
*/
53-
public function __construct($config, $alias, $facade, $magicMethods = array(), $interfaces = array())
53+
public function __construct($config, $alias, $facade, $magicMethods = [], $interfaces = [])
5454
{
5555
$this->alias = $alias;
5656
$this->magicMethods = $magicMethods;
@@ -82,7 +82,7 @@ public function __construct($config, $alias, $facade, $magicMethods = array(), $
8282

8383

8484
if ($facade === '\Illuminate\Database\Eloquent\Model') {
85-
$this->usedMethods = array('decrement', 'increment');
85+
$this->usedMethods = ['decrement', 'increment'];
8686
}
8787
}
8888

@@ -289,12 +289,12 @@ protected function detectRoot()
289289
//When the database connection is not set, some classes will be skipped
290290
} catch (\PDOException $e) {
291291
$this->error(
292-
"PDOException: " . $e->getMessage() .
292+
'PDOException: ' . $e->getMessage() .
293293
"\nPlease configure your database connection correctly, or use the sqlite memory driver (-M)." .
294294
" Skipping $facade."
295295
);
296296
} catch (\Exception $e) {
297-
$this->error("Exception: " . $e->getMessage() . "\nSkipping $facade.");
297+
$this->error('Exception: ' . $e->getMessage() . "\nSkipping $facade.");
298298
}
299299
}
300300

0 commit comments

Comments
 (0)