Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

T_FN constant is used on PHP 7.3 where it is not available #811

Closed
dvdoug opened this issue Sep 18, 2020 · 6 comments
Closed

T_FN constant is used on PHP 7.3 where it is not available #811

dvdoug opened this issue Sep 18, 2020 · 6 comments

Comments

@dvdoug
Copy link
Contributor

dvdoug commented Sep 18, 2020

Q A
php-code-coverage version 9.1.10
PHP version 7.3.22
Driver PCOV
Xdebug version (if used) N/A
Installation Method Composer
Usage Method PHPUnit
PHPUnit version (if used) 9.3.10

T_FN on PHP 7.3 results in an undefined constant warning. Similar to #808 I assume. The CI run that shows the error is here: https://github.com/dvdoug/behat-code-coverage/runs/1134780400

Weirdly, the error is only shown when running under PCOV on CI, although I can reproduce locally when using Xdebug too.

Generating code coverage report in HTML format ... 
Fatal error: Uncaught Error: Undefined constant 'T_FN' in /code/vendor/phpunit/php-code-coverage/src/Report/Html/Renderer/File.php:1094
Stack trace:
#0 /code/vendor/phpunit/php-code-coverage/src/Report/Html/Renderer/File.php(1052): SebastianBergmann\CodeCoverage\Report\Html\File::keywordTokens()
#1 /code/vendor/phpunit/php-code-coverage/src/Report/Html/Renderer/File.php(938): SebastianBergmann\CodeCoverage\Report\Html\File->isKeyword(379)
#2 /code/vendor/phpunit/php-code-coverage/src/Report/Html/Renderer/File.php(418): SebastianBergmann\CodeCoverage\Report\Html\File->loadFile('/code/src/Contr...')
#3 /code/vendor/phpunit/php-code-coverage/src/Report/Html/Renderer/File.php(132): SebastianBergmann\CodeCoverage\Report\Html\File->renderSourceWithLineCoverage(Object(SebastianBergmann\CodeCoverage\Node\File))
#4 /code/vendor/phpunit/php-code-coverage/src/Report/Html/Facade.php(103): SebastianBergmann\CodeCoverage\Report\Html\File->render(Object(SebastianBergmann\CodeCoverage\Node\File), '/code/build/cov...')
#5 /code/vendor/php in /code/vendor/phpunit/phpunit/src/TextUI/Command.php on line 103
@dvdoug
Copy link
Contributor Author

dvdoug commented Sep 18, 2020

I cannot reproduce this on php-code-coverage's own test suite, only when using it via a consuming application's PHPUnit test suite.

I have a hypothesis that it's to do with $parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7) in sebastian/lines-of-code and sebastian/complexity. Although this library was changed to use non-emulative lexing, those 2 sub-libraries are still using it and I think might be acting as an accidental compatibility layer in some (but not) circumstances depending on the order things are executed in?

@dvdoug
Copy link
Contributor Author

dvdoug commented Sep 18, 2020

On further debugging, I reliably see the error on my own project when using <coverage cacheDirectory="...">. Removing the cache makes the error disappear.

Therefore, I think it's safe to say that it's parsing (at all) that is triggering the compatibility layer and allowing T_FN to work on PHP7.3, and was previously allowing T_MATCH to work on 7.3/7.4. When using the cache, no parsing is done and the compat layer is not loaded resulting in the undefined constant errors appearing.

That's why it only triggered on the pcov testing on my test suite - the pcov test there is part of the same build and runs after the Xdebug test...using the cache.

@sebastianbergmann
Copy link
Owner

sebastianbergmann commented Sep 19, 2020

I have a hypothesis that it's to do with $parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7) in sebastian/lines-of-code and sebastian/complexity.

This library (php-code-coverage) only uses the actual visitor objects, SebastianBergmann\Complexity\ComplexityCalculatingVisitor and SebastianBergmann\LinesOfCode\LineCountingVisitor, from the dependencies you mention.

The $parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7); code in SebastianBergmann\Complexity\Calculator and SebastianBergmann\LinesOfCode\Counter is not used.

PHP-Parser is therefore only used with non-emulative lexing in this library.

@sebastianbergmann sebastianbergmann changed the title Fatal error: Uncaught Error: Undefined constant 'T_FN' in /code/vendor/phpunit/php-code-coverage/src/Report/Html/Renderer/File.php:1094 T_FN constant is used on PHP 7.3 where it is not available Sep 19, 2020
@sebastianbergmann
Copy link
Owner

As of ebce861, the T_FN constant is no longer used on PHP 7.3. And while this change solves the issue at hand, I would like to understand what is going on here with regard to the static analysis cache, etc.

@sebastianbergmann
Copy link
Owner

Ah, of course: even when non-emulative lexing is used, PHP-Parser conditionally defines T_FN and T_MATCH.

See #798 (comment) and nikic/PHP-Parser#701 (comment).

@dvdoug
Copy link
Contributor Author

dvdoug commented Sep 19, 2020

Thanks

smmccabe pushed a commit to smmccabe/php-code-coverage that referenced this issue Sep 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants