Skip to content

Commit d0d3449

Browse files
committed
Report all errors in one go
1 parent 3893034 commit d0d3449

File tree

2 files changed

+180
-35
lines changed

2 files changed

+180
-35
lines changed

src/Config.php

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1655,34 +1655,49 @@ public function printConfigData($data)
16551655
*/
16561656
private function validateSniffCodes($sniffs, $argument)
16571657
{
1658+
$error = '';
1659+
$suggestions = [];
1660+
16581661
foreach ($sniffs as $sniff) {
16591662
$partCount = substr_count($sniff, '.');
16601663
if ($partCount === 2) {
16611664
// Correct number of parts.
1665+
$suggestions[] = $sniff;
16621666
continue;
16631667
}
16641668

1665-
$error = 'ERROR: The specified sniff code "'.$sniff.'" is invalid'.PHP_EOL.PHP_EOL;
1669+
$error .= PHP_EOL.'ERROR: The specified sniff code "'.$sniff.'" is invalid'.PHP_EOL;
16661670

1667-
if ($partCount === 0) {
1668-
$error .= 'This appears to be a Standard code, but the '.$argument.' option only supports sniff codes.'.PHP_EOL;
1671+
if ($sniff === '') {
1672+
$error .= 'Empty string detected. Perhaps there is a stray comma somewhere.'.PHP_EOL;
1673+
} else if ($partCount === 0) {
1674+
$error .= 'This appears to be a Standard code.'.PHP_EOL;
16691675
} else if ($partCount === 1) {
1670-
$error .= 'This appears to be a Category code, but the '.$argument.' option only supports sniff codes.'.PHP_EOL;
1676+
$error .= 'This appears to be a Category code.'.PHP_EOL;
16711677
} else if ($partCount === 3) {
1672-
$error .= 'This appears to be a Message code, but the '.$argument.' option only supports sniff codes.'.PHP_EOL;
1678+
$error .= 'This appears to be a Message code.'.PHP_EOL;
1679+
} else {
1680+
$error .= 'Too many dots.'.PHP_EOL;
1681+
}
1682+
1683+
if ($partCount > 2) {
1684+
$parts = explode('.', $sniff, 4);
1685+
$suggestions[] = $parts[0].'.'.$parts[1].'.'.$parts[2];
16731686
}
1687+
}//end foreach
16741688

1689+
if ($error !== '') {
1690+
$error .= PHP_EOL.'The --'.$argument.' option only supports sniff codes. ';
16751691
$error .= 'Sniff codes are in the form "Standard.Category.Sniff"'.PHP_EOL.PHP_EOL;
16761692

1677-
if ($partCount > 2) {
1678-
$parts = explode('.', $sniff, 4);
1679-
$error .= 'Perhaps try "'.$parts[0].'.'.$parts[1].'.'.$parts[2].'" instead.'.PHP_EOL.PHP_EOL;
1693+
if ($suggestions !== []) {
1694+
$suggestions = array_unique($suggestions);
1695+
$error .= 'Perhaps try --'.$argument.'="'.implode(',', $suggestions).'" instead.'.PHP_EOL;
16801696
}
16811697

16821698
$error .= $this->printShortUsage(true);
1683-
1684-
throw new DeepExitException($error, 3);
1685-
}//end foreach
1699+
throw new DeepExitException(ltrim($error), 3);
1700+
}
16861701

16871702
}//end validateSniffCodes()
16881703

tests/Core/Config/SniffsExcludeArgsTest.php

Lines changed: 154 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,35 @@ final class SniffsExcludeArgsTest extends TestCase
2323
/**
2424
* Ensure that the expected error message is returned for invalid arguments.
2525
*
26-
* @param string $argument 'sniffs' or 'exclude'.
27-
* @param string $value List of sniffs to include / exclude.
28-
* @param string $message Expected error message text.
26+
* @param string $argument 'sniffs' or 'exclude'.
27+
* @param string $value List of sniffs to include / exclude.
28+
* @param array<string, string> $errors Sniff code and associated help text.
29+
* @param string|null $suggestion Help text shown to end user with correct syntax for argument.
2930
*
3031
* @return void
3132
* @dataProvider dataInvalidSniffs
3233
*/
33-
public function testInvalid($argument, $value, $message)
34+
public function testInvalid($argument, $value, $errors, $suggestion)
3435
{
3536
$exception = 'PHP_CodeSniffer\Exceptions\DeepExitException';
37+
$message = '';
38+
39+
foreach ($errors as $error) {
40+
list($sniffCode, $helpText) = $error;
41+
$message .= "ERROR: The specified sniff code \"$sniffCode\" is invalid".PHP_EOL;
42+
$message .= $helpText.PHP_EOL;
43+
$message .= PHP_EOL;
44+
}
45+
46+
$message .= "The --$argument option only supports sniff codes. Sniff codes are in the form \"Standard.Category.Sniff\"".PHP_EOL;
47+
$message .= PHP_EOL;
48+
49+
if ($suggestion !== null) {
50+
$message .= "Perhaps try --$argument=\"$suggestion\" instead.".PHP_EOL;
51+
}
52+
53+
$message .= 'Run "phpcs --help" for usage information'.PHP_EOL;
54+
$message .= PHP_EOL;
3655

3756
if (method_exists($this, 'expectException') === true) {
3857
// PHPUnit 5+.
@@ -62,47 +81,158 @@ public static function dataInvalidSniffs()
6281
];
6382
$data = [];
6483

65-
$messageTemplate = 'ERROR: The specified sniff code "%s" is invalid'.PHP_EOL.PHP_EOL;
84+
$messageTemplate = 'ERROR: The specified sniff code "%s" is invalid'.PHP_EOL;
6685

6786
foreach ($arguments as $argument) {
6887
// An empty string is not a valid sniff.
6988
$data[$argument.'; empty string'] = [
70-
'argument' => $argument,
71-
'value' => '',
72-
'message' => sprintf($messageTemplate, ''),
89+
'argument' => $argument,
90+
'value' => '',
91+
'errors' => [
92+
[
93+
'',
94+
'Empty string detected. Perhaps there is a stray comma somewhere.',
95+
],
96+
],
97+
'suggestion' => null,
7398
];
7499

75100
// A standard is not a valid sniff.
76101
$data[$argument.'; standard'] = [
77-
'argument' => $argument,
78-
'value' => 'Standard',
79-
'message' => sprintf($messageTemplate, 'Standard'),
102+
'argument' => $argument,
103+
'value' => 'Standard',
104+
'errors' => [
105+
[
106+
'Standard',
107+
'This appears to be a Standard code.',
108+
],
109+
],
110+
'suggestion' => null,
80111
];
81112

82113
// A category is not a valid sniff.
83114
$data[$argument.'; category'] = [
84-
'argument' => $argument,
85-
'value' => 'Standard.Category',
86-
'message' => sprintf($messageTemplate, 'Standard.Category'),
115+
'argument' => $argument,
116+
'value' => 'Standard.Category',
117+
'errors' => [
118+
[
119+
'Standard.Category',
120+
'This appears to be a Category code.',
121+
],
122+
],
123+
'suggestion' => null,
87124
];
88125

89126
// An error-code is not a valid sniff.
90127
$data[$argument.'; error-code'] = [
91-
'argument' => $argument,
92-
'value' => 'Standard.Category',
93-
'message' => sprintf($messageTemplate, 'Standard.Category'),
128+
'argument' => $argument,
129+
'value' => 'Standard.Category.Sniff.Code',
130+
'errors' => [
131+
[
132+
'Standard.Category.Sniff.Code',
133+
'This appears to be a Message code.',
134+
],
135+
],
136+
'suggestion' => 'Standard.Category.Sniff',
94137
];
95138

96-
// Only the first error is reported.
139+
// All errors are reported in one go.
97140
$data[$argument.'; two errors'] = [
98-
'argument' => $argument,
99-
'value' => 'StandardOne,StandardTwo',
100-
'message' => sprintf($messageTemplate, 'StandardOne'),
141+
'argument' => $argument,
142+
'value' => 'StandardOne,StandardTwo',
143+
'errors' => [
144+
[
145+
'StandardOne',
146+
'This appears to be a Standard code.',
147+
],
148+
[
149+
'StandardTwo',
150+
'This appears to be a Standard code.',
151+
],
152+
],
153+
'suggestion' => null,
101154
];
155+
156+
// Order of valid/invalid does not impact error reporting.
102157
$data[$argument.'; valid followed by invalid'] = [
103-
'argument' => $argument,
104-
'value' => 'StandardOne.Category.Sniff,StandardTwo.Category',
105-
'message' => sprintf($messageTemplate, 'StandardTwo.Category'),
158+
'argument' => $argument,
159+
'value' => 'StandardOne.Category.Sniff,StandardTwo.Category',
160+
'errors' => [
161+
[
162+
'StandardTwo.Category',
163+
'This appears to be a Category code.',
164+
],
165+
],
166+
'suggestion' => 'StandardOne.Category.Sniff',
167+
];
168+
$data[$argument.'; invalid followed by valid'] = [
169+
'argument' => $argument,
170+
'value' => 'StandardOne.Category,StandardTwo.Category.Sniff',
171+
'errors' => [
172+
[
173+
'StandardOne.Category',
174+
'This appears to be a Category code.',
175+
],
176+
],
177+
'suggestion' => 'StandardTwo.Category.Sniff',
178+
];
179+
180+
// Rogue commas get reported.
181+
$data[$argument.'; one comma alone'] = [
182+
'argument' => $argument,
183+
'value' => ',',
184+
'errors' => [
185+
[
186+
'',
187+
'Empty string detected. Perhaps there is a stray comma somewhere.',
188+
],
189+
[
190+
'',
191+
'Empty string detected. Perhaps there is a stray comma somewhere.',
192+
],
193+
],
194+
'suggestion' => null,
195+
];
196+
$data[$argument.'; two commas alone'] = [
197+
'argument' => $argument,
198+
'value' => ',,',
199+
'errors' => [
200+
[
201+
'',
202+
'Empty string detected. Perhaps there is a stray comma somewhere.',
203+
],
204+
[
205+
'',
206+
'Empty string detected. Perhaps there is a stray comma somewhere.',
207+
],
208+
[
209+
'',
210+
'Empty string detected. Perhaps there is a stray comma somewhere.',
211+
],
212+
],
213+
'suggestion' => null,
214+
];
215+
$data[$argument.'; trailing comma'] = [
216+
'argument' => $argument,
217+
'value' => 'Standard.Category.Sniff,',
218+
'errors' => [
219+
[
220+
'',
221+
'Empty string detected. Perhaps there is a stray comma somewhere.',
222+
],
223+
],
224+
'suggestion' => 'Standard.Category.Sniff',
225+
];
226+
$data[$argument.'; double comma between sniffs'] = [
227+
'argument' => $argument,
228+
'value' => 'StandardOne.Category.Sniff,,StandardTwo.Category.Sniff',
229+
'errors' => [
230+
[
231+
'',
232+
'Empty string detected. Perhaps there is a stray comma somewhere.',
233+
],
234+
],
235+
'suggestion' => 'StandardOne.Category.Sniff,StandardTwo.Category.Sniff',
106236
];
107237
}//end foreach
108238

0 commit comments

Comments
 (0)