From c019c75f344589857e57ecc87a30071f7f54e9e9 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 5 Sep 2025 06:02:00 +0200 Subject: [PATCH 1/3] PHP 8.5 | File::getMemberProperties(): fix "Using null as an array offset" deprecation If a variable in the global scope is passed to `File::getMemberProperties()`, the token may not have any "conditions". This would result in `array_keys()` returning an empty array, which will cause `array_pop()` to return `null`, leading to the deprecation notice. Fixed now via some extra defensive coding. 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 --- src/Files/File.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Files/File.php b/src/Files/File.php index 1a63437ea4..42e1a39f9b 100644 --- a/src/Files/File.php +++ b/src/Files/File.php @@ -1876,6 +1876,10 @@ public function getMemberProperties($stackPtr) throw new RuntimeException('$stackPtr must be of type T_VARIABLE'); } + if (empty($this->tokens[$stackPtr]['conditions']) === true) { + throw new RuntimeException('$stackPtr is not a class member var'); + } + $conditions = array_keys($this->tokens[$stackPtr]['conditions']); $ptr = array_pop($conditions); if (isset($this->tokens[$ptr]) === false From acf2a54cf3718e72c75d283cc59b2fb0d7a476dc Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 5 Sep 2025 06:11:45 +0200 Subject: [PATCH 2/3] 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 --- src/Tokenizers/PHP.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Tokenizers/PHP.php b/src/Tokenizers/PHP.php index dea4438575..ecc2964160 100644 --- a/src/Tokenizers/PHP.php +++ b/src/Tokenizers/PHP.php @@ -1468,16 +1468,18 @@ protected function tokenize($string) $newToken['type'] = 'T_ATTRIBUTE'; $newToken['content'] = '#['; $finalTokens[$newStackPtr] = $newToken; + $newStackPtr++; - $tokens[$bracketCloser] = []; - $tokens[$bracketCloser][0] = T_ATTRIBUTE_END; - $tokens[$bracketCloser][1] = ']'; + if ($bracketCloser !== null) { + $tokens[$bracketCloser] = []; + $tokens[$bracketCloser][0] = T_ATTRIBUTE_END; + $tokens[$bracketCloser][1] = ']'; - if (PHP_CODESNIFFER_VERBOSITY > 1) { - echo "\t\t* token $bracketCloser changed from T_CLOSE_SQUARE_BRACKET to T_ATTRIBUTE_END".PHP_EOL; + if (PHP_CODESNIFFER_VERBOSITY > 1) { + echo "\t\t* token $bracketCloser changed from T_CLOSE_SQUARE_BRACKET to T_ATTRIBUTE_END".PHP_EOL; + } } - $newStackPtr++; continue; }//end if From 6b82a86419bb51cdb25dc20d9ff56ea30f43a258 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 5 Sep 2025 06:31:48 +0200 Subject: [PATCH 3/3] PHP 8.5 | Tokenizer/PHP: temporarily silence "Using null as an array offset" deprecation When running the `NullsafeObjectOperatorTest`, the "Using null as an array offset" deprecation gets triggered in the tokenizer layer handling re-tokenization to `T_NULLABLE` and/or `T_INLINE_THEN`. This should only be possible if the below code at the top of the loop would result in `$tokenType` being `null`, which shouldn't be possible.... https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/f1c9394f1724ed45ca42029da330ff5c702af2a9/src/Tokenizers/PHP.php#L2254-L2258 With this conundrum in mind, I'm electing to (temporarily) silence the deprecation notice for now until there is more time to investigate in more depth. Ref: https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_using_values_null_as_an_array_offset_and_when_calling_array_key_exists --- src/Tokenizers/PHP.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Tokenizers/PHP.php b/src/Tokenizers/PHP.php index ecc2964160..b5a7f84ada 100644 --- a/src/Tokenizers/PHP.php +++ b/src/Tokenizers/PHP.php @@ -2268,7 +2268,7 @@ protected function tokenize($string) } if ($prevNonEmpty === null - && isset(Tokens::$emptyTokens[$tokenType]) === false + && @isset(Tokens::$emptyTokens[$tokenType]) === false ) { // Found the previous non-empty token. if ($tokenType === ':' || $tokenType === ',' || $tokenType === T_ATTRIBUTE_END) { @@ -2287,8 +2287,8 @@ protected function tokenize($string) if ($tokenType === T_FUNCTION || $tokenType === T_FN - || isset(Tokens::$methodPrefixes[$tokenType]) === true - || isset(Tokens::$scopeModifiers[$tokenType]) === true + || @isset(Tokens::$methodPrefixes[$tokenType]) === true + || @isset(Tokens::$scopeModifiers[$tokenType]) === true || $tokenType === T_VAR || $tokenType === T_READONLY ) { @@ -2311,7 +2311,7 @@ protected function tokenize($string) break; } - if (isset(Tokens::$emptyTokens[$tokenType]) === false) { + if (@isset(Tokens::$emptyTokens[$tokenType]) === false) { $lastSeenNonEmpty = $tokenType; } }//end for