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 and NoExtraBlankLinesFixer, DisallowAttributesJoiningSniff, DisallowMultipleAttributesPerLineSniff) (part of #94)
  • Loading branch information
OndraM committed May 23, 2024
1 parent 0709431 commit e94dc5d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
],
"require": {
"php": "^8.0",
"friendsofphp/php-cs-fixer": "^3.0",
"slevomat/coding-standard": "^8.0",
"friendsofphp/php-cs-fixer": "^3.47.1",
"slevomat/coding-standard": "^8.6",
"squizlabs/php_codesniffer": "^3.9",
"symplify/easy-coding-standard": "^12.1.5"
},
Expand Down
21 changes: 19 additions & 2 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 @@ -148,6 +150,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 @@ -230,6 +234,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 @@ -415,6 +421,10 @@
// Takes `@return` annotation of non-mixed types and adjusts accordingly the function signature.
PhpdocToReturnTypeFixer::class,
ReturnTypeHintSniff::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
// For php-cs-fixer implementation @see https://github.com/FriendsOfPHP/PHP-CS-Fixer/issues/5956
RequireConstructorPropertyPromotionSniff::class,
Expand Down Expand Up @@ -520,13 +530,15 @@
// Removes extra blank lines and/or blank lines following configuration
->withConfiguredRule(NoExtraBlankLinesFixer::class, [
'tokens' => [
'break',
'attribute',
'case',
'continue',
'curly_brace_block',
'default',
'extra',
'parenthesis_brace_block',
'return',
'square_brace_block',
'switch',
'throw',
'use',
'use_trait',
Expand Down Expand Up @@ -564,6 +576,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 {}
}
11 changes: 11 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,15 @@ 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 e94dc5d

Please sign in to comment.