Skip to content

Commit 295b113

Browse files
rodrigoprimojrfnl
authored andcommitted
Generic/CallTimePassByReference: fix non-problematic bug
This commit fixes a non-problematic bug when there are only empty tokens after a variable or string token and nothing else. Before this commit, the code would search for the next non-empty token after a variable or string token. It would assume that there would always be such a token and it would check if this token is a open parenthesis token. It is possible that there are no tokens or only empty tokens after the variable or string token when live coding. This assumption would not cause any problems because when checking if `$tokens[$openBracket]['code'] !== T_OPEN_PARENTHESIS`, `$openBracket` would be `false`, and then PHP would check for the first element in the $tokens array. The first token can never be a T_OPEN_PARENTHESIS and so the code would bail early as expected. This commit adds a `$openBracket === false` check to bail early as well to make it clear that `$openBracket` can be false.
1 parent a7ece48 commit 295b113

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

src/Standards/Generic/Sniffs/Functions/CallTimePassByReferenceSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function process(File $phpcsFile, $stackPtr)
6969
true
7070
);
7171

72-
if ($tokens[$openBracket]['code'] !== T_OPEN_PARENTHESIS) {
72+
if ($openBracket === false || $tokens[$openBracket]['code'] !== T_OPEN_PARENTHESIS) {
7373
return;
7474
}
7575

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.
4+
// This should be the only test in this file.
5+
// Testing that the sniff is *not* triggered when there are only empty tokens after a variable and nothing else.
6+
7+
$var

0 commit comments

Comments
 (0)