Problem/Motivation

In #3400366: Remove DrupalComponentTestListenerTrait and replace with a PHPStan rule, we are trying to replace DrupalComponentTestListenerTrait with a PHPStan rule.

The testing of that rule via PHPUnit fails, though, given PHPStan uses a internally version 4 of nikic/php-parser. See details here: https://github.com/phpstan/phpstan/issues/10620

We have currently locked nikic/php-parser to v5.

However, there is no reason to strictly adhere to that constraint at the moment:

$ composer why nikic/php-parser 
phpunit/php-code-coverage 9.2.31 requires nikic/php-parser (^4.18 || ^5.0) 
sebastian/complexity      2.0.3  requires nikic/php-parser (^4.18 || ^5.0) 
sebastian/lines-of-code   1.0.4  requires nikic/php-parser (^4.18 || ^5.0) 

Not even under PHPUnit 10:

$ composer why nikic/php-parser 
phpunit/php-code-coverage 10.1.14 requires nikic/php-parser (^4.18 || ^5.0) 
sebastian/complexity      3.2.0   requires nikic/php-parser (^4.18 || ^5.0) 
sebastian/lines-of-code   2.0.2   requires nikic/php-parser (^4.18 || ^5.0) 

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

CommentFileSizeAuthor
#17 3441353-nr-bot.txt90 bytesneeds-review-queue-bot

Issue fork drupal-3441353

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

mondrake created an issue. See original summary.

mondrake’s picture

mondrake’s picture

Status: Active » Needs review
mondrake’s picture

Title: Downgrade nikic/php-parser to ^4 » Downgrade (temporarily) nikic/php-parser to ^4
Issue summary: View changes
mondrake’s picture

Issue summary: View changes
spokje’s picture

Not a big fan of lowering versions of dependencies, but seeing that:

- None of our other dependencies need it
- Tests are green
- PHPUnit 10 waits for no man...

I'm going to RTBC

spokje’s picture

Status: Needs review » Reviewed & tested by the community
alexpott’s picture

I;ve read https://github.com/phpstan/phpstan/issues/7943 and it states quite clearly that

Upgrading is not blocked by PHPStan. You can use PHPStan in projects that use PHP-Parser v5.

Reading #3400366: Remove DrupalComponentTestListenerTrait and replace with a PHPStan rule it sounds like the real issue is with DebugClassLoader

mondrake’s picture

mondrake’s picture

You can USE PHPStan for static analysis, but you cannot TEST your own rules.

alexpott’s picture

So the solution is also written there...

The rare problem happens when you're mixing the two versions at runtime. So if you run tests for something that loads PHP-Parser v5, it's going to crash PHPStan's tests which need PHP-Parser v4.

The solution is to test these things separately. @group the PHPStan extensions tests and run them in a separate CI target.

Given how PHPStan runs we can hit this problem in the future. If we adding PHPStan tests to core then we're going to need to run them separately.

longwave’s picture

This will also prevent us from going to PHPUnit 11, though I suppose we cross that bridge when we come to it: https://github.com/sebastianbergmann/php-code-coverage/blob/main/compose...

But because PHPUnit itself uses nikic/php-parser, I'm not sure how we run these tests without explicitly installing v4 anyway? PHPUnit will load the code and then PHPStan crashes because of it, as far as I can see, unless I'm missing something. Or do we somehow need to make the autoloader look in the phar first?

catch’s picture

Status: Reviewed & tested by the community » Needs review

Looks like this needs more discussion.

I think it would be fine to exclude phpstan tests in the main phpunit job and add a new job, which could then manually downgrade nikic/php-parser?

mondrake’s picture

#13 locally I solved by running composer require nikic/php-parser:^4, and the test passes (it throws some debugclassloader deprecations that we'll have to silence, but one thing at a time).

So we can
a) downgrade like here
b) create a separate job to just run PHPUnit on PHPstan rules, downgrading via composer require nikic/php-parser:^4 on each run
c) mark the test skipped in #3400366: Remove DrupalComponentTestListenerTrait and replace with a PHPStan rule while we wait for PHPStan 2

To me this one seemed the lightest to do, and in any case is temp. If we do want to do b) for more separation, then it's another discussion, and I think should happen in #3400366: Remove DrupalComponentTestListenerTrait and replace with a PHPStan rule.

alexpott’s picture

I'm for B. I think or PHPStan test should not use our root composer.json for installing and testing things. Or perhaps even better and maybe option D, we should not put the PHPStan rules in core. Can we contribute them to mglaman's package or can we add another package.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new90 bytes

The Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

mondrake’s picture

Status: Needs work » Needs review
Issue tags: +no-needs-review-bot
smustgrave’s picture

Would this need to be done before 10.3?

mondrake’s picture

Status: Needs review » Closed (won't fix)

As discussed in the parent, we will likely go for a decoupling of the PHPStan rule tests from Drupal core test, so this is not going to be necessary.