Skip to content

Commit 72ea7b3

Browse files
authored
Merge pull request #128 from DannyvdSluijs/ImproveMessageReadabilityForMissingSpaces
Correct pluralization of the word space/spaces depending on the spacing value
2 parents f83010f + 5632363 commit 72ea7b3

17 files changed

+252
-36
lines changed

src/Standards/Generic/Sniffs/Arrays/ArrayIndentSniff.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,15 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
7676
// check indent levels because it's not valid. But we don't enforce exactly
7777
// how far indented it should be.
7878
if ($startIndent < $baseIndent) {
79-
$error = 'Array open brace not indented correctly; expected at least %s spaces but found %s';
79+
$pluralizeSpace = 's';
80+
if ($baseIndent === 1) {
81+
$pluralizeSpace = '';
82+
}
83+
84+
$error = 'Array open brace not indented correctly; expected at least %s space%s but found %s';
8085
$data = [
8186
$baseIndent,
87+
$pluralizeSpace,
8288
$startIndent,
8389
];
8490
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'OpenBraceIncorrect', $data);
@@ -117,9 +123,15 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
117123
continue;
118124
}
119125

120-
$error = 'Array key not indented correctly; expected %s spaces but found %s';
126+
$pluralizeSpace = 's';
127+
if ($expectedIndent === 1) {
128+
$pluralizeSpace = '';
129+
}
130+
131+
$error = 'Array key not indented correctly; expected %s space%s but found %s';
121132
$data = [
122133
$expectedIndent,
134+
$pluralizeSpace,
123135
$foundIndent,
124136
];
125137
$fix = $phpcsFile->addFixableError($error, $first, 'KeyIncorrect', $data);
@@ -154,9 +166,15 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
154166
return;
155167
}
156168

157-
$error = 'Array close brace not indented correctly; expected %s spaces but found %s';
169+
$pluralizeSpace = 's';
170+
if ($expectedIndent === 1) {
171+
$pluralizeSpace = '';
172+
}
173+
174+
$error = 'Array close brace not indented correctly; expected %s space%s but found %s';
158175
$data = [
159176
$expectedIndent,
177+
$pluralizeSpace,
160178
$foundIndent,
161179
];
162180
$fix = $phpcsFile->addFixableError($error, $arrayEnd, 'CloseBraceIncorrect', $data);

