Skip to content

Commit e94dc5d

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

File tree

4 files changed

+46
-4
lines changed

4 files changed

+46
-4
lines changed

Diff for: composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
],
1212
"require": {
1313
"php": "^8.0",
14-
"friendsofphp/php-cs-fixer": "^3.0",
15-
"slevomat/coding-standard": "^8.0",
14+
"friendsofphp/php-cs-fixer": "^3.47.1",
15+
"slevomat/coding-standard": "^8.6",
1616
"squizlabs/php_codesniffer": "^3.9",
1717
"symplify/easy-coding-standard": "^12.1.5"
1818
},

Diff for: 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;
@@ -148,6 +150,8 @@
148150
use PhpCsFixer\Fixer\Whitespace\NoWhitespaceInBlankLineFixer;
149151
use PhpCsFixer\Fixer\Whitespace\TypeDeclarationSpacesFixer;
150152
use PhpCsFixer\Fixer\Whitespace\TypesSpacesFixer;
153+
use SlevomatCodingStandard\Sniffs\Attributes\DisallowAttributesJoiningSniff;
154+
use SlevomatCodingStandard\Sniffs\Attributes\DisallowMultipleAttributesPerLineSniff;
151155
use SlevomatCodingStandard\Sniffs\Classes\RequireConstructorPropertyPromotionSniff;
152156
use SlevomatCodingStandard\Sniffs\ControlStructures\RequireNullSafeObjectOperatorSniff;
153157
use SlevomatCodingStandard\Sniffs\Exceptions\ReferenceThrowableOnlySniff;
@@ -230,6 +234,8 @@
230234
RandomApiMigrationFixer::class,
231235
// Cast shall be used, not `settype()`
232236
SetTypeToCastFixer::class,
237+
// Attributes declared without arguments must not be followed by empty parentheses.
238+
AttributeEmptyParenthesesFixer::class,
233239
// Array index should always be written by using square braces
234240
NormalizeIndexBraceFixer::class,
235241
// 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.
@@ -415,6 +421,10 @@
415421
// Takes `@return` annotation of non-mixed types and adjusts accordingly the function signature.
416422
PhpdocToReturnTypeFixer::class,
417423
ReturnTypeHintSniff::class,
424+
// Requires that only one attribute can be placed inside #[].
425+
DisallowAttributesJoiningSniff::class,
426+
// Disallows multiple attributes of some target on same line.
427+
DisallowMultipleAttributesPerLineSniff::class,
418428
// Promote constructor properties
419429
// For php-cs-fixer implementation @see https://github.com/FriendsOfPHP/PHP-CS-Fixer/issues/5956
420430
RequireConstructorPropertyPromotionSniff::class,
@@ -520,13 +530,15 @@
520530
// Removes extra blank lines and/or blank lines following configuration
521531
->withConfiguredRule(NoExtraBlankLinesFixer::class, [
522532
'tokens' => [
523-
'break',
533+
'attribute',
534+
'case',
524535
'continue',
525536
'curly_brace_block',
537+
'default',
526538
'extra',
527539
'parenthesis_brace_block',
528-
'return',
529540
'square_brace_block',
541+
'switch',
530542
'throw',
531543
'use',
532544
'use_trait',
@@ -564,6 +576,11 @@
564576
FullyQualifiedStrictTypesFixer::class,
565577
['import_symbols' => true], // Also import symbols from other namespaces than in current file
566578
)
579+
// Spaces and newlines in method arguments and attributes
580+
->withConfiguredRule(
581+
MethodArgumentSpaceFixer::class,
582+
['attribute_placement' => 'standalone', 'on_multiline' => 'ensure_fully_multiline'],
583+
)
567584
->withSkip([
568585
// We allow empty catch statements (but they must have comment - see EmptyCatchCommentSniff)
569586
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

+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)