Skip to content

Commit b3bdce9

Browse files
committed
Feat: Add rules for attributes handling (AttributeEmptyParenthesesFixer, reconfigure MethodArgumentSpaceFixer and NoExtraBlankLinesFixer, DisallowAttributesJoiningSniff, DisallowMultipleAttributesPerLineSniff) (part of #94)
1 parent a9f7049 commit b3bdce9

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

ecs.php

+19-2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
use PhpCsFixer\Fixer\ArrayNotation\NormalizeIndexBraceFixer;
4444
use PhpCsFixer\Fixer\ArrayNotation\TrimArraySpacesFixer;
4545
use PhpCsFixer\Fixer\ArrayNotation\WhitespaceAfterCommaInArrayFixer;
46+
use PhpCsFixer\Fixer\AttributeNotation\AttributeEmptyParenthesesFixer;
4647
use PhpCsFixer\Fixer\Basic\BracesFixer;
4748
use PhpCsFixer\Fixer\Basic\NoTrailingCommaInSinglelineFixer;
4849
use PhpCsFixer\Fixer\Basic\PsrAutoloadingFixer;
@@ -72,6 +73,7 @@
7273
use PhpCsFixer\Fixer\FunctionNotation\FunctionDeclarationFixer;
7374
use PhpCsFixer\Fixer\FunctionNotation\ImplodeCallFixer;
7475
use PhpCsFixer\Fixer\FunctionNotation\LambdaNotUsedImportFixer;
76+
use PhpCsFixer\Fixer\FunctionNotation\MethodArgumentSpaceFixer;
7577
use PhpCsFixer\Fixer\FunctionNotation\NoUnreachableDefaultArgumentValueFixer;
7678
use PhpCsFixer\Fixer\FunctionNotation\NoUselessSprintfFixer;
7779
use PhpCsFixer\Fixer\FunctionNotation\PhpdocToParamTypeFixer;
@@ -147,6 +149,8 @@
147149
use PhpCsFixer\Fixer\Whitespace\NoWhitespaceInBlankLineFixer;
148150
use PhpCsFixer\Fixer\Whitespace\TypeDeclarationSpacesFixer;
149151
use PhpCsFixer\Fixer\Whitespace\TypesSpacesFixer;
152+
use SlevomatCodingStandard\Sniffs\Attributes\DisallowAttributesJoiningSniff;
153+
use SlevomatCodingStandard\Sniffs\Attributes\DisallowMultipleAttributesPerLineSniff;
150154
use SlevomatCodingStandard\Sniffs\Classes\RequireConstructorPropertyPromotionSniff;
151155
use SlevomatCodingStandard\Sniffs\ControlStructures\RequireNullSafeObjectOperatorSniff;
152156
use SlevomatCodingStandard\Sniffs\Exceptions\ReferenceThrowableOnlySniff;
@@ -229,6 +233,8 @@
229233
RandomApiMigrationFixer::class,
230234
// Cast shall be used, not `settype()`
231235
SetTypeToCastFixer::class,
236+
// Attributes declared without arguments must not be followed by empty parentheses.
237+
AttributeEmptyParenthesesFixer::class,
232238
// Array index should always be written by using square braces
233239
NormalizeIndexBraceFixer::class,
234240
// Empty body of class, interface, trait, enum or function must be abbreviated as {} and placed on the same line as the previous symbol, separated by a single space.
@@ -409,6 +415,10 @@
409415
PhpdocToParamTypeFixer::class,
410416
// Takes `@return` annotation of non-mixed types and adjusts accordingly the function signature.
411417
PhpdocToReturnTypeFixer::class,
418+
// Requires that only one attribute can be placed inside #[].
419+
DisallowAttributesJoiningSniff::class,
420+
// Disallows multiple attributes of some target on same line.
421+
DisallowMultipleAttributesPerLineSniff::class,
412422
// Promote constructor properties
413423
// @see https://github.com/FriendsOfPHP/PHP-CS-Fixer/issues/5956
414424
RequireConstructorPropertyPromotionSniff::class,
@@ -518,13 +528,15 @@
518528
// Removes extra blank lines and/or blank lines following configuration
519529
->withConfiguredRule(NoExtraBlankLinesFixer::class, [
520530
'tokens' => [
521-
'break',
531+
'attribute',
532+
'case',
522533
'continue',
523534
'curly_brace_block',
535+
'default',
524536
'extra',
525537
'parenthesis_brace_block',
526-
'return',
527538
'square_brace_block',
539+
'switch',
528540
'throw',
529541
'use',
530542
'use_trait',
@@ -562,6 +574,11 @@
562574
FullyQualifiedStrictTypesFixer::class,
563575
['import_symbols' => true], // Also import symbols from other namespaces than in current file
564576
)
577+
// Spaces and newlines in method arguments and attributes
578+
->withConfiguredRule(
579+
MethodArgumentSpaceFixer::class,
580+
['attribute_placement' => 'standalone', 'on_multiline' => 'ensure_fully_multiline'],
581+
)
565582
->withSkip([
566583
// We allow empty catch statements (but they must have comment - see EmptyCatchCommentSniff)
567584
EmptyStatementSniff::class . '.DetectedCatch' => null,

tests/Integration/Fixtures/NewPhpFeatures.correct.php.inc

+14
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,18 @@ class NewPhpFeatures
2929
{
3030
return null;
3131
}
32+
33+
// AttributeEmptyParenthesesFixer
34+
#[SomeFunctionAttribute]
35+
#[AnotherAttribute('bar')]
36+
#[AnotherAttribute]
37+
#[First]
38+
#[Second]
39+
public function functionWithAttributes(
40+
// MethodArgumentSpaceFixer
41+
#[ParamAttribute]
42+
#[AnotherAttribute('foo')]
43+
string $foo,
44+
string $bar,
45+
): void {}
3246
}

tests/Integration/Fixtures/NewPhpFeatures.wrong.php.inc

+11
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,15 @@ class NewPhpFeatures
3333
{
3434
return null;
3535
}
36+
37+
// AttributeEmptyParenthesesFixer
38+
#[SomeFunctionAttribute()]
39+
#[AnotherAttribute('bar')]#[AnotherAttribute()]
40+
41+
#[First, Second]
42+
public function functionWithAttributes(
43+
// MethodArgumentSpaceFixer
44+
#[ParamAttribute] #[AnotherAttribute('foo')] string $foo,
45+
string $bar,
46+
): void {}
3647
}

0 commit comments

Comments
 (0)