Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PSR12.Files.DeclareStatement - bow out when parse error detected #335

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading