Skip to content

Commit bcaf355

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

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

Diff for: ecs.php

+15
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,
@@ -562,6 +572,11 @@
562572
FullyQualifiedStrictTypesFixer::class,
563573
['import_symbols' => true], // Also import symbols from other namespaces than in current file
564574
)
575+
// Spaces and newlines in method arguments and attributes
576+
->withConfiguredRule(
577+
MethodArgumentSpaceFixer::class,
578+
['attribute_placement' => 'standalone', 'on_multiline' => 'ensure_fully_multiline'],
579+
)
565580
->withSkip([
566581
// We allow empty catch statements (but they must have comment - see EmptyCatchCommentSniff)
567582
EmptyStatementSniff::class . '.DetectedCatch' => null,

Diff for: 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
}

Diff for: tests/Integration/Fixtures/NewPhpFeatures.wrong.php.inc

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

0 commit comments

Comments
 (0)