From a217c7e7e1ce74a63fa2faa1dd10645427a14f9a Mon Sep 17 00:00:00 2001 From: Nicolas Giraud Date: Tue, 7 Feb 2023 10:51:15 +0100 Subject: [PATCH 1/2] Allow multiple spaces in PropertyDeclaration sniff to respect more carefully the PSR-12. --- .../Classes/PropertyDeclarationSniff.php | 20 +++++++++---------- .../PropertyDeclarationUnitTest.inc.fixed | 6 +++--- .../Classes/PropertyDeclarationUnitTest.php | 3 --- 3 files changed, 12 insertions(+), 17 deletions(-) diff --git a/src/Standards/PSR2/Sniffs/Classes/PropertyDeclarationSniff.php b/src/Standards/PSR2/Sniffs/Classes/PropertyDeclarationSniff.php index efdbb43827..8b7d3b1295 100644 --- a/src/Standards/PSR2/Sniffs/Classes/PropertyDeclarationSniff.php +++ b/src/Standards/PSR2/Sniffs/Classes/PropertyDeclarationSniff.php @@ -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..91a71e9d7b 100644 --- a/src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.inc.fixed +++ b/src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.inc.fixed @@ -38,7 +38,7 @@ class ABC { public static $propD; protected static $propE; - private static /*comment*/ $propF; + private static /*comment*/ $propF; } class MyClass @@ -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..9ba9dcd9f8 100644 --- a/src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.php +++ b/src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.php @@ -35,7 +35,6 @@ public function getErrorList() 23 => 1, 38 => 1, 41 => 1, - 42 => 1, 50 => 2, 51 => 1, 55 => 1, @@ -45,9 +44,7 @@ public function getErrorList() 68 => 1, 69 => 1, 71 => 1, - 72 => 1, 76 => 1, - 80 => 1, 82 => 1, ]; From de283acdf2de89fe0d96117171b96a211981bc9b Mon Sep 17 00:00:00 2001 From: Nicolas Giraud Date: Thu, 9 Feb 2023 09:16:19 +0100 Subject: [PATCH 2/2] Fix unit tests. --- src/Standards/PSR2/Sniffs/Classes/PropertyDeclarationSniff.php | 2 +- .../PSR2/Tests/Classes/PropertyDeclarationUnitTest.inc.fixed | 2 +- .../PSR2/Tests/Classes/PropertyDeclarationUnitTest.php | 3 ++- .../Squiz/Tests/Commenting/FileCommentUnitTest.1.inc.fixed | 2 +- .../Squiz/Tests/Commenting/FileCommentUnitTest.1.js.fixed | 2 +- 5 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Standards/PSR2/Sniffs/Classes/PropertyDeclarationSniff.php b/src/Standards/PSR2/Sniffs/Classes/PropertyDeclarationSniff.php index 8b7d3b1295..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); diff --git a/src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.inc.fixed b/src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.inc.fixed index 91a71e9d7b..b40a5f551e 100644 --- a/src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.inc.fixed +++ b/src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.inc.fixed @@ -38,7 +38,7 @@ class ABC { public static $propD; protected static $propE; - private static /*comment*/ $propF; + private static /*comment*/ $propF; } class MyClass diff --git a/src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.php b/src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.php index 9ba9dcd9f8..e83e3145bd 100644 --- a/src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.php +++ b/src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.php @@ -35,6 +35,7 @@ public function getErrorList() 23 => 1, 38 => 1, 41 => 1, + 42 => 1, 50 => 2, 51 => 1, 55 => 1, @@ -43,7 +44,7 @@ public function getErrorList() 62 => 1, 68 => 1, 69 => 1, - 71 => 1, + 72 => 1, 76 => 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 *