Skip to content

Commit e5ce70a

Browse files
committed
PEAR/Functions/FunctionDeclaration: add extra defensive coding
When the sniff is run during live coding, the sniff could encounter a situation where a function declaration does not have a scope opener, not a function body. In that case, the "SpaceBeforeSemicolon" check would try to find a semi-colon, presuming the function is an abstract method or interface method, but would not find one, which would result in the `if ($tokens[($end - 1)]['content'] === $phpcsFile->eolChar)` condition throwing a _"Undefined array key -1"_ notice. Fixed now. Includes test safeguarding the fix. Note: this fix will, by extension, fix this same error for the `PSR2.Methods.FunctionCallSignature` sniff and the `Squiz.Functions.MultiLineFunctionDeclaration` sniff. :point_right: this commit will be easier to review while ignoring whitespace changes.
1 parent a50b5fb commit e5ce70a

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

src/Standards/PEAR/Sniffs/Functions/FunctionDeclarationSniff.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -128,23 +128,25 @@ public function process(File $phpcsFile, $stackPtr)
128128
// Must be no space before semicolon in abstract/interface methods.
129129
if ($methodProps['has_body'] === false) {
130130
$end = $phpcsFile->findNext(T_SEMICOLON, $closeBracket);
131-
if ($tokens[($end - 1)]['content'] === $phpcsFile->eolChar) {
132-
$spaces = 'newline';
133-
} else if ($tokens[($end - 1)]['code'] === T_WHITESPACE) {
134-
$spaces = $tokens[($end - 1)]['length'];
135-
} else {
136-
$spaces = 0;
137-
}
131+
if ($end !== false) {
132+
if ($tokens[($end - 1)]['content'] === $phpcsFile->eolChar) {
133+
$spaces = 'newline';
134+
} else if ($tokens[($end - 1)]['code'] === T_WHITESPACE) {
135+
$spaces = $tokens[($end - 1)]['length'];
136+
} else {
137+
$spaces = 0;
138+
}
138139

139-
if ($spaces !== 0) {
140-
$error = 'Expected 0 spaces before semicolon; %s found';
141-
$data = [$spaces];
142-
$fix = $phpcsFile->addFixableError($error, $end, 'SpaceBeforeSemicolon', $data);
143-
if ($fix === true) {
144-
$phpcsFile->fixer->replaceToken(($end - 1), '');
140+
if ($spaces !== 0) {
141+
$error = 'Expected 0 spaces before semicolon; %s found';
142+
$data = [$spaces];
143+
$fix = $phpcsFile->addFixableError($error, $end, 'SpaceBeforeSemicolon', $data);
144+
if ($fix === true) {
145+
$phpcsFile->fixer->replaceToken(($end - 1), '');
146+
}
145147
}
146148
}
147-
}
149+
}//end if
148150
}//end if
149151

150152
// Must be one space before and after USE keyword for closures.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
// Intentional parse error/live coding test.
4+
// This must be the only test in this file.
5+
// Safeguarding that the sniff does not throw a PHP notice for this test.
6+
7+
function liveCoding()

0 commit comments

Comments
 (0)