You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Tokenization failure with line comment in ternary #1418
phpcs fails to tokenize the file when a line comment (//) is used before : in a ternary.
Code sample
<?phpecho ($argc >= 10)
? "aaa"//
: '';
Custom ruleset
No, the bug is reproducible without any custom ruleset (I removed .phpcs.xml)
To reproduce
Steps to reproduce the behavior:
Create a file called test.php with the code sample above...
Run phpcs -ps test.php
See error message displayed
❯ docker compose exec app vendor/bin/phpcs -ps t.php
E 1 / 1 (100%)
FILE: /var/www/html/t.php
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 | ERROR | An error occurred during processing; checking has been aborted. The error message was: Trying to access array offset on null in
| | /var/www/html/vendor/squizlabs/php_codesniffer/src/Tokenizers/PHP.php on line 2628 (Internal.Exception)
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Time: 16ms; Memory: 6MB
Expected behavior
This is a valid PHP code that should be tokenized successfully.
Versions (please complete the following information)
Operating System
Linux (Debian 6.19.13-1)
PHP version
8.4.17
PHP_CodeSniffer version
4.0.1
Standard
irrelevant
Install type
Composer, local
Additional context
None.
Please confirm
I have searched the issue list and am not opening a duplicate issue.
Thanks for your report, @weirdan! I can confirm this bug. I was able to reproduce it on the 4.x branch. It seems this is a PHP 8+ only problem. I was not able to reproduce it when running PHPCS with PHP 7.4.
The error I got points to line 2680 instead of line 2628 as reported in the issue, probably due to new code added to the file after the 4.0.1 release.
Below is my initial assessment of what seems to be causing this problem:
On PHP 8+, different from previous versions, // and # comment tokens no longer include the trailing newline (https://3v4l.org/6jf0n).
However, the backward-walking loops in the colon-handling section do not skip null entries. When one of these loops encounters a null entry, is_array(null) returns false, causing the loop to break with $i pointing to the null entry. The subsequent $tokens[$i][0] access then triggers the "Trying to access array offset on null" error. After this check, there are other $tokens[$i] that don't take into account the possibility of null entries.
Activity
rodrigoprimo commentedon Apr 29, 2026
Thanks for your report, @weirdan! I can confirm this bug. I was able to reproduce it on the
4.xbranch. It seems this is a PHP 8+ only problem. I was not able to reproduce it when running PHPCS with PHP 7.4.The error I got points to line 2680 instead of line 2628 as reported in the issue, probably due to new code added to the file after the 4.0.1 release.
Below is my initial assessment of what seems to be causing this problem:
On PHP 8+, different from previous versions,
//and#comment tokens no longer include the trailing newline (https://3v4l.org/6jf0n).To account for this, the tokenizer merges the trailing newline back into the comment token and sets the original newline token entry to
null. This handling was added in squizlabs/PHP_CodeSniffer#3027. The main tokenizer loop skipsnullentries, and the?retokenization code also handles them since #1230.However, the backward-walking loops in the colon-handling section do not skip
nullentries. When one of these loops encounters anullentry,is_array(null)returnsfalse, causing the loop to break with$ipointing to thenullentry. The subsequent$tokens[$i][0]access then triggers the "Trying to access array offset on null" error. After this check, there are other$tokens[$i]that don't take into account the possibility ofnullentries.jrfnl commentedon Apr 29, 2026
This sounds like a bug which is part of task #1231 ?
rodrigoprimo commentedon Apr 30, 2026
Yes, it does sound like a bug related to this task.