diff --git a/src/Standards/Squiz/Sniffs/WhiteSpace/MemberVarSpacingSniff.php b/src/Standards/Squiz/Sniffs/WhiteSpace/MemberVarSpacingSniff.php index 847b43b8c5..96cfd7aa95 100644 --- a/src/Standards/Squiz/Sniffs/WhiteSpace/MemberVarSpacingSniff.php +++ b/src/Standards/Squiz/Sniffs/WhiteSpace/MemberVarSpacingSniff.php @@ -45,23 +45,29 @@ protected function processMemberVar(File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); + $stopPoints = [ + T_SEMICOLON, + T_OPEN_CURLY_BRACKET, + T_CLOSE_CURLY_BRACKET, + ]; + + $endOfPreviousStatement = $phpcsFile->findPrevious($stopPoints, ($stackPtr - 1), null, false, null, true); + $validPrefixes = Tokens::$methodPrefixes; $validPrefixes[] = T_VAR; $validPrefixes[] = T_READONLY; - $startOfStatement = $phpcsFile->findPrevious($validPrefixes, ($stackPtr - 1), null, false, null, true); + $startOfStatement = $phpcsFile->findNext($validPrefixes, ($endOfPreviousStatement + 1), $stackPtr, false, null, true); if ($startOfStatement === false) { + // Parse error/live coding - property without modifier. Bow out. return; } $endOfStatement = $phpcsFile->findNext(T_SEMICOLON, ($stackPtr + 1), null, false, null, true); - $ignore = $validPrefixes; - $ignore[T_WHITESPACE] = T_WHITESPACE; - $start = $startOfStatement; for ($prev = ($startOfStatement - 1); $prev >= 0; $prev--) { - if (isset($ignore[$tokens[$prev]['code']]) === true) { + if ($tokens[$prev]['code'] === T_WHITESPACE) { continue; } diff --git a/src/Standards/Squiz/Tests/WhiteSpace/MemberVarSpacingUnitTest.inc.fixed b/src/Standards/Squiz/Tests/WhiteSpace/MemberVarSpacingUnitTest.1.inc similarity index 98% rename from src/Standards/Squiz/Tests/WhiteSpace/MemberVarSpacingUnitTest.inc.fixed rename to src/Standards/Squiz/Tests/WhiteSpace/MemberVarSpacingUnitTest.1.inc index 12953c21a3..b2e9db886e 100644 --- a/src/Standards/Squiz/Tests/WhiteSpace/MemberVarSpacingUnitTest.inc.fixed +++ b/src/Standards/Squiz/Tests/WhiteSpace/MemberVarSpacingUnitTest.1.inc @@ -1,9 +1,9 @@ 'a', 'b' => 'b' ), @@ -259,9 +270,9 @@ class ClassUsingSimpleTraits { use HelloWorld; + /* comment */ public $firstVar = array( 'a', 'b' ); - protected $secondVar = true; } @@ -272,8 +283,11 @@ class ClassUsingComplexTraits A::bigTalk insteadof B; } + + public $firstVar = array( 'a', 'b' ); + /* comment */ protected $secondVar = true; } @@ -311,7 +325,6 @@ class CommentedOutCodeAtStartOfClass { class CommentedOutCodeAtStartOfClassNoBlankLine { // phpcs:disable Stnd.Cat.Sniff -- For reasons. - /** * Description. * @@ -322,21 +335,24 @@ class CommentedOutCodeAtStartOfClassNoBlankLine { class HasAttributes { - /** * Short description of the member variable. * * @var array */ + #[ORM\Id]#[ORM\Column("integer")] + private $id; + /** * Short description of the member variable. * * @var array */ #[ORM\GeneratedValue] + #[ORM\Column(ORM\Column::T_INTEGER)] protected $height; @@ -346,7 +362,6 @@ class HasAttributes #[FirstAttribute] #[SecondAttribute] protected $propertyDouble; - #[ThirdAttribute] protected $propertyWithoutSpacing; } @@ -359,12 +374,14 @@ enum SomeEnum } class SupportReadonlyProperties { - readonly int $readonlyA; - public readonly string $publicReadonly; - readonly bool $readonlyB; - readonly private bool $readonlyPrivate; } + +class NoPreambleMultilineDeclaration { + public + static + int $prop = 1; +} diff --git a/src/Standards/Squiz/Tests/WhiteSpace/MemberVarSpacingUnitTest.inc b/src/Standards/Squiz/Tests/WhiteSpace/MemberVarSpacingUnitTest.1.inc.fixed similarity index 98% rename from src/Standards/Squiz/Tests/WhiteSpace/MemberVarSpacingUnitTest.inc rename to src/Standards/Squiz/Tests/WhiteSpace/MemberVarSpacingUnitTest.1.inc.fixed index 8072ece66b..86835dfc6d 100644 --- a/src/Standards/Squiz/Tests/WhiteSpace/MemberVarSpacingUnitTest.inc +++ b/src/Standards/Squiz/Tests/WhiteSpace/MemberVarSpacingUnitTest.1.inc.fixed @@ -1,8 +1,8 @@ 'a', 'b' => 'b' ), @@ -270,9 +259,9 @@ class ClassUsingSimpleTraits { use HelloWorld; - /* comment */ public $firstVar = array( 'a', 'b' ); + protected $secondVar = true; } @@ -283,11 +272,8 @@ class ClassUsingComplexTraits A::bigTalk insteadof B; } - - public $firstVar = array( 'a', 'b' ); - /* comment */ protected $secondVar = true; } @@ -325,6 +311,7 @@ class CommentedOutCodeAtStartOfClass { class CommentedOutCodeAtStartOfClassNoBlankLine { // phpcs:disable Stnd.Cat.Sniff -- For reasons. + /** * Description. * @@ -335,24 +322,21 @@ class CommentedOutCodeAtStartOfClassNoBlankLine { class HasAttributes { + /** * Short description of the member variable. * * @var array */ - #[ORM\Id]#[ORM\Column("integer")] - private $id; - /** * Short description of the member variable. * * @var array */ #[ORM\GeneratedValue] - #[ORM\Column(ORM\Column::T_INTEGER)] protected $height; @@ -362,6 +346,7 @@ class HasAttributes #[FirstAttribute] #[SecondAttribute] protected $propertyDouble; + #[ThirdAttribute] protected $propertyWithoutSpacing; } @@ -374,8 +359,19 @@ enum SomeEnum } class SupportReadonlyProperties { + readonly int $readonlyA; + public readonly string $publicReadonly; + readonly bool $readonlyB; + readonly private bool $readonlyPrivate; } + +class NoPreambleMultilineDeclaration { + + public + static + int $prop = 1; +} diff --git a/src/Standards/Squiz/Tests/WhiteSpace/MemberVarSpacingUnitTest.2.inc b/src/Standards/Squiz/Tests/WhiteSpace/MemberVarSpacingUnitTest.2.inc new file mode 100644 index 0000000000..2aab7c0591 --- /dev/null +++ b/src/Standards/Squiz/Tests/WhiteSpace/MemberVarSpacingUnitTest.2.inc @@ -0,0 +1,10 @@ + */ - public function getErrorList() + public function getErrorList($testFile='') { - return [ - 4 => 1, - 7 => 1, - 20 => 1, - 30 => 1, - 35 => 1, - 44 => 1, - 50 => 1, - 73 => 1, - 86 => 1, - 106 => 1, - 115 => 1, - 150 => 1, - 160 => 1, - 165 => 1, - 177 => 1, - 186 => 1, - 200 => 1, - 209 => 1, - 211 => 1, - 224 => 1, - 229 => 1, - 241 => 1, - 246 => 1, - 252 => 1, - 254 => 1, - 261 => 1, - 275 => 1, - 276 => 1, - 288 => 1, - 292 => 1, - 333 => 1, - 342 => 1, - 346 => 1, - 353 => 1, - 357 => 1, - 366 => 1, - 377 => 1, - 378 => 1, - 379 => 1, - 380 => 1, - ]; + switch ($testFile) { + case 'MemberVarSpacingUnitTest.1.inc': + return [ + 4 => 1, + 7 => 1, + 20 => 1, + 30 => 1, + 35 => 1, + 44 => 1, + 50 => 1, + 73 => 1, + 86 => 1, + 106 => 1, + 115 => 1, + 150 => 1, + 160 => 1, + 165 => 1, + 177 => 1, + 186 => 1, + 200 => 1, + 209 => 1, + 211 => 1, + 224 => 1, + 229 => 1, + 241 => 1, + 246 => 1, + 252 => 1, + 254 => 1, + 261 => 1, + 275 => 1, + 276 => 1, + 288 => 1, + 292 => 1, + 333 => 1, + 342 => 1, + 346 => 1, + 353 => 1, + 357 => 1, + 366 => 1, + 377 => 1, + 378 => 1, + 379 => 1, + 380 => 1, + 384 => 1, + ]; + + default: + return []; + }//end switch }//end getErrorList()