From cf5cad161b624255794947764bcd83653824afd5 Mon Sep 17 00:00:00 2001 From: Danny van der Sluijs Date: Thu, 24 Aug 2023 17:05:59 +0200 Subject: [PATCH 1/5] fix: Correct pluralization of the word space/spaces depending on the spacing value --- .../Sniffs/Arrays/ArrayIndentSniff.php | 7 ++++- .../Sniffs/Formatting/SpaceAfterCastSniff.php | 14 ++++++++-- .../Sniffs/Formatting/SpaceAfterNotSniff.php | 14 ++++++++-- .../Sniffs/Arrays/ArrayDeclarationSniff.php | 28 ++++++++++++++++--- .../Commenting/DocCommentAlignmentSniff.php | 7 ++++- .../ControlSignatureSniff.php | 14 ++++++++-- 6 files changed, 70 insertions(+), 14 deletions(-) diff --git a/src/Standards/Generic/Sniffs/Arrays/ArrayIndentSniff.php b/src/Standards/Generic/Sniffs/Arrays/ArrayIndentSniff.php index adb74ec8cb..0a0a57f787 100644 --- a/src/Standards/Generic/Sniffs/Arrays/ArrayIndentSniff.php +++ b/src/Standards/Generic/Sniffs/Arrays/ArrayIndentSniff.php @@ -153,10 +153,15 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array if ($foundIndent === $expectedIndent) { return; } + $pluralizeSpace = 's'; + if ($expectedIndent === 1) { + $pluralizeSpace = ''; + } - $error = 'Array close brace not indented correctly; expected %s spaces but found %s'; + $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 f19489e8e5..0a73b4d16e 100644 --- a/src/Standards/Generic/Sniffs/Formatting/SpaceAfterCastSniff.php +++ b/src/Standards/Generic/Sniffs/Formatting/SpaceAfterCastSniff.php @@ -56,6 +56,10 @@ public function process(File $phpcsFile, $stackPtr) { $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 b74ca80edc..45ce3e7f0c 100644 --- a/src/Standards/Generic/Sniffs/Formatting/SpaceAfterNotSniff.php +++ b/src/Standards/Generic/Sniffs/Formatting/SpaceAfterNotSniff.php @@ -66,6 +66,10 @@ public function process(File $phpcsFile, $stackPtr) { $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/Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php b/src/Standards/Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php index 64d5d64da2..f93fa5e0e3 100644 --- a/src/Standards/Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php +++ b/src/Standards/Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php @@ -332,9 +332,14 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array // 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'; + $pluralizeSpace = 's'; + if ($expected === 1) { + $pluralizeSpace = ''; + } + $error = 'Closing parenthesis not aligned correctly; expected %s space%s but found %s'; $data = [ $expected, + $pluralizeSpace, $found, ]; @@ -676,10 +681,15 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array $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, ]; @@ -785,9 +795,14 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array 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'; + $pluralizeSpace = 's'; + if ($expected === 1) { + $pluralizeSpace = ''; + } + $error = 'Array double arrow not aligned correctly; expected %s space%s but found %s'; $data = [ $expected, + $pluralizeSpace, $found, ]; @@ -810,10 +825,15 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array if ($found < 0) { $found = 'newline'; } + $pluralizeSpace = 's'; + if ($expected === 1) { + $pluralizeSpace = ''; + } - $error = 'Array value not aligned correctly; expected %s space(s) but found %s'; + $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 1f49d2c0c9..78934d166e 100644 --- a/src/Standards/Squiz/Sniffs/Commenting/DocCommentAlignmentSniff.php +++ b/src/Standards/Squiz/Sniffs/Commenting/DocCommentAlignmentSniff.php @@ -112,9 +112,14 @@ 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); diff --git a/src/Standards/Squiz/Sniffs/ControlStructures/ControlSignatureSniff.php b/src/Standards/Squiz/Sniffs/ControlStructures/ControlSignatureSniff.php index 8a56b23fcb..19e1116e4e 100644 --- a/src/Standards/Squiz/Sniffs/ControlStructures/ControlSignatureSniff.php +++ b/src/Standards/Squiz/Sniffs/ControlStructures/ControlSignatureSniff.php @@ -105,9 +105,14 @@ 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, ]; @@ -146,9 +151,14 @@ 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, ]; From ed9d2b8c46088975d626385680b50624d1d77518 Mon Sep 17 00:00:00 2001 From: Danny van der Sluijs Date: Fri, 25 Aug 2023 09:19:48 +0200 Subject: [PATCH 2/5] style: Correct code style violations --- .../Sniffs/Arrays/ArrayIndentSniff.php | 1 + .../Sniffs/Formatting/SpaceAfterCastSniff.php | 4 +-- .../Sniffs/Formatting/SpaceAfterNotSniff.php | 4 +-- .../Sniffs/Arrays/ArrayDeclarationSniff.php | 26 +++++++++++-------- .../Commenting/DocCommentAlignmentSniff.php | 5 ++-- .../ControlSignatureSniff.php | 4 ++- 6 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/Standards/Generic/Sniffs/Arrays/ArrayIndentSniff.php b/src/Standards/Generic/Sniffs/Arrays/ArrayIndentSniff.php index 0a0a57f787..3285f4a46c 100644 --- a/src/Standards/Generic/Sniffs/Arrays/ArrayIndentSniff.php +++ b/src/Standards/Generic/Sniffs/Arrays/ArrayIndentSniff.php @@ -153,6 +153,7 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array if ($foundIndent === $expectedIndent) { return; } + $pluralizeSpace = 's'; if ($expectedIndent === 1) { $pluralizeSpace = ''; diff --git a/src/Standards/Generic/Sniffs/Formatting/SpaceAfterCastSniff.php b/src/Standards/Generic/Sniffs/Formatting/SpaceAfterCastSniff.php index 0a73b4d16e..aff88d2e35 100644 --- a/src/Standards/Generic/Sniffs/Formatting/SpaceAfterCastSniff.php +++ b/src/Standards/Generic/Sniffs/Formatting/SpaceAfterCastSniff.php @@ -54,8 +54,8 @@ 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 = ''; diff --git a/src/Standards/Generic/Sniffs/Formatting/SpaceAfterNotSniff.php b/src/Standards/Generic/Sniffs/Formatting/SpaceAfterNotSniff.php index 45ce3e7f0c..1d91aaaa35 100644 --- a/src/Standards/Generic/Sniffs/Formatting/SpaceAfterNotSniff.php +++ b/src/Standards/Generic/Sniffs/Formatting/SpaceAfterNotSniff.php @@ -64,8 +64,8 @@ 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 = ''; diff --git a/src/Standards/Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php b/src/Standards/Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php index f93fa5e0e3..f38a5e95b7 100644 --- a/src/Standards/Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php +++ b/src/Standards/Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php @@ -330,14 +330,15 @@ 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); + $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 = [ + + $error = 'Closing parenthesis not aligned correctly; expected %s space%s but found %s'; + $data = [ $expected, $pluralizeSpace, $found, @@ -679,12 +680,13 @@ 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 space%s but found %s'; $data = [ @@ -793,14 +795,15 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array $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'])); + $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 = [ + + $error = 'Array double arrow not aligned correctly; expected %s space%s but found %s'; + $data = [ $expected, $pluralizeSpace, $found, @@ -816,7 +819,7 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array } continue; - } + }//end if $valueStart = ($arrowStart + 3); if ($tokens[$valuePointer]['column'] !== $valueStart) { @@ -825,6 +828,7 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array if ($found < 0) { $found = 'newline'; } + $pluralizeSpace = 's'; if ($expected === 1) { $pluralizeSpace = ''; diff --git a/src/Standards/Squiz/Sniffs/Commenting/DocCommentAlignmentSniff.php b/src/Standards/Squiz/Sniffs/Commenting/DocCommentAlignmentSniff.php index 78934d166e..03a82a32e7 100644 --- a/src/Standards/Squiz/Sniffs/Commenting/DocCommentAlignmentSniff.php +++ b/src/Standards/Squiz/Sniffs/Commenting/DocCommentAlignmentSniff.php @@ -113,9 +113,10 @@ public function process(File $phpcsFile, $stackPtr) if ($tokens[$i]['column'] !== $requiredColumn) { $pluralizeSpace = 's'; - if ($requiredColumn - 1 === 1) { + if (($requiredColumn - 1) === 1) { $pluralizeSpace = ''; } + $error = 'Expected %s space%s before asterisk; %s found'; $data = [ ($requiredColumn - 1), @@ -131,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 19e1116e4e..4893b1eb28 100644 --- a/src/Standards/Squiz/Sniffs/ControlStructures/ControlSignatureSniff.php +++ b/src/Standards/Squiz/Sniffs/ControlStructures/ControlSignatureSniff.php @@ -109,6 +109,7 @@ public function process(File $phpcsFile, $stackPtr) if ($expected === 1) { $pluralizeSpace = ''; } + $error = 'Expected %s space%s after %s keyword; %s found'; $data = [ $expected, @@ -125,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 @@ -155,6 +156,7 @@ public function process(File $phpcsFile, $stackPtr) if ($expected === 1) { $pluralizeSpace = ''; } + $error = 'Expected %s space%s after closing parenthesis; found %s'; $data = [ $expected, From 2d51a632ccb2c0172c10b266a06b825d8b38aafb Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 27 Aug 2023 17:42:24 +0200 Subject: [PATCH 3/5] Generic/ArrayIndent: add test covering the change --- .../Generic/Tests/Arrays/ArrayIndentUnitTest.inc | 16 ++++++++++++++++ .../Tests/Arrays/ArrayIndentUnitTest.inc.fixed | 16 ++++++++++++++++ .../Generic/Tests/Arrays/ArrayIndentUnitTest.php | 3 +++ 3 files changed, 35 insertions(+) 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 4861041f62..d3fd0f68fd 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() From a99c2d56dfa74ee3ec1a3d1cdce72eb78a81e168 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 27 Aug 2023 19:07:09 +0200 Subject: [PATCH 4/5] Squiz/ArrayDeclaration: test for CloseBraceNotAligned --- .../Squiz/Tests/Arrays/ArrayDeclarationUnitTest.1.inc | 6 ++++++ .../Squiz/Tests/Arrays/ArrayDeclarationUnitTest.1.inc.fixed | 6 ++++++ .../Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc | 6 ++++++ .../Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc.fixed | 6 ++++++ .../Squiz/Tests/Arrays/ArrayDeclarationUnitTest.php | 2 ++ 5 files changed, 26 insertions(+) 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 9d917b46b0..60f93f4822 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 []; From 5e03f110d4d8485111943abf76f230ec0a4a3631 Mon Sep 17 00:00:00 2001 From: Danny van der Sluijs Date: Thu, 31 Aug 2023 20:44:27 +0200 Subject: [PATCH 5/5] fix: Correct pluralization of the word space/spaces depending on the spacing value --- .../Generic/Sniffs/Arrays/ArrayIndentSniff.php | 16 ++++++++++++++-- .../Sniffs/Arrays/ArrayDeclarationSniff.php | 16 +++++++++++----- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/Standards/Generic/Sniffs/Arrays/ArrayIndentSniff.php b/src/Standards/Generic/Sniffs/Arrays/ArrayIndentSniff.php index 3285f4a46c..df1874fb2f 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); diff --git a/src/Standards/Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php b/src/Standards/Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php index f38a5e95b7..82d77c853f 100644 --- a/src/Standards/Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php +++ b/src/Standards/Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php @@ -775,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, ]; @@ -791,7 +797,7 @@ 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) {