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 7, 2024
1 parent a9f7049 commit 6cfea73
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,9 +11,9 @@
],
"require": {
"php": "^8.0",
"friendsofphp/php-cs-fixer": "^3.0",
"friendsofphp/php-cs-fixer": "^3.47.1",
"nette/utils": "^3.2",
"slevomat/coding-standard": "^8.0",
"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 @@ -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 @@ -518,13 +528,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 @@ -562,6 +574,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 6cfea73

Please sign in to comment.