Skip to content

Commit d07aba6

Browse files
committed
Add CS-Fixer config and custom fixer
1 parent 69d753a commit d07aba6

File tree

2 files changed

+285
-0
lines changed

2 files changed

+285
-0
lines changed

.php_cs.dist

+213
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
<?php
2+
3+
require __DIR__ . '/tools/php-cs-fixer.d/PhpdocSingleLineVarFixer.php';
4+
5+
return PhpCsFixer\Config::create()
6+
->registerCustomFixers([
7+
new \PharIo\CSFixer\PhpdocSingleLineVarFixer()
8+
])
9+
->setRiskyAllowed(true)
10+
->setRules(
11+
[
12+
'PharIo/phpdoc_single_line_var_fixer' => true,
13+
14+
'align_multiline_comment' => true,
15+
'array_indentation' => true,
16+
'array_syntax' => ['syntax' => 'short'],
17+
'binary_operator_spaces' => [
18+
'operators' => [
19+
'=' => 'align',
20+
'=>' => 'align',
21+
],
22+
],
23+
'blank_line_after_namespace' => true,
24+
'blank_line_after_opening_tag' => false,
25+
'blank_line_before_statement' => [
26+
'statements' => [
27+
'break',
28+
'continue',
29+
'declare',
30+
'do',
31+
'for',
32+
'foreach',
33+
'if',
34+
'include',
35+
'include_once',
36+
'require',
37+
'require_once',
38+
'return',
39+
'switch',
40+
'throw',
41+
'try',
42+
'while',
43+
'yield',
44+
],
45+
],
46+
'braces' => [
47+
'allow_single_line_closure' => false,
48+
'position_after_anonymous_constructs' => 'same',
49+
'position_after_control_structures' => 'same',
50+
'position_after_functions_and_oop_constructs' => 'same'
51+
],
52+
'cast_spaces' => ['space' => 'none'],
53+
54+
// This fixer removes the blank line at class start, no way to disable that, so we disable the fixer :(
55+
//'class_attributes_separation' => ['elements' => ['const', 'method', 'property']],
56+
57+
'combine_consecutive_issets' => true,
58+
'combine_consecutive_unsets' => true,
59+
'compact_nullable_typehint' => true,
60+
'concat_space' => ['spacing' => 'one'],
61+
'date_time_immutable' => true,
62+
'declare_equal_normalize' => ['space' => 'single'],
63+
'declare_strict_types' => true,
64+
'dir_constant' => true,
65+
'elseif' => true,
66+
'encoding' => true,
67+
'full_opening_tag' => true,
68+
'fully_qualified_strict_types' => true,
69+
'function_declaration' => [
70+
'closure_function_spacing' => 'one'
71+
],
72+
'header_comment' => false,
73+
'indentation_type' => true,
74+
'is_null' => true,
75+
'line_ending' => true,
76+
'list_syntax' => ['syntax' => 'short'],
77+
'logical_operators' => true,
78+
'lowercase_cast' => true,
79+
'lowercase_constants' => true,
80+
'lowercase_keywords' => true,
81+
'lowercase_static_reference' => true,
82+
'magic_constant_casing' => true,
83+
'method_argument_space' => ['ensure_fully_multiline' => true],
84+
'modernize_types_casting' => true,
85+
'multiline_comment_opening_closing' => true,
86+
'multiline_whitespace_before_semicolons' => true,
87+
'native_constant_invocation' => true,
88+
'native_function_casing' => true,
89+
'native_function_invocation' => true,
90+
'new_with_braces' => false,
91+
'no_alias_functions' => true,
92+
'no_alternative_syntax' => true,
93+
'no_blank_lines_after_class_opening' => false,
94+
'no_blank_lines_after_phpdoc' => true,
95+
'no_blank_lines_before_namespace' => true,
96+
'no_closing_tag' => true,
97+
'no_empty_comment' => true,
98+
'no_empty_phpdoc' => true,
99+
'no_empty_statement' => true,
100+
'no_extra_blank_lines' => true,
101+
'no_homoglyph_names' => true,
102+
'no_leading_import_slash' => true,
103+
'no_leading_namespace_whitespace' => true,
104+
'no_mixed_echo_print' => ['use' => 'print'],
105+
'no_multiline_whitespace_around_double_arrow' => true,
106+
'no_null_property_initialization' => true,
107+
'no_php4_constructor' => true,
108+
'no_short_bool_cast' => true,
109+
'no_short_echo_tag' => true,
110+
'no_singleline_whitespace_before_semicolons' => true,
111+
'no_spaces_after_function_name' => true,
112+
'no_spaces_inside_parenthesis' => true,
113+
'no_superfluous_elseif' => true,
114+
'no_superfluous_phpdoc_tags' => true,
115+
'no_trailing_comma_in_list_call' => true,
116+
'no_trailing_comma_in_singleline_array' => true,
117+
'no_trailing_whitespace' => true,
118+
'no_trailing_whitespace_in_comment' => true,
119+
'no_unneeded_control_parentheses' => false,
120+
'no_unneeded_curly_braces' => false,
121+
'no_unneeded_final_method' => true,
122+
'no_unreachable_default_argument_value' => true,
123+
'no_unset_on_property' => true,
124+
'no_unused_imports' => true,
125+
'no_useless_else' => true,
126+
'no_useless_return' => true,
127+
'no_whitespace_before_comma_in_array' => true,
128+
'no_whitespace_in_blank_line' => true,
129+
'non_printable_character' => true,
130+
'normalize_index_brace' => true,
131+
'object_operator_without_whitespace' => true,
132+
'ordered_class_elements' => [
133+
'order' => [
134+
'use_trait',
135+
'constant_public',
136+
'constant_protected',
137+
'constant_private',
138+
'property_public_static',
139+
'property_protected_static',
140+
'property_private_static',
141+
'property_public',
142+
'property_protected',
143+
'property_private',
144+
'method_public_static',
145+
'construct',
146+
'destruct',
147+
'magic',
148+
'phpunit',
149+
'method_public',
150+
'method_protected',
151+
'method_private',
152+
'method_protected_static',
153+
'method_private_static',
154+
],
155+
],
156+
'ordered_imports' => true,
157+
'phpdoc_add_missing_param_annotation' => true,
158+
'phpdoc_align' => true,
159+
'phpdoc_annotation_without_dot' => true,
160+
'phpdoc_indent' => true,
161+
'phpdoc_no_access' => true,
162+
'phpdoc_no_empty_return' => true,
163+
'phpdoc_no_package' => true,
164+
'phpdoc_order' => true,
165+
'phpdoc_return_self_reference' => true,
166+
'phpdoc_scalar' => true,
167+
'phpdoc_separation' => true,
168+
'phpdoc_single_line_var_spacing' => true,
169+
'phpdoc_to_comment' => true,
170+
'phpdoc_trim' => true,
171+
'phpdoc_trim_consecutive_blank_line_separation' => true,
172+
'phpdoc_types' => ['groups' => ['simple', 'meta']],
173+
'phpdoc_types_order' => true,
174+
'phpdoc_to_return_type' => true,
175+
'phpdoc_var_without_name' => true,
176+
'pow_to_exponentiation' => true,
177+
'protected_to_private' => true,
178+
'return_assignment' => true,
179+
'return_type_declaration' => ['space_before' => 'none'],
180+
'self_accessor' => false,
181+
'semicolon_after_instruction' => true,
182+
'set_type_to_cast' => true,
183+
'short_scalar_cast' => true,
184+
'simplified_null_return' => true,
185+
'single_blank_line_at_eof' => true,
186+
'single_import_per_statement' => true,
187+
'single_line_after_imports' => true,
188+
'single_quote' => true,
189+
'standardize_not_equals' => true,
190+
'ternary_to_null_coalescing' => true,
191+
'trailing_comma_in_multiline_array' => false,
192+
'trim_array_spaces' => true,
193+
'unary_operator_spaces' => true,
194+
'visibility_required' => [
195+
'elements' => [
196+
'const',
197+
'method',
198+
'property',
199+
],
200+
],
201+
'void_return' => true,
202+
'whitespace_after_comma_in_array' => true,
203+
'yoda_style' => false
204+
]
205+
)
206+
->setFinder(
207+
PhpCsFixer\Finder::create()
208+
->files()
209+
->in(__DIR__ . '/build')
210+
->in(__DIR__ . '/src')
211+
->in(__DIR__ . '/tests')
212+
->notName('autoload.php')
213+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
namespace PharIo\CSFixer;
3+
4+
use PhpCsFixer\Fixer\DefinedFixerInterface;
5+
use PhpCsFixer\FixerDefinition\FixerDefinition;
6+
use PhpCsFixer\Tokenizer\Tokens;
7+
use PhpCsFixer\Tokenizer\Token;
8+
9+
/**
10+
* Main implementation taken from kubawerlos/php-cs-fixer-customere-fixers
11+
* Copyright (c) 2018 Kuba Werłos
12+
*
13+
* Slightly modified to work without the gazillion of composer dependencies
14+
*
15+
* Original:
16+
* https://github.com/kubawerlos/php-cs-fixer-custom-fixers/blob/master/src/Fixer/PhpdocSingleLineVarFixer.php
17+
*
18+
*/
19+
class PhpdocSingleLineVarFixer implements DefinedFixerInterface {
20+
21+
public function getDefinition(): FixerDefinition {
22+
return new FixerDefinition(
23+
'`@var` annotation must be in single line when is the only content.',
24+
[new CodeSample('<?php
25+
/**
26+
* @var string
27+
*/
28+
')]
29+
);
30+
}
31+
32+
public function isCandidate(Tokens $tokens): bool {
33+
return $tokens->isTokenKindFound(T_DOC_COMMENT);
34+
}
35+
36+
public function isRisky(): bool {
37+
return false;
38+
}
39+
40+
public function fix(\SplFileInfo $file, Tokens $tokens): void {
41+
foreach($tokens as $index => $token) {
42+
if (!$token->isGivenKind(T_DOC_COMMENT)) {
43+
continue;
44+
}
45+
if (\stripos($token->getContent(), '@var') === false) {
46+
continue;
47+
}
48+
49+
if (preg_match('#^/\*\*[\s\*]+(@var[^\r\n]+)[\s\*]*\*\/$#u', $token->getContent(), $matches) !== 1) {
50+
continue;
51+
}
52+
$newContent = '/** ' . \rtrim($matches[1]) . ' */';
53+
if ($newContent === $token->getContent()) {
54+
continue;
55+
}
56+
$tokens[$index] = new Token([T_DOC_COMMENT, $newContent]);
57+
}
58+
}
59+
60+
public function getPriority(): int {
61+
return 0;
62+
}
63+
64+
public function getName() {
65+
return 'PharIo/phpdoc_single_line_var_fixer';
66+
}
67+
68+
public function supports(\SplFileInfo $file) {
69+
return true;
70+
}
71+
72+
}

0 commit comments

Comments
 (0)