From 074622bc355fa1b096a546dcb38abc622ea4491d Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 5 Sep 2021 04:59:16 +0200 Subject: [PATCH 1/2] PEAR/FunctionDeclaration: ignore multi-line promoted properties Similar to (multi-line) arrays in a multi-line function declaration, ignore (multi-line) attributes for the purposes of the indentation checks in this sniff. This does mean that inconsistent indentation within multi-line attributes/ for an attribute closer will not be fixed, but that should be handled by a dedicated attribute formatting sniff in my opinion. Includes unit tests. Fixes 3424 --- .../Functions/FunctionDeclarationSniff.php | 7 +++ .../Functions/FunctionDeclarationUnitTest.inc | 44 +++++++++++++++++++ .../FunctionDeclarationUnitTest.inc.fixed | 44 +++++++++++++++++++ .../Functions/FunctionDeclarationUnitTest.php | 2 + 4 files changed, 97 insertions(+) diff --git a/src/Standards/PEAR/Sniffs/Functions/FunctionDeclarationSniff.php b/src/Standards/PEAR/Sniffs/Functions/FunctionDeclarationSniff.php index 1924f9c4df..bfb3e5ea72 100644 --- a/src/Standards/PEAR/Sniffs/Functions/FunctionDeclarationSniff.php +++ b/src/Standards/PEAR/Sniffs/Functions/FunctionDeclarationSniff.php @@ -507,6 +507,13 @@ public function processArgumentList($phpcsFile, $stackPtr, $indent, $type='funct $lastLine = $tokens[$i]['line']; continue; } + + if ($tokens[$i]['code'] === T_ATTRIBUTE) { + // Skip attributes as they have their own indentation rules. + $i = $tokens[$i]['attribute_closer']; + $lastLine = $tokens[$i]['line']; + continue; + } }//end for }//end processArgumentList() diff --git a/src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.inc b/src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.inc index 0003ca0bda..d675d3f812 100644 --- a/src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.inc +++ b/src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.inc @@ -372,3 +372,47 @@ private string $private, ) { } } + +class ConstructorPropertyPromotionMultiLineAttributesOK + public function __construct( + #[ORM\ManyToOne( + Something: true, + SomethingElse: 'text', + )] + #[Groups([ + 'ArrayEntry', + 'Another.ArrayEntry', + ])] + #[MoreGroups( + [ + 'ArrayEntry', + 'Another.ArrayEntry', + ] + )] + private Type $property + ) { + // Do something. + } +} + +class ConstructorPropertyPromotionMultiLineAttributesIncorrectIndent + public function __construct( + #[ORM\ManyToOne( + Something: true, + SomethingElse: 'text', + )] + #[Groups([ + 'ArrayEntry', + 'Another.ArrayEntry', + ])] + #[MoreGroups( + [ + 'ArrayEntry', + 'Another.ArrayEntry', + ] + )] + private Type $property + ) { + // Do something. + } +} diff --git a/src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.inc.fixed b/src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.inc.fixed index 0f8d39db06..dce5d6535d 100644 --- a/src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.inc.fixed +++ b/src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.inc.fixed @@ -370,3 +370,47 @@ class ConstructorPropertyPromotionMultiLineDocblockAndAttributeIncorrectIndent ) { } } + +class ConstructorPropertyPromotionMultiLineAttributesOK + public function __construct( + #[ORM\ManyToOne( + Something: true, + SomethingElse: 'text', + )] + #[Groups([ + 'ArrayEntry', + 'Another.ArrayEntry', + ])] + #[MoreGroups( + [ + 'ArrayEntry', + 'Another.ArrayEntry', + ] + )] + private Type $property + ) { + // Do something. + } +} + +class ConstructorPropertyPromotionMultiLineAttributesIncorrectIndent + public function __construct( + #[ORM\ManyToOne( + Something: true, + SomethingElse: 'text', + )] + #[Groups([ + 'ArrayEntry', + 'Another.ArrayEntry', + ])] + #[MoreGroups( + [ + 'ArrayEntry', + 'Another.ArrayEntry', + ] + )] + private Type $property + ) { + // Do something. + } +} diff --git a/src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.php b/src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.php index 161d2b34f5..a97ae55c30 100644 --- a/src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.php +++ b/src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.php @@ -97,6 +97,8 @@ public function getErrorList($testFile='FunctionDeclarationUnitTest.inc') 369 => 1, 370 => 1, 371 => 1, + 400 => 1, + 404 => 1, ]; } else { $errors = [ From 18c5d6f922bb5818594b0fa018bba19db2ab5ee9 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 5 Sep 2021 04:59:51 +0200 Subject: [PATCH 2/2] PEAR/FunctionDeclaration: minor efficiency tweak --- .../PEAR/Sniffs/Functions/FunctionDeclarationSniff.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Standards/PEAR/Sniffs/Functions/FunctionDeclarationSniff.php b/src/Standards/PEAR/Sniffs/Functions/FunctionDeclarationSniff.php index bfb3e5ea72..bd59a7cd90 100644 --- a/src/Standards/PEAR/Sniffs/Functions/FunctionDeclarationSniff.php +++ b/src/Standards/PEAR/Sniffs/Functions/FunctionDeclarationSniff.php @@ -460,14 +460,14 @@ public function processArgumentList($phpcsFile, $stackPtr, $indent, $type='funct if ($tokens[$i]['code'] === T_WHITESPACE && $tokens[$i]['line'] !== $tokens[($i + 1)]['line'] ) { - // This is an empty line, so don't check the indent. - $foundIndent = $expectedIndent; - $error = 'Blank lines are not allowed in a multi-line '.$type.' declaration'; $fix = $phpcsFile->addFixableError($error, $i, 'EmptyLine'); if ($fix === true) { $phpcsFile->fixer->replaceToken($i, ''); } + + // This is an empty line, so don't check the indent. + continue; } else if ($tokens[$i]['code'] === T_WHITESPACE) { $foundIndent = $tokens[$i]['length']; } else if ($tokens[$i]['code'] === T_DOC_COMMENT_WHITESPACE) {