From 6e0457e39544b1b6b612d80c81b15472e8ef2601 Mon Sep 17 00:00:00 2001 From: Danny van der Sluijs Date: Mon, 4 Dec 2023 20:04:05 +0100 Subject: [PATCH] fix: Port https://github.com/squizlabs/PHP_CodeSniffer/pull/3881 to this repo --- .../Sniffs/Arrays/ArrayIndentSniff.php | 24 ++++++- .../Sniffs/Formatting/SpaceAfterCastSniff.php | 18 +++-- .../Sniffs/Formatting/SpaceAfterNotSniff.php | 18 +++-- .../Tests/Arrays/ArrayIndentUnitTest.inc | 16 +++++ .../Arrays/ArrayIndentUnitTest.inc.fixed | 16 +++++ .../Tests/Arrays/ArrayIndentUnitTest.php | 3 + .../Sniffs/Arrays/ArrayDeclarationSniff.php | 66 ++++++++++++++----- .../Commenting/DocCommentAlignmentSniff.php | 10 ++- .../ControlSignatureSniff.php | 18 ++++- .../Arrays/ArrayDeclarationUnitTest.1.inc | 6 ++ .../ArrayDeclarationUnitTest.1.inc.fixed | 6 ++ .../Arrays/ArrayDeclarationUnitTest.2.inc | 6 ++ .../ArrayDeclarationUnitTest.2.inc.fixed | 6 ++ .../Tests/Arrays/ArrayDeclarationUnitTest.php | 2 + 14 files changed, 179 insertions(+), 36 deletions(-) diff --git a/src/Standards/Generic/Sniffs/Arrays/ArrayIndentSniff.php b/src/Standards/Generic/Sniffs/Arrays/ArrayIndentSniff.php index 9bff5c6354..87da8f3cf9 100644 --- a/src/Standards/Generic/Sniffs/Arrays/ArrayIndentSniff.php +++ b/src/Standards/Generic/Sniffs/Arrays/ArrayIndentSniff.php @@ -76,9 +76,15 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array // check indent levels because it's not valid. But we don't enforce exactly // how far indented it should be. if ($startIndent < $baseIndent) { - $error = 'Array open brace not indented correctly; expected at least %s spaces but found %s'; + $pluralizeSpace = 's'; + if ($baseIndent === 1) { + $pluralizeSpace = ''; + } + + $error = 'Array open brace not indented correctly; expected at least %s space%s but found %s'; $data = [ $baseIndent, + $pluralizeSpace, $startIndent, ]; $fix = $phpcsFile->addFixableError($error, $stackPtr, 'OpenBraceIncorrect', $data); @@ -117,9 +123,15 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array continue; } - $error = 'Array key not indented correctly; expected %s spaces but found %s'; + $pluralizeSpace = 's'; + if ($expectedIndent === 1) { + $pluralizeSpace = ''; + } + + $error = 'Array key not indented correctly; expected %s space%s but found %s'; $data = [ $expectedIndent, + $pluralizeSpace, $foundIndent, ]; $fix = $phpcsFile->addFixableError($error, $first, 'KeyIncorrect', $data); @@ -154,9 +166,15 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array return; } - $error = 'Array close brace not indented correctly; expected %s spaces but found %s'; + $pluralizeSpace = 's'; + if ($expectedIndent === 1) { + $pluralizeSpace = ''; + } + + $error = 'Array close brace not indented correctly; expected %s space%s but found %s'; $data = [ $expectedIndent, + $pluralizeSpace, $foundIndent, ]; $fix = $phpcsFile->addFixableError($error, $arrayEnd, 'CloseBraceIncorrect', $data); diff --git a/src/Standards/Generic/Sniffs/Formatting/SpaceAfterCastSniff.php b/src/Standards/Generic/Sniffs/Formatting/SpaceAfterCastSniff.php index 36eff864ac..0ab8d3d52c 100644 --- a/src/Standards/Generic/Sniffs/Formatting/SpaceAfterCastSniff.php +++ b/src/Standards/Generic/Sniffs/Formatting/SpaceAfterCastSniff.php @@ -54,8 +54,12 @@ public function register() */ public function process(File $phpcsFile, $stackPtr) { - $tokens = $phpcsFile->getTokens(); - $this->spacing = (int) $this->spacing; + $tokens = $phpcsFile->getTokens(); + $this->spacing = (int) $this->spacing; + $pluralizeSpace = 's'; + if ($this->spacing === 1) { + $pluralizeSpace = ''; + } if ($tokens[$stackPtr]['code'] === T_BINARY_CAST && $tokens[$stackPtr]['content'] === 'b' @@ -83,8 +87,11 @@ public function process(File $phpcsFile, $stackPtr) $nextNonWhitespace = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); if ($nextNonEmpty !== $nextNonWhitespace) { - $error = 'Expected %s space(s) after cast statement; comment found'; - $data = [$this->spacing]; + $error = 'Expected %s space%s after cast statement; comment found'; + $data = [ + $this->spacing, + $pluralizeSpace, + ]; $phpcsFile->addError($error, $stackPtr, 'CommentFound', $data); if ($tokens[($stackPtr + 1)]['code'] === T_WHITESPACE) { @@ -109,9 +116,10 @@ public function process(File $phpcsFile, $stackPtr) return; } - $error = 'Expected %s space(s) after cast statement; %s found'; + $error = 'Expected %s space%s after cast statement; %s found'; $data = [ $this->spacing, + $pluralizeSpace, $found, ]; diff --git a/src/Standards/Generic/Sniffs/Formatting/SpaceAfterNotSniff.php b/src/Standards/Generic/Sniffs/Formatting/SpaceAfterNotSniff.php index 1ff25bdade..99e267d61d 100644 --- a/src/Standards/Generic/Sniffs/Formatting/SpaceAfterNotSniff.php +++ b/src/Standards/Generic/Sniffs/Formatting/SpaceAfterNotSniff.php @@ -64,8 +64,12 @@ public function register() */ public function process(File $phpcsFile, $stackPtr) { - $tokens = $phpcsFile->getTokens(); - $this->spacing = (int) $this->spacing; + $tokens = $phpcsFile->getTokens(); + $this->spacing = (int) $this->spacing; + $pluralizeSpace = 's'; + if ($this->spacing === 1) { + $pluralizeSpace = ''; + } $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); if ($nextNonEmpty === false) { @@ -84,8 +88,11 @@ public function process(File $phpcsFile, $stackPtr) $nextNonWhitespace = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); if ($nextNonEmpty !== $nextNonWhitespace) { - $error = 'Expected %s space(s) after NOT operator; comment found'; - $data = [$this->spacing]; + $error = 'Expected %s space%s after NOT operator; comment found'; + $data = [ + $this->spacing, + $pluralizeSpace, + ]; $phpcsFile->addError($error, $stackPtr, 'CommentFound', $data); return; } @@ -101,9 +108,10 @@ public function process(File $phpcsFile, $stackPtr) return; } - $error = 'Expected %s space(s) after NOT operator; %s found'; + $error = 'Expected %s space%s after NOT operator; %s found'; $data = [ $this->spacing, + $pluralizeSpace, $found, ]; diff --git a/src/Standards/Generic/Tests/Arrays/ArrayIndentUnitTest.inc b/src/Standards/Generic/Tests/Arrays/ArrayIndentUnitTest.inc index 075fc34c5b..bf100d1d5f 100644 --- a/src/Standards/Generic/Tests/Arrays/ArrayIndentUnitTest.inc +++ b/src/Standards/Generic/Tests/Arrays/ArrayIndentUnitTest.inc @@ -111,3 +111,19 @@ $array = [ name: $value ), ]; + +// phpcs:set Generic.Arrays.ArrayIndent indent 1 + +// Testing pluralization of indent text - array item indent. +$var = [ + 1 => 'one', + 2 => 'two', + /* three */ 3 => 'three', +]; + +// Testing pluralization of indent text - close brace indent. + $var = [ + 1 => 'one', + ]; + +// phpcs:set Generic.Arrays.ArrayIndent indent 4 diff --git a/src/Standards/Generic/Tests/Arrays/ArrayIndentUnitTest.inc.fixed b/src/Standards/Generic/Tests/Arrays/ArrayIndentUnitTest.inc.fixed index 505de5f780..ca56aaa3e0 100644 --- a/src/Standards/Generic/Tests/Arrays/ArrayIndentUnitTest.inc.fixed +++ b/src/Standards/Generic/Tests/Arrays/ArrayIndentUnitTest.inc.fixed @@ -112,3 +112,19 @@ $array = [ name: $value ), ]; + +// phpcs:set Generic.Arrays.ArrayIndent indent 1 + +// Testing pluralization of indent text - array item indent. +$var = [ + 1 => 'one', + 2 => 'two', + /* three */ 3 => 'three', +]; + +// Testing pluralization of indent text - close brace indent. + $var = [ + 1 => 'one', + ]; + +// phpcs:set Generic.Arrays.ArrayIndent indent 4 diff --git a/src/Standards/Generic/Tests/Arrays/ArrayIndentUnitTest.php b/src/Standards/Generic/Tests/Arrays/ArrayIndentUnitTest.php index 482742a356..c8b3fc6b69 100644 --- a/src/Standards/Generic/Tests/Arrays/ArrayIndentUnitTest.php +++ b/src/Standards/Generic/Tests/Arrays/ArrayIndentUnitTest.php @@ -45,6 +45,9 @@ public function getErrorList() 88 => 1, 98 => 1, 110 => 1, + 120 => 1, + 121 => 1, + 127 => 1, ]; }//end getErrorList() diff --git a/src/Standards/Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php b/src/Standards/Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php index 73454fd99f..f9e4ec035a 100644 --- a/src/Standards/Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php +++ b/src/Standards/Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php @@ -330,11 +330,17 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array } } else if ($tokens[$arrayEnd]['column'] !== $keywordStart) { // Check the closing bracket is lined up under the "a" in array. - $expected = ($keywordStart - 1); - $found = ($tokens[$arrayEnd]['column'] - 1); - $error = 'Closing parenthesis not aligned correctly; expected %s space(s) but found %s'; - $data = [ + $expected = ($keywordStart - 1); + $found = ($tokens[$arrayEnd]['column'] - 1); + $pluralizeSpace = 's'; + if ($expected === 1) { + $pluralizeSpace = ''; + } + + $error = 'Closing parenthesis not aligned correctly; expected %s space%s but found %s'; + $data = [ $expected, + $pluralizeSpace, $found, ]; @@ -674,12 +680,18 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array } else if ($previousIsWhitespace === true) { $expected = $keywordStart; - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $valuePointer, true); - $found = ($tokens[$first]['column'] - 1); + $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $valuePointer, true); + $found = ($tokens[$first]['column'] - 1); + $pluralizeSpace = 's'; + if ($expected === 1) { + $pluralizeSpace = ''; + } + if ($found !== $expected) { - $error = 'Array value not aligned correctly; expected %s spaces but found %s'; + $error = 'Array value not aligned correctly; expected %s space%s but found %s'; $data = [ $expected, + $pluralizeSpace, $found, ]; @@ -763,11 +775,17 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array } if ($tokens[$indexPointer]['column'] !== $indicesStart && ($indexPointer - 1) !== $arrayStart) { - $expected = ($indicesStart - 1); - $found = ($tokens[$indexPointer]['column'] - 1); - $error = 'Array key not aligned correctly; expected %s spaces but found %s'; - $data = [ + $expected = ($indicesStart - 1); + $found = ($tokens[$indexPointer]['column'] - 1); + $pluralizeSpace = 's'; + if ($expected === 1) { + $pluralizeSpace = ''; + } + + $error = 'Array key not aligned correctly; expected %s space%s but found %s'; + $data = [ $expected, + $pluralizeSpace, $found, ]; @@ -779,15 +797,21 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array $phpcsFile->fixer->replaceToken(($indexPointer - 1), str_repeat(' ', $expected)); } } - } + }//end if $arrowStart = ($tokens[$indexPointer]['column'] + $maxLength + 1); if ($tokens[$index['arrow']]['column'] !== $arrowStart) { - $expected = ($arrowStart - ($index['index_length'] + $tokens[$indexPointer]['column'])); - $found = ($tokens[$index['arrow']]['column'] - ($index['index_length'] + $tokens[$indexPointer]['column'])); - $error = 'Array double arrow not aligned correctly; expected %s space(s) but found %s'; - $data = [ + $expected = ($arrowStart - ($index['index_length'] + $tokens[$indexPointer]['column'])); + $found = ($tokens[$index['arrow']]['column'] - ($index['index_length'] + $tokens[$indexPointer]['column'])); + $pluralizeSpace = 's'; + if ($expected === 1) { + $pluralizeSpace = ''; + } + + $error = 'Array double arrow not aligned correctly; expected %s space%s but found %s'; + $data = [ $expected, + $pluralizeSpace, $found, ]; @@ -801,7 +825,7 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array } continue; - } + }//end if $valueStart = ($arrowStart + 3); if ($tokens[$valuePointer]['column'] !== $valueStart) { @@ -811,9 +835,15 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array $found = 'newline'; } - $error = 'Array value not aligned correctly; expected %s space(s) but found %s'; + $pluralizeSpace = 's'; + if ($expected === 1) { + $pluralizeSpace = ''; + } + + $error = 'Array value not aligned correctly; expected %s space%s but found %s'; $data = [ $expected, + $pluralizeSpace, $found, ]; diff --git a/src/Standards/Squiz/Sniffs/Commenting/DocCommentAlignmentSniff.php b/src/Standards/Squiz/Sniffs/Commenting/DocCommentAlignmentSniff.php index 55cd62526e..7259e533ec 100644 --- a/src/Standards/Squiz/Sniffs/Commenting/DocCommentAlignmentSniff.php +++ b/src/Standards/Squiz/Sniffs/Commenting/DocCommentAlignmentSniff.php @@ -112,9 +112,15 @@ public function process(File $phpcsFile, $stackPtr) } if ($tokens[$i]['column'] !== $requiredColumn) { - $error = 'Expected %s space(s) before asterisk; %s found'; + $pluralizeSpace = 's'; + if (($requiredColumn - 1) === 1) { + $pluralizeSpace = ''; + } + + $error = 'Expected %s space%s before asterisk; %s found'; $data = [ ($requiredColumn - 1), + $pluralizeSpace, ($tokens[$i]['column'] - 1), ]; $fix = $phpcsFile->addFixableError($error, $i, 'SpaceBeforeStar', $data); @@ -126,7 +132,7 @@ public function process(File $phpcsFile, $stackPtr) $phpcsFile->fixer->replaceToken(($i - 1), $padding); } } - } + }//end if if ($tokens[$i]['code'] !== T_DOC_COMMENT_STAR) { continue; diff --git a/src/Standards/Squiz/Sniffs/ControlStructures/ControlSignatureSniff.php b/src/Standards/Squiz/Sniffs/ControlStructures/ControlSignatureSniff.php index e2794aaf02..bb5392364a 100644 --- a/src/Standards/Squiz/Sniffs/ControlStructures/ControlSignatureSniff.php +++ b/src/Standards/Squiz/Sniffs/ControlStructures/ControlSignatureSniff.php @@ -105,9 +105,15 @@ public function process(File $phpcsFile, $stackPtr) } if ($found !== $expected) { - $error = 'Expected %s space(s) after %s keyword; %s found'; + $pluralizeSpace = 's'; + if ($expected === 1) { + $pluralizeSpace = ''; + } + + $error = 'Expected %s space%s after %s keyword; %s found'; $data = [ $expected, + $pluralizeSpace, strtoupper($tokens[$stackPtr]['content']), $found, ]; @@ -120,7 +126,7 @@ public function process(File $phpcsFile, $stackPtr) $phpcsFile->fixer->replaceToken(($stackPtr + 1), str_repeat(' ', $expected)); } } - } + }//end if // Single space after closing parenthesis. if (isset($tokens[$stackPtr]['parenthesis_closer']) === true @@ -146,9 +152,15 @@ public function process(File $phpcsFile, $stackPtr) } if ($found !== $expected) { - $error = 'Expected %s space(s) after closing parenthesis; found %s'; + $pluralizeSpace = 's'; + if ($expected === 1) { + $pluralizeSpace = ''; + } + + $error = 'Expected %s space%s after closing parenthesis; found %s'; $data = [ $expected, + $pluralizeSpace, $found, ]; diff --git a/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.1.inc b/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.1.inc index b96aec7bde..5b539a372a 100644 --- a/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.1.inc +++ b/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.1.inc @@ -529,3 +529,9 @@ $x = array( 'bar', 'baz' => 'bar', // KeySpecified (based on first entry). ); + + $x = + array( + 'a', + 'b', + ); diff --git a/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.1.inc.fixed b/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.1.inc.fixed index 9da6a4279b..5ebf715024 100644 --- a/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.1.inc.fixed +++ b/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.1.inc.fixed @@ -565,3 +565,9 @@ $x = array( 'bar', 'baz' => 'bar', // KeySpecified (based on first entry). ); + + $x = + array( + 'a', + 'b', + ); diff --git a/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc b/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc index 0c8b48fc89..7bcc5e044e 100644 --- a/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc +++ b/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc @@ -518,3 +518,9 @@ $x = [ 'bar', 'baz' => 'bar', // KeySpecified (based on first entry). ]; + + $x = + [ + 'a', + 'b', + ]; diff --git a/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc.fixed b/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc.fixed index 4b09e2f234..d156357c93 100644 --- a/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc.fixed +++ b/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc.fixed @@ -552,3 +552,9 @@ $x = [ 'bar', 'baz' => 'bar', // KeySpecified (based on first entry). ]; + + $x = + [ + 'a', + 'b', + ]; diff --git a/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.php b/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.php index d723e577e0..e4eb4f3b8f 100644 --- a/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.php +++ b/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.php @@ -128,6 +128,7 @@ public function getErrorList($testFile='') 516 => 1, 523 => 1, 530 => 1, + 537 => 1, ]; case 'ArrayDeclarationUnitTest.2.inc': return [ @@ -218,6 +219,7 @@ public function getErrorList($testFile='') 505 => 1, 512 => 1, 519 => 1, + 526 => 1, ]; default: return [];