Problem/Motivation

Running PHPStan locally on the MR from #3254866: Update the deprecation notices related to "Manage Permissions" pages it fails with:

Running PHPStan on changed files.
 ------ ----------------------------------------------------------------------- 
  Line   tests/src/Kernel/FieldUiRouteEnhancerTest.php                          
 ------ ----------------------------------------------------------------------- 
  27     The "%alias_id%" service is deprecated in drupal:9.4.0 and is removed  
         from drupal:10.0.0. Use the "route_enhancer.entity_bundle" service     
         instead. See https://www.drupal.org/node/3245017                       
 ------ ----------------------------------------------------------------------- 


                                                                                
 [ERROR] Found 1 error                                                          
                                                                                


PHPStan: failed

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

CommentFileSizeAuthor
#28 3262937-27.patch3.42 KBalexpott
#19 3215062-19.patch5.02 KBalexpott

Issue fork drupal-3262937

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

catch created an issue. See original summary.

catch’s picture

Priority: Major » Critical

Since this prevents some patches being committed, bumping to critical.

mondrake’s picture

I suppose we could add that to the baseline short term, while we file and wait an issue upstream to prevent this being reported?

mallezie’s picture

Am i misreading something, but that MR should be comitted to D9.4 which does not have PHPStan?

mallezie’s picture

If reading summary correct here the point is to add deprecated code (in a test here?). This should be marked explicitly as phpstan-ignore then (or added to the baseline).

mondrake’s picture

I think @catch was prepping the D10 commit that needs to go first anyway in Drupal’s workflow

mallezie’s picture

I see. But then indeed PHPStan is 'correct' here. PHPStan expects when you deprecate code, you also remove usages of it. (Since using deprecated code is a level 0 notice). So in this case we add the exception by saying on that line, ignore it, or add it to the baseline (if the plan is to later remove, which is the case here, so i would say indeed add it to the baseline)

mondrake’s picture

Actually if the deprecation is for D10, the D10 patch should just not have the legacy test, no?

mondrake’s picture

catch’s picture

We add deprecation tests for most new Drupal core deprecations, I don't think we want to be adding every single one to the phpstan baseline.

mallezie’s picture

@catch this is not needed. Only here we're taking a bit of a 'shortcut' were we add a 'deprecated in D10' item in D10. (Which we do to be able to cherry-pick it to D9, but actually in D10 it should not be there, since it's deprecated in D10).

Normal flow would be we add a 'deprecated in D11', with deprecation test to D10 branch, which PHPStan will not flag untill we open the D11 branch, then phpstan will flag it to have it removed in D11 (which is correct).

In this specific case we have 2 options, add the usage of a deprecated function in D10 (and add it to the baseline to remove later), or only add it to D9.

catch’s picture

@mallezie we have a lot of code that is deprecated for removal in 10.0.0 in the 10.0.x branch, because we're still in the process of removing all of that deprecated code. This isn't only an issue for new changes if we run phpstan on the whole code base per #3259355: Always do a full phpstan analysis on DrupalCI.

mallezie’s picture

In our current setup we only have deprecation rules which are included in the mglaman/phpstan-drupal package.
https://github.com/mglaman/phpstan-drupal/tree/main/src/Rules/Deprecations

Specific rule in the above context kicking in is StaticServiceDeprecatedServiceRule.php

Question is how we want phpstan to notice our deprecations. If we want all deprecations to be flagged (this might include deprecations for D11, which we might not want (yet)) we should add the https://github.com/phpstan/phpstan-deprecation-rules package.
To filter between what deprecations to flag (the ones for D10, but not the ones for D11) we should check how upgrade_status does this (if that does it)

If we don't want deprecations to be flagged we should disable the rules included in the phpstan-drupal package.

catch’s picture

The main issue here is that we don't want deprecations flagged in phpunit tests marked @legacy. We know they're going to call deprecated code because that's the point of them. There are already multiple steps to deprecating code (deprecation itself, change record, test coverage, release note, then removing the deprecation and test in the next version, and possibly rector rules if we add those), and that is enough steps really. So if we could add support for @legacy, that would be good.

In general, I think it's OK (probably good) for other core code to be flagged if it's using something deprecated, because this will either be a failed Drupal deprecation, or something upstream from Symfony. However, disabling all deprecation checking, then enabling it again when we don't need to implement workarounds for the current core workflow would be fine I think.

mallezie’s picture

Status: Active » Needs review

This MR disables the (partial) deprecation checking we have at the moment.
I've created #3263109: Use PHPStan for deprecation checking to further discuss how to handle deprecations with phpstan.

mallezie’s picture

Issue tags: +PHPStan
alexpott’s picture

Status: Needs review » Reviewed & tested by the community

This looks great to me. We3 already have deprecated service testing via Symfony's PHPUnit bridge so this is duplicating and the only things it is discovering is correct usage in legacy tests.

alexpott’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new5.02 KB

I've had another think about this and I think we can just add a comment to the legacy test to disable the phpstan check for the next line. It means we'll still benefit from untested code that calls a deprecated service being found and it's not that much an onus to do.

Here's a new patch that does just this.

catch’s picture

Status: Needs review » Reviewed & tested by the community

#19 looks fine.

I'm not convinced it's going to work if we want to enable phpstan's own deprecation checking to cover everything, would be hundreds of these, but we can revisit it when we're closer to doing that.

mondrake’s picture

#19 is to me the best option ATM; may require some extra work to PHPStan-ignore calls in legacy tests when deprecating something but I think that's OK, PHPStan will tell you what.

alexpott’s picture

Adding credit for @catch and @mondrake

taran2l’s picture

great! happy to see that direction with comments has been taken

mglaman’s picture

+++ b/core/tests/Drupal/FunctionalTests/Core/Container/ServiceDeprecationTest.php
@@ -34,7 +34,9 @@ class ServiceDeprecationTest extends BrowserTestBase {
+    // @phpstan-ignore-next-line
     \Drupal::service('deprecation_test.service');
+    // @phpstan-ignore-next-line
     \Drupal::service('deprecation_test.alias');

<3 I love it! It also makes it easy to grep for and find usages of ignores later on. Thanks @alexpott!

mallezie’s picture

Looks good to me. For sure since we already now with the legacy tag, that those tests need to go in the next version.

mondrake’s picture

catch’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs reroll

Needs a re-roll.

alexpott’s picture

Status: Needs work » Reviewed & tested by the community
StatusFileSize
new3.42 KB

core/modules/node/tests/src/Functional/NodeRevisionPermissionsTest.php had been deleted so easy to reroll.

  • catch committed 30e53df on 10.0.x
    Issue #3262937 by mallezie, alexpott, catch, mondrake, mglaman: PHPStan...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed 30e53df and pushed to 10.0.x. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.