Skip to content

Commit efea5f3

Browse files
committed
Squiz/ScopeKeywordSpacing: add more defensive coding
When the sniff would encounter a parse error/be run during live coding, the sniff could trigger an `Undefined array key` notice for the use of `$stackPtr + 2`. Fixed now by bowing out early if the keyword is found at the end of a file. Includes additional tests.
1 parent 080cc38 commit efea5f3

File tree

5 files changed

+24
-0
lines changed

5 files changed

+24
-0
lines changed

src/Standards/Squiz/Sniffs/WhiteSpace/ScopeKeywordSpacingSniff.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ public function process(File $phpcsFile, $stackPtr)
127127

128128
if ($tokens[($stackPtr + 1)]['code'] !== T_WHITESPACE) {
129129
$spacing = 0;
130+
} else if (isset($tokens[($stackPtr + 2)]) === false) {
131+
// Parse error/live coding. Bow out.
132+
return;
130133
} else {
131134
if ($tokens[($stackPtr + 2)]['line'] !== $tokens[$stackPtr]['line']) {
132135
$spacing = 'newline';
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
// Intentional parse error.
4+
// Testing that the sniff does not throw an "Undefined array key" notice during live coding.
5+
// This must be the only test in this file.
6+
readonly
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
// Intentional parse error.
4+
// Testing that the sniff still adds a space when there is only a comment after the keyword during live coding.
5+
// This must be the only test in this file.
6+
readonly/*comment*/
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
// Intentional parse error.
4+
// Testing that the sniff still adds a space when there is only a comment after the keyword during live coding.
5+
// This must be the only test in this file.
6+
readonly /*comment*/

src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ public function getErrorList($testFile='')
5858
140 => 3,
5959
];
6060

61+
case 'ScopeKeywordSpacingUnitTest.3.inc':
62+
return [6 => 1];
63+
6164
default:
6265
return [];
6366
}//end switch

0 commit comments

Comments
 (0)