src/Standards/Generic/Sniffs/Formatting/SpaceAfterCastSniff.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,12 @@ public function register()
5454
*/
5555
public function process(File $phpcsFile, $stackPtr)
5656
{
57-
$tokens = $phpcsFile->getTokens();
58-
$this->spacing = (int) $this->spacing;
57+
$tokens = $phpcsFile->getTokens();
58+
$this->spacing = (int) $this->spacing;
59+
$pluralizeSpace = 's';
60+
if ($this->spacing === 1) {
61+
$pluralizeSpace = '';
62+
}
5963

6064
if ($tokens[$stackPtr]['code'] === T_BINARY_CAST
6165
&& $tokens[$stackPtr]['content'] === 'b'
@@ -83,8 +87,11 @@ public function process(File $phpcsFile, $stackPtr)
8387

8488
$nextNonWhitespace = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
8589
if ($nextNonEmpty !== $nextNonWhitespace) {
86-
$error = 'Expected %s space(s) after cast statement; comment found';
87-
$data = [$this->spacing];
90+
$error = 'Expected %s space%s after cast statement; comment found';
91+
$data = [
92+
$this->spacing,
93+
$pluralizeSpace,
94+
];
8895
$phpcsFile->addError($error, $stackPtr, 'CommentFound', $data);
8996

9097
if ($tokens[($stackPtr + 1)]['code'] === T_WHITESPACE) {
@@ -109,9 +116,10 @@ public function process(File $phpcsFile, $stackPtr)
109116
return;
110117
}
111118

112-
$error = 'Expected %s space(s) after cast statement; %s found';
119+
$error = 'Expected %s space%s after cast statement; %s found';
113120
$data = [
114121
$this->spacing,
122+
$pluralizeSpace,
115123
$found,
116124
];
117125

src/Standards/Generic/Sniffs/Formatting/SpaceAfterNotSniff.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,12 @@ public function register()
6464
*/
6565
public function process(File $phpcsFile, $stackPtr)
6666
{
67-
$tokens = $phpcsFile->getTokens();
68-
$this->spacing = (int) $this->spacing;
67+
$tokens = $phpcsFile->getTokens();
68+
$this->spacing = (int) $this->spacing;
69+
$pluralizeSpace = 's';
70+
if ($this->spacing === 1) {
71+
$pluralizeSpace = '';
72+
}
6973

7074
$nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
7175
if ($nextNonEmpty === false) {
@@ -84,8 +88,11 @@ public function process(File $phpcsFile, $stackPtr)
8488

8589
$nextNonWhitespace = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
8690
if ($nextNonEmpty !== $nextNonWhitespace) {
87-
$error = 'Expected %s space(s) after NOT operator; comment found';
88-
$data = [$this->spacing];
91+
$error = 'Expected %s space%s after NOT operator; comment found';
92+
$data = [
93+
$this->spacing,
94+
$pluralizeSpace,
95+
];
8996
$phpcsFile->addError($error, $stackPtr, 'CommentFound', $data);
9097
return;
9198
}
@@ -101,9 +108,10 @@ public function process(File $phpcsFile, $stackPtr)
101108
return;
102109
}
103110

104-
$error = 'Expected %s space(s) after NOT operator; %s found';
111+
$error = 'Expected %s space%s after NOT operator; %s found';
105112
$data = [
106113
$this->spacing,
114+
$pluralizeSpace,
107115
$found,
108116
];
109117

src/Standards/Generic/Tests/Arrays/ArrayIndentUnitTest.inc

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,41 @@ $array = [
111111
name: $value
112112
),
113113
];
114+
115+
// phpcs:set Generic.Arrays.ArrayIndent indent 1
116+
117+
// Testing pluralization of indent text - open brace indent.
118+
$var =
119+
[
120+
1 => 'one',
121+
];
122+
123+
// Testing pluralization of indent text - array item indent.
124+
$var = [
125+
1 => 'one',
126+
2 => 'two',
127+
/* three */ 3 => 'three',
128+
];
129+
130+
// Testing pluralization of indent text - close brace indent.
131+
$var = [
132+
1 => 'one',
133+
];
134+
135+
// phpcs:set Generic.Arrays.ArrayIndent indent 0
136+
137+
// No test for open brace indent as that is _minimum_ and any actual value will be 0 or more, so with indent 0, this will never yield an error.
138+
139+
// Testing pluralization of indent text - array item indent.
140+
$var = [
141+
1 => 'one',
142+
2 => 'two',
143+
/* three */ 3 => 'three',
144+
];
145+
146+
// Testing pluralization of indent text - close brace indent.
147+
$var = [
148+
1 => 'one',
149+
];
150+
151+
// phpcs:set Generic.Arrays.ArrayIndent indent 4

src/Standards/Generic/Tests/Arrays/ArrayIndentUnitTest.inc.fixed

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,41 @@ $array = [
112112
name: $value
113113
),
114114
];
115+
116+
// phpcs:set Generic.Arrays.ArrayIndent indent 1
117+
118+
// Testing pluralization of indent text - open brace indent.
119+
$var =
120+
[
121+
1 => 'one',
122+
];
123+
124+
// Testing pluralization of indent text - array item indent.
125+
$var = [
126+
1 => 'one',
127+
2 => 'two',
128+
/* three */ 3 => 'three',
129+
];
130+
131+
// Testing pluralization of indent text - close brace indent.
132+
$var = [
133+
1 => 'one',
134+
];
135+
136+
// phpcs:set Generic.Arrays.ArrayIndent indent 0
137+
138+
// No test for open brace indent as that is _minimum_ and any actual value will be 0 or more, so with indent 0, this will never yield an error.
139+
140+
// Testing pluralization of indent text - array item indent.
141+
$var = [
142+
1 => 'one',
143+
2 => 'two',
144+
/* three */ 3 => 'three',
145+
];
146+
147+
// Testing pluralization of indent text - close brace indent.
148+
$var = [
149+
1 => 'one',
150+
];
151+
152+
// phpcs:set Generic.Arrays.ArrayIndent indent 4

src/Standards/Generic/Tests/Arrays/ArrayIndentUnitTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ public function getErrorList()
4545
88 => 1,
4646
98 => 1,
4747
110 => 1,
48+
119 => 1,
49+
126 => 1,
50+
127 => 1,
51+
133 => 1,
52+
141 => 1,
53+
142 => 1,
54+
143 => 1,
55+
149 => 1,
4856
];
4957

5058
}//end getErrorList()

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/Sniffs/Commenting/DocCommentAlignmentSniff.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,15 @@ public function process(File $phpcsFile, $stackPtr)
112112
}
113113

114114
if ($tokens[$i]['column'] !== $requiredColumn) {
115-
$error = 'Expected %s space(s) before asterisk; %s found';
115+
$pluralizeSpace = 's';
116+
if (($requiredColumn - 1) === 1) {
117+
$pluralizeSpace = '';
118+
}
119+
120+
$error = 'Expected %s space%s before asterisk; %s found';
116121
$data = [
117122
($requiredColumn - 1),
123+
$pluralizeSpace,
118124
($tokens[$i]['column'] - 1),
119125
];
120126
$fix = $phpcsFile->addFixableError($error, $i, 'SpaceBeforeStar', $data);
@@ -126,7 +132,7 @@ public function process(File $phpcsFile, $stackPtr)
126132
$phpcsFile->fixer->replaceToken(($i - 1), $padding);
127133
}
128134
}
129-
}
135+
}//end if
130136

131137
if ($tokens[$i]['code'] !== T_DOC_COMMENT_STAR) {
132138
continue;

0 commit comments

Comments
 (0)