Skip to content

Commit 7dcfd93

Browse files
committed
Add php-cs-fixer
1 parent d560be7 commit 7dcfd93

File tree

3 files changed

+167
-0
lines changed

3 files changed

+167
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Coding style
2+
3+
on:
4+
push:
5+
paths:
6+
- '**.php'
7+
- '.composer.json'
8+
- '.php_cs'
9+
- '.github/workflows/php-coding-style.yml'
10+
11+
jobs:
12+
style:
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- name: Checkout code
17+
uses: actions/checkout@v4
18+
19+
- name: Fix style
20+
uses: docker://oskarstark/php-cs-fixer-ga
21+
with:
22+
args: --config=.php-cs-fixer.php --allow-risky=yes
23+
24+
- name: Commit changes
25+
uses: stefanzweifel/git-auto-commit-action@v5
26+
with:
27+
commit_message: Fix coding style

.php-cs-fixer.php

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @see https://mlocati.github.io/php-cs-fixer-configurator/
7+
*/
8+
$finder = \PhpCsFixer\Finder::create()
9+
->in(__DIR__)
10+
->exclude(
11+
[
12+
'vendor',
13+
]
14+
)
15+
->name('*.php')
16+
->ignoreDotFiles(false)
17+
->ignoreVCS(true)
18+
->ignoreVCSIgnored(true);
19+
20+
return (new \PhpCsFixer\Config())
21+
->setUsingCache(true)
22+
->setCacheFile('./cache/.php-cs-fixer.cache')
23+
->setRiskyAllowed(true)
24+
->setIndent(' ')
25+
->setLineEnding("\n")
26+
->setRules([
27+
// Basic PER Coding Style 2.0 ruleset plus our "fixes" for it
28+
'@PER-CS2.0' => true, // https://www.php-fig.org/per/coding-style/}
29+
// 'concat_space' => ['spacing' => 'none'], // make strings shorter "'hello' . $name . '!'" => "'hello'.$name.'!'"
30+
// 'blank_line_after_opening_tag' => false, // it makes "<?php declare(strict_types=1);" multiline (and more verbose)
31+
'function_declaration' => false, // It makes "fn ()" into "fn()" and conflicts with our PHPCS ruleset
32+
'single_line_empty_body' => false, // It has conflict with PSR2.Classes.ClassDeclaration.OpenBraceNewLine
33+
// 'unary_operator_spaces' => false, // It has conflict with PHPCS ruleset
34+
35+
// Additional rules on the top of PER-CS2
36+
// Please keep these rules alphabetically
37+
'align_multiline_comment' => ['comment_type' => 'phpdocs_only'],
38+
'array_indentation' => true,
39+
'assign_null_coalescing_to_coalesce_equal' => true,
40+
'binary_operator_spaces' => ['default' => 'single_space'],
41+
'cast_spaces' => ['space' => 'single'],
42+
'class_attributes_separation' => ['elements' => ['method' => 'one']],
43+
'declare_strict_types' => true,
44+
'explicit_string_variable' => true,
45+
// 'final_public_method_for_abstract_class' => true, // @todo enable it
46+
'general_phpdoc_annotation_remove' => [
47+
'annotations' => [
48+
'api',
49+
'access',
50+
'author',
51+
'category',
52+
'copyright',
53+
'created',
54+
'license',
55+
'link',
56+
'package',
57+
'since',
58+
'subpackage',
59+
'version',
60+
],
61+
],
62+
'modernize_types_casting' => true,
63+
'mb_str_functions' => true,
64+
'no_alias_functions' => true,
65+
'no_binary_string' => true,
66+
'no_empty_comment' => true,
67+
'no_empty_phpdoc' => true,
68+
'no_empty_statement' => true,
69+
'no_extra_blank_lines' => ['tokens' => ['extra', 'curly_brace_block']],
70+
'no_homoglyph_names' => true,
71+
'no_leading_namespace_whitespace' => true,
72+
'no_mixed_echo_print' => true,
73+
'no_short_bool_cast' => true,
74+
'no_singleline_whitespace_before_semicolons' => true,
75+
'no_spaces_around_offset' => true,
76+
'no_trailing_comma_in_singleline' => false, // it's a good marker that there are more elements in an array
77+
'no_unneeded_braces' => true,
78+
'no_unneeded_control_parentheses' => true,
79+
'no_unneeded_final_method' => true,
80+
'no_unreachable_default_argument_value' => true,
81+
'no_unused_imports' => true,
82+
'no_useless_concat_operator' => true,
83+
'no_useless_return' => true,
84+
'no_whitespace_before_comma_in_array' => true,
85+
'normalize_index_brace' => true,
86+
'nullable_type_declaration' => ['syntax' => 'question_mark'],
87+
'object_operator_without_whitespace' => true,
88+
/*
89+
* @see https://github.com/slevomat/coding-standard/issues/1620#issuecomment-1758006718
90+
* 'ordered_class_elements' => [
91+
'order' => [
92+
'use_trait',
93+
'constant',
94+
'case', // for enums only
95+
'property',
96+
'method',
97+
]
98+
],*/
99+
'php_unit_construct' => true,
100+
'php_unit_dedicate_assert' => ['target' => 'newest'],
101+
'php_unit_expectation' => true,
102+
'php_unit_fqcn_annotation' => true,
103+
'php_unit_method_casing' => ['case' => 'snake_case'],
104+
'php_unit_no_expectation_annotation' => true,
105+
'php_unit_set_up_tear_down_visibility' => true,
106+
'php_unit_strict' => true,
107+
'php_unit_test_annotation' => ['style' => 'annotation'],
108+
'php_unit_test_class_requires_covers' => true,
109+
'phpdoc_align' => ['align' => 'left'],
110+
'phpdoc_indent' => true,
111+
'phpdoc_line_span' => ['const' => 'single', 'property' => 'single', 'method' => 'single'],
112+
'phpdoc_param_order' => true,
113+
'phpdoc_scalar' => true,
114+
'phpdoc_single_line_var_spacing' => true,
115+
'phpdoc_tag_casing' => true,
116+
'phpdoc_types' => true,
117+
'protected_to_private' => true,
118+
'psr_autoloading' => true,
119+
'self_accessor' => true,
120+
'self_static_accessor' => true,
121+
'single_line_comment_spacing' => true,
122+
'single_line_comment_style' => ['comment_types' => ['asterisk', 'hash']],
123+
'space_after_semicolon' => true,
124+
'standardize_not_equals' => true,
125+
'strict_param' => true,
126+
'ternary_to_null_coalescing' => true,
127+
'trim_array_spaces' => true,
128+
'trailing_comma_in_multiline' => true,
129+
'type_declaration_spaces' => true,
130+
'types_spaces' => ['space' => 'single'],
131+
'whitespace_after_comma_in_array' => true,
132+
'yoda_style' => ['equal' => false, 'identical' => false, 'less_and_greater' => false],
133+
])
134+
->setFinder($finder);

composer.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"illuminate/support": "^10.0 || ^11.0"
1919
},
2020
"require-dev": {
21+
"friendsofphp/php-cs-fixer": "^3.50",
2122
"geoip2/geoip2": "^2.11",
2223
"mockery/mockery": "^1.6",
2324
"phpstan/phpstan": "^1.10",
@@ -46,6 +47,9 @@
4647
"tests/TestFunctions.php"
4748
]
4849
},
50+
"config": {
51+
"sort-packages": true
52+
},
4953
"extra": {
5054
"laravel": {
5155
"aliases": {
@@ -57,6 +61,8 @@
5761
}
5862
},
5963
"scripts": {
64+
"cs": "@php-cs-fixer",
65+
"php-cs-fixer": "@php -d memory_limit=-1 vendor/bin/php-cs-fixer fix --no-interaction --ansi --verbose",
6066
"phpcs": "vendor/bin/phpcs --standard=phpcs.xml src",
6167
"phpstan": "vendor/bin/phpstan --level=0 --no-progress analyse --configuration phpstan.neon --memory-limit 2G",
6268
"psalm": "vendor/bin/psalm",

0 commit comments

Comments
 (0)