Skip to content

PHP-Parser interferes with testing code that uses (back|for)ward compatibility layers for ext/tokenizer #798

@jrfnl

Description

@jrfnl
Q A
php-code-coverage version whichever version comes with PHPUnit releases
PHP version PHP 7.3 - 7.4 (confirmed, possibly more)
Driver Xdebug
Xdebug version (if used) 2.9.6
Installation Method BOTH: Composer / PHPUnit PHAR
Usage Method PHPUnit
PHPUnit version (if used) 9.3.x

When running PHPUnit with code coverage enabled the tests for PHPCSUtils and PHPCompatibility have started to semi-randomly break due to PHPUnit's use of PHP-Parser.

PHP-Parser breaks our tests as it interferes with globally defined PHP native constants by backfilling them.

The first few times a defined() for a PHP native token constant is called, the response is correct and as expected, but the third time that same defined() is called, the response will be true, even if the token constant is not defined in our stack and all subsequent tests relying on the defined() logic working will subsequently fail.

Related code in PHP-Parser:

vendor/nikic/php-parser/lib/PhpParser/Lexer.php:        if (!defined('T_NULLSAFE_OBJECT_OPERATOR')) {
vendor/nikic/php-parser/lib/PhpParser/Lexer.php:            \define('T_NULLSAFE_OBJECT_OPERATOR', -8);

Could it be that PHP Parser is loaded too early to prevent interference ?

For an extensive description of the problem, please see: https://gist.github.com/jrfnl/c6fad49bb7580b7b4371af3c68b122f7

Note: while this isn't a problem yet for PHP_CodeSniffer itself at this time as they don't support PHPUnit 9 yet, it will be in the near future. PHPCS itself should also be able to rely on whether or not tokens are defined in PHP to determine how to adjust a tokenized file (if necessary). So, the PHP native tokens being defined by PHP Parser will break coverage builds for PHPCS as well.

With graceful thanks to @derickr for helping me debug this as I initially though it was related to the use of the latest Xdebug version.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions