Skip to content

Commit 03e1866

Browse files
committed
PHP 8.5 | Fix another runtime deprecation notice
This fixes yet another "Using null as an array offset" deprecation notice on PHP 8.5. This one only occurred on PHPCS 4.0, not on 3.x. The tests for this fix have been pulled in 1225 (to safeguard that the issue doesn't exist in 3.x) and merged up into the 4.x branch. Note: as the `if` condition was pretty complex, I've done a small refactor to make the code more readable and understandable.
1 parent 644978d commit 03e1866

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

src/Tokenizers/PHP.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2139,15 +2139,25 @@ protected function tokenize($string)
21392139
continue;
21402140
}
21412141

2142-
if (($tokenType !== T_CALLABLE
2143-
&& isset($lastRelevantNonEmpty) === false)
2144-
|| ($lastRelevantNonEmpty === T_ARRAY
2145-
&& $tokenType === '(')
2146-
|| (isset(Tokens::NAME_TOKENS[$lastRelevantNonEmpty]) === true
2147-
&& ($tokenType === T_DOUBLE_COLON
2148-
|| $tokenType === '('
2149-
|| $tokenType === ':'))
2150-
) {
2142+
$isInlineThen = false;
2143+
if (isset($lastRelevantNonEmpty) === false && $tokenType !== T_CALLABLE) {
2144+
// Can be anything, but is definitely not a type declaration.
2145+
$isInlineThen = true;
2146+
} else if (isset($lastRelevantNonEmpty) === true) {
2147+
if ($lastRelevantNonEmpty === T_ARRAY && $tokenType === '(') {
2148+
// Array declaration in ternary then.
2149+
$isInlineThen = true;
2150+
} else if (isset(Tokens::NAME_TOKENS[$lastRelevantNonEmpty]) === true
2151+
&& ($tokenType === T_DOUBLE_COLON
2152+
|| $tokenType === '('
2153+
|| $tokenType === ':')
2154+
) {
2155+
// Constant access, function call, static class member access in ternary then.
2156+
$isInlineThen = true;
2157+
}
2158+
}
2159+
2160+
if ($isInlineThen === true) {
21512161
if (PHP_CODESNIFFER_VERBOSITY > 1) {
21522162
StatusWriter::write("* token $stackPtr changed from ? to T_INLINE_THEN", 2);
21532163
}

0 commit comments

Comments
 (0)