diff --git a/src/Standards/PSR2/Sniffs/Classes/PropertyDeclarationSniff.php b/src/Standards/PSR2/Sniffs/Classes/PropertyDeclarationSniff.php index efdbb43827..1782c70f17 100644 --- a/src/Standards/PSR2/Sniffs/Classes/PropertyDeclarationSniff.php +++ b/src/Standards/PSR2/Sniffs/Classes/PropertyDeclarationSniff.php @@ -73,7 +73,7 @@ protected function processMemberVar(File $phpcsFile, $stackPtr) if ($propertyInfo['type'] !== '') { $typeToken = $propertyInfo['type_end_token']; - $error = 'There must be 1 space after the property type declaration; %s found'; + $error = 'There must be a space after the property type declaration; %s found'; if ($tokens[($typeToken + 1)]['code'] !== T_WHITESPACE) { $data = ['0']; $fix = $phpcsFile->addFixableError($error, $typeToken, 'SpacingAfterType', $data); @@ -85,7 +85,9 @@ protected function processMemberVar(File $phpcsFile, $stackPtr) if ($tokens[$next]['line'] !== $tokens[$typeToken]['line']) { $found = 'newline'; } else { - $found = $tokens[($typeToken + 1)]['length']; + // According to PSR-12: "There MUST be a space between type declaration and property name." + // This does not mean a single space only is expected, but at least 1. So, if many, ignore sniff. + return; } $data = [$found]; @@ -96,17 +98,13 @@ protected function processMemberVar(File $phpcsFile, $stackPtr) } else { $fix = $phpcsFile->addFixableError($error, $typeToken, 'SpacingAfterType', $data); if ($fix === true) { - if ($found === 'newline') { - $phpcsFile->fixer->beginChangeset(); - for ($x = ($typeToken + 1); $x < $next; $x++) { - $phpcsFile->fixer->replaceToken($x, ''); - } - - $phpcsFile->fixer->addContent($typeToken, ' '); - $phpcsFile->fixer->endChangeset(); - } else { - $phpcsFile->fixer->replaceToken(($typeToken + 1), ' '); + $phpcsFile->fixer->beginChangeset(); + for ($x = ($typeToken + 1); $x < $next; $x++) { + $phpcsFile->fixer->replaceToken($x, ''); } + + $phpcsFile->fixer->addContent($typeToken, ' '); + $phpcsFile->fixer->endChangeset(); } } }//end if diff --git a/src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.inc.fixed b/src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.inc.fixed index df83112af2..b40a5f551e 100644 --- a/src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.inc.fixed +++ b/src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.inc.fixed @@ -65,7 +65,7 @@ class MyClass { public string $var = null; protected ?Folder\ClassName $var = null; - public int $var = null; + public int $var = null; public static int /*comment*/$var = null; } @@ -74,7 +74,7 @@ class ReadOnlyProp { $bar, $var = null; - protected readonly ?string $foo; + protected readonly ?string $foo; readonly array $foo; } diff --git a/src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.php b/src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.php index f1dd0194d2..e83e3145bd 100644 --- a/src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.php +++ b/src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.php @@ -44,10 +44,8 @@ public function getErrorList() 62 => 1, 68 => 1, 69 => 1, - 71 => 1, 72 => 1, 76 => 1, - 80 => 1, 82 => 1, ]; diff --git a/src/Standards/Squiz/Tests/Commenting/FileCommentUnitTest.1.inc.fixed b/src/Standards/Squiz/Tests/Commenting/FileCommentUnitTest.1.inc.fixed index e42e037c70..3bcd945388 100644 --- a/src/Standards/Squiz/Tests/Commenting/FileCommentUnitTest.1.inc.fixed +++ b/src/Standards/Squiz/Tests/Commenting/FileCommentUnitTest.1.inc.fixed @@ -25,7 +25,7 @@ * @author * @copyright 1997 Squiz Pty Ltd (ABN 77 084 670 600) * @copyright 1994-1997 Squiz Pty Ltd (ABN 77 084 670 600) -* @copyright 2022 Squiz Pty Ltd (ABN 77 084 670 600) +* @copyright 2023 Squiz Pty Ltd (ABN 77 084 670 600) * @license http://www.php.net/license/3_0.txt * @summary An unknown summary tag * diff --git a/src/Standards/Squiz/Tests/Commenting/FileCommentUnitTest.1.js.fixed b/src/Standards/Squiz/Tests/Commenting/FileCommentUnitTest.1.js.fixed index 90046c7102..56a392d985 100644 --- a/src/Standards/Squiz/Tests/Commenting/FileCommentUnitTest.1.js.fixed +++ b/src/Standards/Squiz/Tests/Commenting/FileCommentUnitTest.1.js.fixed @@ -25,7 +25,7 @@ * @author * @copyright 1997 Squiz Pty Ltd (ABN 77 084 670 600) * @copyright 1994-1997 Squiz Pty Ltd (ABN 77 084 670 600) -* @copyright 2022 Squiz Pty Ltd (ABN 77 084 670 600) +* @copyright 2023 Squiz Pty Ltd (ABN 77 084 670 600) * @license http://www.php.net/license/3_0.txt * @summary An unknown summary tag *