Skip to content

Commit acf2a54

Browse files
committed
PHP 8.5 | Tokenizer/PHP: fix "Using null as an array offset" deprecation
If an attribute is unclosed (missing the closing `]` bracket), the `PHP::findCloser()` returns `null`, which will lead to the PHP 8.5 deprecation notice. This can only occur during live coding or when a file has a parse error, but PHPCS should handle that situation gracefully. Fixed now. This change is already covered via the existing tests. Ref: https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_using_values_null_as_an_array_offset_and_when_calling_array_key_exists
1 parent c019c75 commit acf2a54

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/Tokenizers/PHP.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,16 +1468,18 @@ protected function tokenize($string)
14681468
$newToken['type'] = 'T_ATTRIBUTE';
14691469
$newToken['content'] = '#[';
14701470
$finalTokens[$newStackPtr] = $newToken;
1471+
$newStackPtr++;
14711472

1472-
$tokens[$bracketCloser] = [];
1473-
$tokens[$bracketCloser][0] = T_ATTRIBUTE_END;
1474-
$tokens[$bracketCloser][1] = ']';
1473+
if ($bracketCloser !== null) {
1474+
$tokens[$bracketCloser] = [];
1475+
$tokens[$bracketCloser][0] = T_ATTRIBUTE_END;
1476+
$tokens[$bracketCloser][1] = ']';
14751477

1476-
if (PHP_CODESNIFFER_VERBOSITY > 1) {
1477-
echo "\t\t* token $bracketCloser changed from T_CLOSE_SQUARE_BRACKET to T_ATTRIBUTE_END".PHP_EOL;
1478+
if (PHP_CODESNIFFER_VERBOSITY > 1) {
1479+
echo "\t\t* token $bracketCloser changed from T_CLOSE_SQUARE_BRACKET to T_ATTRIBUTE_END".PHP_EOL;
1480+
}
14781481
}
14791482

1480-
$newStackPtr++;
14811483
continue;
14821484
}//end if
14831485

0 commit comments

Comments
 (0)