Skip to content

Commit d785815

Browse files
jrfnlDannyvdSluijs
andcommitted
Squiz/ArrayDeclaration: fix grammar for various error messages
Correct pluralization of the word space/spaces depending on the spacing value. Includes adding tests for the `CloseBraceNotAligned` error code which would use the "1 space" phrasing. Co-authored-by: Danny van der Sluijs <danny.van.der.sluijs@infi.nl>
1 parent 4cf6bad commit d785815

File tree

6 files changed

+74
-18
lines changed

6 files changed

+74
-18
lines changed

src/Standards/Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -330,11 +330,17 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
330330
}
331331
} else if ($tokens[$arrayEnd]['column'] !== $keywordStart) {
332332
// Check the closing bracket is lined up under the "a" in array.
333-
$expected = ($keywordStart - 1);
334-
$found = ($tokens[$arrayEnd]['column'] - 1);
335-
$error = 'Closing parenthesis not aligned correctly; expected %s space(s) but found %s';
336-
$data = [
333+
$expected = ($keywordStart - 1);
334+
$found = ($tokens[$arrayEnd]['column'] - 1);
335+
$pluralizeSpace = 's';
336+
if ($expected === 1) {
337+
$pluralizeSpace = '';
338+
}
339+
340+
$error = 'Closing parenthesis not aligned correctly; expected %s space%s but found %s';
341+
$data = [
337342
$expected,
343+
$pluralizeSpace,
338344
$found,
339345
];
340346

@@ -674,12 +680,18 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
674680
} else if ($previousIsWhitespace === true) {
675681
$expected = $keywordStart;
676682

677-
$first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $valuePointer, true);
678-
$found = ($tokens[$first]['column'] - 1);
683+
$first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $valuePointer, true);
684+
$found = ($tokens[$first]['column'] - 1);
685+
$pluralizeSpace = 's';
686+
if ($expected === 1) {
687+
$pluralizeSpace = '';
688+
}
689+
679690
if ($found !== $expected) {
680-
$error = 'Array value not aligned correctly; expected %s spaces but found %s';
691+
$error = 'Array value not aligned correctly; expected %s space%s but found %s';
681692
$data = [
682693
$expected,
694+
$pluralizeSpace,
683695
$found,
684696
];
685697

@@ -763,11 +775,17 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
763775
}
764776

765777
if ($tokens[$indexPointer]['column'] !== $indicesStart && ($indexPointer - 1) !== $arrayStart) {
766-
$expected = ($indicesStart - 1);
767-
$found = ($tokens[$indexPointer]['column'] - 1);
768-
$error = 'Array key not aligned correctly; expected %s spaces but found %s';
769-
$data = [
778+
$expected = ($indicesStart - 1);
779+
$found = ($tokens[$indexPointer]['column'] - 1);
780+
$pluralizeSpace = 's';
781+
if ($expected === 1) {
782+
$pluralizeSpace = '';
783+
}
784+
785+
$error = 'Array key not aligned correctly; expected %s space%s but found %s';
786+
$data = [
770787
$expected,
788+
$pluralizeSpace,
771789
$found,
772790
];
773791

@@ -779,15 +797,21 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
779797
$phpcsFile->fixer->replaceToken(($indexPointer - 1), str_repeat(' ', $expected));
780798
}
781799
}
782-
}
800+
}//end if
783801

784802
$arrowStart = ($tokens[$indexPointer]['column'] + $maxLength + 1);
785803
if ($tokens[$index['arrow']]['column'] !== $arrowStart) {
786-
$expected = ($arrowStart - ($index['index_length'] + $tokens[$indexPointer]['column']));
787-
$found = ($tokens[$index['arrow']]['column'] - ($index['index_length'] + $tokens[$indexPointer]['column']));
788-
$error = 'Array double arrow not aligned correctly; expected %s space(s) but found %s';
789-
$data = [
804+
$expected = ($arrowStart - ($index['index_length'] + $tokens[$indexPointer]['column']));
805+
$found = ($tokens[$index['arrow']]['column'] - ($index['index_length'] + $tokens[$indexPointer]['column']));
806+
$pluralizeSpace = 's';
807+
if ($expected === 1) {
808+
$pluralizeSpace = '';
809+
}
810+
811+
$error = 'Array double arrow not aligned correctly; expected %s space%s but found %s';
812+
$data = [
790813
$expected,
814+
$pluralizeSpace,
791815
$found,
792816
];
793817

@@ -801,7 +825,7 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
801825
}
802826

803827
continue;
804-
}
828+
}//end if
805829

806830
$valueStart = ($arrowStart + 3);
807831
if ($tokens[$valuePointer]['column'] !== $valueStart) {
@@ -811,9 +835,15 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
811835
$found = 'newline';
812836
}
813837

814-
$error = 'Array value not aligned correctly; expected %s space(s) but found %s';
838+
$pluralizeSpace = 's';
839+
if ($expected === 1) {
840+
$pluralizeSpace = '';
841+
}
842+
843+
$error = 'Array value not aligned correctly; expected %s space%s but found %s';
815844
$data = [
816845
$expected,
846+
$pluralizeSpace,
817847
$found,
818848
];
819849

src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.1.inc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,3 +529,9 @@ $x = array(
529529
'bar',
530530
'baz' => 'bar', // KeySpecified (based on first entry).
531531
);
532+
533+
$x =
534+
array(
535+
'a',
536+
'b',
537+
);

src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.1.inc.fixed

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,3 +565,9 @@ $x = array(
565565
'bar',
566566
'baz' => 'bar', // KeySpecified (based on first entry).
567567
);
568+
569+
$x =
570+
array(
571+
'a',
572+
'b',
573+
);

src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,3 +518,9 @@ $x = [
518518
'bar',
519519
'baz' => 'bar', // KeySpecified (based on first entry).
520520
];
521+
522+
$x =
523+
[
524+
'a',
525+
'b',
526+
];

src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc.fixed

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,3 +552,9 @@ $x = [
552552
'bar',
553553
'baz' => 'bar', // KeySpecified (based on first entry).
554554
];
555+
556+
$x =
557+
[
558+
'a',
559+
'b',
560+
];

src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ public function getErrorList($testFile='')
128128
516 => 1,
129129
523 => 1,
130130
530 => 1,
131+
537 => 1,
131132
];
132133
case 'ArrayDeclarationUnitTest.2.inc':
133134
return [
@@ -218,6 +219,7 @@ public function getErrorList($testFile='')
218219
505 => 1,
219220
512 => 1,
220221
519 => 1,
222+
526 => 1,
221223
];
222224
default:
223225
return [];

0 commit comments

Comments
 (0)