Skip to content

Commit

Permalink
Merge pull request #335 from fredden/parse-error-detection/PSR12.File…
Browse files Browse the repository at this point in the history
…s.DeclareStatement

`PSR12.Files.DeclareStatement` - bow out when parse error detected
  • Loading branch information
jrfnl authored Feb 10, 2024
2 parents 328c528 + 7f5c88a commit 7240235
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 26 deletions.
6 changes: 6 additions & 0 deletions src/Standards/PSR12/Sniffs/Files/DeclareStatementSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ public function process(File $phpcsFile, $stackPtr)

// There should be no space between equal sign and directive value.
$value = $phpcsFile->findNext(T_WHITESPACE, ($equals + 1), null, true);

if ($value === false) {
// Live coding / parse error.
return;
}

if ($equals !== false) {
if ($tokens[($equals + 1)]['type'] !== 'T_LNUMBER') {
$error = 'Expected no space between equal sign and the directive value in a declare statement';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php
// Safeguard sniff handles parse error/live coding correctly.
declare(strict_types=
59 changes: 33 additions & 26 deletions src/Standards/PSR12/Tests/Files/DeclareStatementUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,42 @@ final class DeclareStatementUnitTest extends AbstractSniffUnitTest
* The key of the array should represent the line number and the value
* should represent the number of errors that should occur on that line.
*
* @param string $testFile The name of the file being tested.
*
* @return array<int, int>
*/
public function getErrorList()
public function getErrorList($testFile='')
{
return [
2 => 1,
3 => 1,
4 => 1,
5 => 2,
6 => 1,
7 => 1,
9 => 2,
10 => 1,
11 => 3,
12 => 2,
13 => 1,
14 => 2,
16 => 3,
19 => 3,
22 => 1,
24 => 1,
26 => 3,
28 => 3,
34 => 2,
43 => 1,
46 => 1,
47 => 1,
49 => 1,
];
switch ($testFile) {
case 'DeclareStatementUnitTest.1.inc':
return [
2 => 1,
3 => 1,
4 => 1,
5 => 2,
6 => 1,
7 => 1,
9 => 2,
10 => 1,
11 => 3,
12 => 2,
13 => 1,
14 => 2,
16 => 3,
19 => 3,
22 => 1,
24 => 1,
26 => 3,
28 => 3,
34 => 2,
43 => 1,
46 => 1,
47 => 1,
49 => 1,
];
default:
return [];
}//end switch

}//end getErrorList()

Expand Down

0 comments on commit 7240235

Please sign in to comment.