Skip to content

Tokenization failure with line comment in ternary #1418

Closed

Description

@weirdan

Describe the bug

phpcs fails to tokenize the file when a line comment (//) is used before : in a ternary.

Code sample

<?php

echo ($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:

  1. Create a file called test.php with the code sample above...
  2. Run phpcs -ps test.php
  3. 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.
  • I have read the Contribution Guidelines and this is not a support question.
  • I confirm that this bug is a bug in PHP_CodeSniffer and not in one of the external standards.
  • I have verified the issue still exists in the 4.x branch of PHP_CodeSniffer.

Activity

  1. rodrigoprimo commented on Apr 29, 2026

    @rodrigoprimo
    Contributor

    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).

    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 skips null entries, and the ? retokenization code also handles them since #1230.

    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.

  2. jrfnl commented on Apr 29, 2026

    @jrfnl
    Member

    This sounds like a bug which is part of task #1231 ?

  3. rodrigoprimo commented on Apr 30, 2026

    @rodrigoprimo
    Contributor

    Yes, it does sound like a bug related to this task.

  4. added this to the 4.0.2 milestone on Aug 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions