Skip to content

Commit

Permalink
Feat: Add rules for attributes handling (AttributeEmptyParenthesesFix…
Browse files Browse the repository at this point in the history
…er, reconfigure MethodArgumentSpaceFixer, DisallowAttributesJoiningSniff, DisallowMultipleAttributesPerLineSniff) (part of #94)
  • Loading branch information
OndraM committed May 7, 2024
1 parent a9f7049 commit 6e6c355
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
15 changes: 15 additions & 0 deletions ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
use PhpCsFixer\Fixer\ArrayNotation\NormalizeIndexBraceFixer;
use PhpCsFixer\Fixer\ArrayNotation\TrimArraySpacesFixer;
use PhpCsFixer\Fixer\ArrayNotation\WhitespaceAfterCommaInArrayFixer;
use PhpCsFixer\Fixer\AttributeNotation\AttributeEmptyParenthesesFixer;
use PhpCsFixer\Fixer\Basic\BracesFixer;
use PhpCsFixer\Fixer\Basic\NoTrailingCommaInSinglelineFixer;
use PhpCsFixer\Fixer\Basic\PsrAutoloadingFixer;
Expand Down Expand Up @@ -72,6 +73,7 @@
use PhpCsFixer\Fixer\FunctionNotation\FunctionDeclarationFixer;
use PhpCsFixer\Fixer\FunctionNotation\ImplodeCallFixer;
use PhpCsFixer\Fixer\FunctionNotation\LambdaNotUsedImportFixer;
use PhpCsFixer\Fixer\FunctionNotation\MethodArgumentSpaceFixer;
use PhpCsFixer\Fixer\FunctionNotation\NoUnreachableDefaultArgumentValueFixer;
use PhpCsFixer\Fixer\FunctionNotation\NoUselessSprintfFixer;
use PhpCsFixer\Fixer\FunctionNotation\PhpdocToParamTypeFixer;
Expand Down Expand Up @@ -147,6 +149,8 @@
use PhpCsFixer\Fixer\Whitespace\NoWhitespaceInBlankLineFixer;
use PhpCsFixer\Fixer\Whitespace\TypeDeclarationSpacesFixer;
use PhpCsFixer\Fixer\Whitespace\TypesSpacesFixer;
use SlevomatCodingStandard\Sniffs\Attributes\DisallowAttributesJoiningSniff;
use SlevomatCodingStandard\Sniffs\Attributes\DisallowMultipleAttributesPerLineSniff;
use SlevomatCodingStandard\Sniffs\Classes\RequireConstructorPropertyPromotionSniff;
use SlevomatCodingStandard\Sniffs\ControlStructures\RequireNullSafeObjectOperatorSniff;
use SlevomatCodingStandard\Sniffs\Exceptions\ReferenceThrowableOnlySniff;
Expand Down Expand Up @@ -229,6 +233,8 @@
RandomApiMigrationFixer::class,
// Cast shall be used, not `settype()`
SetTypeToCastFixer::class,
// Attributes declared without arguments must not be followed by empty parentheses.
AttributeEmptyParenthesesFixer::class,
// Array index should always be written by using square braces
NormalizeIndexBraceFixer::class,
// 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.
Expand Down Expand Up @@ -409,6 +415,10 @@
PhpdocToParamTypeFixer::class,
// Takes `@return` annotation of non-mixed types and adjusts accordingly the function signature.
PhpdocToReturnTypeFixer::class,
// Requires that only one attribute can be placed inside #[].
DisallowAttributesJoiningSniff::class,
// Disallows multiple attributes of some target on same line.
DisallowMultipleAttributesPerLineSniff::class,
// Promote constructor properties
// @see https://github.com/FriendsOfPHP/PHP-CS-Fixer/issues/5956
RequireConstructorPropertyPromotionSniff::class,
Expand Down Expand Up @@ -562,6 +572,11 @@
FullyQualifiedStrictTypesFixer::class,
['import_symbols' => true], // Also import symbols from other namespaces than in current file
)
// Spaces and newlines in method arguments and attributes
->withConfiguredRule(
MethodArgumentSpaceFixer::class,
['attribute_placement' => 'standalone', 'on_multiline' => 'ensure_fully_multiline']
)
->withSkip([
// We allow empty catch statements (but they must have comment - see EmptyCatchCommentSniff)
EmptyStatementSniff::class . '.DetectedCatch' => null,
Expand Down
14 changes: 14 additions & 0 deletions tests/Integration/Fixtures/NewPhpFeatures.correct.php.inc
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,18 @@ class NewPhpFeatures
{
return null;
}

// AttributeEmptyParenthesesFixer
#[SomeFunctionAttribute]
#[AnotherAttribute('bar')]
#[AnotherAttribute]
#[First]
#[Second]
public function functionWithAttributes(
// MethodArgumentSpaceFixer
#[ParamAttribute]
#[AnotherAttribute('foo')]
string $foo,
string $bar,
): void {}
}
10 changes: 10 additions & 0 deletions tests/Integration/Fixtures/NewPhpFeatures.wrong.php.inc
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,14 @@ class NewPhpFeatures
{
return null;
}

// AttributeEmptyParenthesesFixer
#[SomeFunctionAttribute()]
#[AnotherAttribute('bar')]#[AnotherAttribute()]
#[First, Second]
public function functionWithAttributes(
// MethodArgumentSpaceFixer
#[ParamAttribute] #[AnotherAttribute('foo')] string $foo,
string $bar,
): void {}
}

0 comments on commit 6e6c355

Please sign in to comment.