Skip to content

Commit 7240235

Browse files
authored
Merge pull request #335 from fredden/parse-error-detection/PSR12.Files.DeclareStatement
`PSR12.Files.DeclareStatement` - bow out when parse error detected
2 parents 328c528 + 7f5c88a commit 7240235

5 files changed

+42
-26
lines changed

src/Standards/PSR12/Sniffs/Files/DeclareStatementSniff.php

+6
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,12 @@ public function process(File $phpcsFile, $stackPtr)
110110

111111
// There should be no space between equal sign and directive value.
112112
$value = $phpcsFile->findNext(T_WHITESPACE, ($equals + 1), null, true);
113+
114+
if ($value === false) {
115+
// Live coding / parse error.
116+
return;
117+
}
118+
113119
if ($equals !== false) {
114120
if ($tokens[($equals + 1)]['type'] !== 'T_LNUMBER') {
115121
$error = 'Expected no space between equal sign and the directive value in a declare statement';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
// Safeguard sniff handles parse error/live coding correctly.
3+
declare(strict_types=

src/Standards/PSR12/Tests/Files/DeclareStatementUnitTest.php

+33-26
Original file line numberDiff line numberDiff line change
@@ -26,35 +26,42 @@ final class DeclareStatementUnitTest extends AbstractSniffUnitTest
2626
* The key of the array should represent the line number and the value
2727
* should represent the number of errors that should occur on that line.
2828
*
29+
* @param string $testFile The name of the file being tested.
30+
*
2931
* @return array<int, int>
3032
*/
31-
public function getErrorList()
33+
public function getErrorList($testFile='')
3234
{
33-
return [
34-
2 => 1,
35-
3 => 1,
36-
4 => 1,
37-
5 => 2,
38-
6 => 1,
39-
7 => 1,
40-
9 => 2,
41-
10 => 1,
42-
11 => 3,
43-
12 => 2,
44-
13 => 1,
45-
14 => 2,
46-
16 => 3,
47-
19 => 3,
48-
22 => 1,
49-
24 => 1,
50-
26 => 3,
51-
28 => 3,
52-
34 => 2,
53-
43 => 1,
54-
46 => 1,
55-
47 => 1,
56-
49 => 1,
57-
];
35+
switch ($testFile) {
36+
case 'DeclareStatementUnitTest.1.inc':
37+
return [
38+
2 => 1,
39+
3 => 1,
40+
4 => 1,
41+
5 => 2,
42+
6 => 1,
43+
7 => 1,
44+
9 => 2,
45+
10 => 1,
46+
11 => 3,
47+
12 => 2,
48+
13 => 1,
49+
14 => 2,
50+
16 => 3,
51+
19 => 3,
52+
22 => 1,
53+
24 => 1,
54+
26 => 3,
55+
28 => 3,
56+
34 => 2,
57+
43 => 1,
58+
46 => 1,
59+
47 => 1,
60+
49 => 1,
61+
];
62+
default:
63+
return [];
64+
}//end switch
5865

5966
}//end getErrorList()
6067

0 commit comments

Comments
 (0)