Bump PHPStan rule level to 2

Created on 4 March 2024, 10 months ago

Problem/Motivation

How about bumping PHPStan rule level to 2 before D11 is shipped, e.g. during the beta phase?

The baseline will likely be at its lowest after all the deprecated D10 code will have been removed, before new deprecations will start getting in. Also, the PHPStan-2 preparation issues โ†’ have been lingering forever, not much focus; bumping the level and adding to the baseline, even if it will bring a large amount of errors, would potentially drive the focus on the fixes.

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

๐Ÿ“Œ Task
Status

Active

Version

11.0 ๐Ÿ”ฅ

Component
Baseย  โ†’

Last updated about 14 hours ago

Created by

๐Ÿ‡ฎ๐Ÿ‡นItaly mondrake ๐Ÿ‡ฎ๐Ÿ‡น

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @mondrake
  • Merge request !6895Closes #3425412 โ†’ (Open) created by mondrake
  • Pipeline finished with Failed
    10 months ago
    Total: 198s
    #110451
  • ๐Ÿ‡ฎ๐Ÿ‡นItaly mondrake ๐Ÿ‡ฎ๐Ÿ‡น

    We'd probably need to add Jan0707/phpstan-prophecy as a dependency to interpret the prophesized methods that are yielding a lot of L2 errors.

  • ๐Ÿ‡ฎ๐Ÿ‡นItaly mondrake ๐Ÿ‡ฎ๐Ÿ‡น

    #4 yes, it's the contrary... :)

    Alternative approach.

  • ๐Ÿ‡ฎ๐Ÿ‡นItaly mondrake ๐Ÿ‡ฎ๐Ÿ‡น

    mondrake โ†’ changed the visibility of the branch 3425412-bump-phpstan-rule to hidden.

  • ๐Ÿ‡ฎ๐Ÿ‡นItaly mondrake ๐Ÿ‡ฎ๐Ÿ‡น

    mondrake โ†’ changed the visibility of the branch 3425412-bump-phpstan-rule to active.

  • Pipeline finished with Failed
    10 months ago
    Total: 385s
    #112192
  • Pipeline finished with Failed
    10 months ago
    Total: 377s
    #112198
  • Pipeline finished with Failed
    9 months ago
    Total: 268s
    #129033
  • Status changed to Needs review 9 months ago
  • ๐Ÿ‡ฎ๐Ÿ‡นItaly mondrake ๐Ÿ‡ฎ๐Ÿ‡น

    Adds 7785 new level-2 errors to the baseline.

    Requires --dev jangregor/phpstan-prophecy dev-master. Release 1.0.0 has a failure that was fixed by https://github.com/Jan0707/phpstan-prophecy/pull/316. We would need to wait for a point release here.

    NR for the concept.

  • Pipeline finished with Failed
    9 months ago
    Total: 613s
    #129036
  • ๐Ÿ‡ณ๐Ÿ‡ฑNetherlands idebr
  • ๐Ÿ‡ฎ๐Ÿ‡นItaly mondrake ๐Ÿ‡ฎ๐Ÿ‡น

    Tagging.

  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia mstrelan

    I generated a baseline for each level from 1-6 so see where is the biggest jump.

    level 1: 630 errors
    level 2: 8415 errors
    level 3: 10657 errors
    level 4: 14012 errors
    level 5: 16015 errors
    level 6: 64641 errors

    I think if we're happy with a baseline of ~8000 errors at level then we should aim a little higher, perhaps level 3, or, if there is appetite, then 4 or 5.

  • ๐Ÿ‡ฎ๐Ÿ‡นItaly mondrake ๐Ÿ‡ฎ๐Ÿ‡น

    TBH I do not think we can be happy with a baseline of 8k. But the pre-work to reduce it is nowhere.

    I am rather hoping that bumping the level 1 notch up will drive more fixing, and in any case prevent further non-L2-compliant code to be introduced.

    We have introduced L0 2 years ago with 1k errors more or less, and bumped to L1 in Nov 22 - still having 630 errors now is not a great score... bumping to 8k is a very large step, and more than enough for some time to come.

    Somewhere I read L2 should be the decent minimum for any codebase, let's walk there before we run further.

  • ๐Ÿ‡ฎ๐Ÿ‡นItaly mondrake ๐Ÿ‡ฎ๐Ÿ‡น

    Let me also say there's another reason not to skip levels: each level up will require contributors some time to adapt their coding style - use of assertions or inline doctypes to clarify type of variables, later on description of iterative types etc... it takes time to digest.

    I remember contributing to Doctrine DBAL (that is on L9), without prior experience of PHPStan reports... at first I simply could not understand what I was doing wrong... then I joined the pieces and started seeing the benefits, but you really need to have a codebase that is already at that level to be able to contribute meaningfully. In the current shape of Drupal codebase, jumping ahead would mean that new contribution will have to do all sort of workarounds to satisfy PHPStan, whereas it should be PHPStan helping us improve the code.

  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom longwave UK

    Was hoping for some low hanging fruit we can knock off to quickly reduce the number but it's not looking that promising:

    $ grep message core/.phpstan-baseline.php|sort|uniq -c|sort -rn|head
        203 	'message' => '#^Access to an undefined property Drupal\\\\Core\\\\Session\\\\AccountInterface\\:\\:\\$passRaw\\.$#',
        201 	'message' => '#^Access to an undefined property Drupal\\\\Core\\\\Session\\\\AccountInterface\\:\\:\\$name\\.$#',
         85 	'message' => '#^Call to an undefined method Drupal\\\\Tests\\\\WebAssert\\:\\:waitForElementVisible\\(\\)\\.$#',
         80 	'message' => '#^Call to an undefined method Drupal\\\\Tests\\\\WebAssert\\:\\:waitForElement\\(\\)\\.$#',
         79 	'message' => '#^Call to an undefined method Drupal\\\\Tests\\\\WebAssert\\:\\:assertWaitOnAjaxRequest\\(\\)\\.$#',
         68 	'message' => '#^Call to an undefined method Drupal\\\\Core\\\\TypedData\\\\TraversableTypedDataInterface\\:\\:get\\(\\)\\.$#',
         48 	'message' => '#^Call to an undefined method Drupal\\\\Core\\\\Entity\\\\EntityInterface\\:\\:get\\(\\)\\.$#',
         41 	'message' => '#^Call to an undefined method Drupal\\\\workflows\\\\WorkflowTypeInterface\\:\\:addEntityTypeAndBundle\\(\\)\\.$#',
         32 	'message' => '#^Call to an undefined method Drupal\\\\Core\\\\Entity\\\\EntityInterface\\:\\:getTranslation\\(\\)\\.$#',
         30 	'message' => '#^Cannot access property \\$uri on object\\|false\\.$#',
    

    Even solving all those will only take 867 off the 8,000+ errors.

  • Status changed to Needs work 9 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    MR appears to be unmergable, surprised the bot never picked it up.

    I'm not super into the phpstan stuff like others here in this ticket but in regards to bumping levels. When a new rule or anything is added usually involves having to go back and update several MRs. Can be argued that's just the name of the game with Drupal but can understand community frustration when they have an MR all ready and it keeps getting sent back because of changing rules. So think doing small increments is good.

  • Pipeline finished with Failed
    9 months ago
    Total: 198s
    #136776
  • Pipeline finished with Success
    9 months ago
    Total: 660s
    #136780
  • Status changed to Needs review 9 months ago
  • ๐Ÿ‡ฎ๐Ÿ‡นItaly mondrake ๐Ÿ‡ฎ๐Ÿ‡น

    Please letโ€™s focus on opinions if this should go or not, rather than starting NR-NW tennis. The MR will become unmergeable every day, but itโ€™s not rerolling it that the question changes.

    Also please keep comments to the scope of this issue: bump to level 2. Thereโ€™s another issue that preaches level 9, lets have the appropriate level discussion there, in one place.

    Thanks

  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia mstrelan

    Just pointing out that phpstan-prophecy 1.0.1 has been released and the MR has been updated to that version, so the concerns in #8 about the dev release are no longer applicable. Let's get this in.

  • ๐Ÿ‡ฎ๐Ÿ‡นItaly mondrake ๐Ÿ‡ฎ๐Ÿ‡น
  • ๐Ÿ‡ฎ๐Ÿ‡นItaly mondrake ๐Ÿ‡ฎ๐Ÿ‡น

    @mstrelan thanks a lot for clarifying in #17 that phpstan-prophecy is no longer blocking

  • ๐Ÿ‡ณ๐Ÿ‡ฑNetherlands spokje

    Looks like when we bump to PHPStan L2 we get an initial addition of ~7800 suppressions.
    Seeing the amount of active issues to tackle the L1 suppressions, I think it's safe to assume these won't go away quickly.

    So the benefit would be preventing new L2 issues creeping in new/refactored code.

    It's probably my lack of Google skills, but what are the extra checks in L2 compared to L1, what will be prevented from creeping in by us doing PHPStan L++?

  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia mstrelan

    Level 2 gives you:

    unknown methods checked on all expressions (not just $this), validating PHPDocs

    See https://phpstan.org/user-guide/rule-levels

    Or more specifically see https://github.com/phpstan/phpstan-src/blob/1.11.x/conf/config.level2.neon

  • Status changed to RTBC 9 months ago
  • ๐Ÿ‡ณ๐Ÿ‡ฑNetherlands spokje

    Thanks @mstrelan, should have found that myself, but somehow didn't. (Makes mental note to check caffeine-percentage of new bought coffee-brand).

    Seeing those checks and also the fact that if we want to do it with the least amount of disruptions to other MRs, it's probably now or when D12 arrives, I'm pulling the trigger and put the ball in core commiters court: RTBC for me.

  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom catch

    I'm +1 here for the obvious reason that it will stop us introducing new errors, so even if it takes years to reduce the current baseline, at least it will be visibly getting smaller instead of silently getting larger.

    On going to L3 or L5 etc., staggering the MR breakage for new rules seems like a good reason to do one at a time. We could still try to go up a level every 6 months or 3 months if we want to (I'm not saying we should definitely do that, but it's worth thinking about). It does look like level 5 would be a couple of small jumps.

  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia mstrelan

    I don't want to change the scope, will almost instantly open a follow up if this goes in, but level 3 gives us:

    return types, types assigned to properties

    On all new code, which most code reviews are picking up anyway, but would be much nicer if it was for free.

  • ๐Ÿ‡ณ๐Ÿ‡ฑNetherlands spokje

    One argument against staggering would be that raising the PHPStan level would be disruptive to (almost) all open MRs for each bump, so bumping more then one level would make it less disruptive.

    That might be the case when the baseline was in the NEON format, and IIRC was one (of many) reasons the Bump-to-L9 issue is stalling.
    But as part of that issue, we switched the baseline over to PHP with the reasoning that Git would be much smarter about merging it.

    Is bumping a PHPStan level (with each one bringing in loads of changes on the baseline file) still as disruptive to other MRs as we (or at least I) think, or is this a non-issue by now?

    If we're in non-issue land on that, I'm all about staggering, if not, we might want to prevent all/a lot of open MRs grinding to a rebase-halt each time we stagger-bump and do it in one go?

  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom catch

    One argument against staggering would be that raising the PHPStan level would be disruptive to (almost) all open MRs for each bump, so bumping more then one level would make it less disruptive.

    This is true to an extent, but it will only be disruptive in the following cases:
    1. The MR is changing the baseline already, and has a conflict.
    2. The MR is introducing new PHPStan failures which are caught by the new level.

    Hopefully this won't be almost all open MRs but just a small subset.

    There have been 698 commits to the 11.x branch in the past 90 days, so even if we went up a level every three months, anything committed in the interim between bumps by definition won't get affected. That still leaves the thousands of open MRs, but not all of these are rebased that often too.

  • ๐Ÿ‡ฎ๐Ÿ‡นItaly mondrake ๐Ÿ‡ฎ๐Ÿ‡น

    Just noticed that phpstan-prophecy specifies this in their composer.json

                "conflict": {
                    "phpspec/prophecy": "<1.7.0 || >=2.0.0",
                    "phpunit/phpunit": "<6.0.0 || >=10.0.0"
                },

    So this ATM would block the PHPUnit bump to 10.

    We probably want to wait for a release upstream that is compatible?

  • Status changed to Postponed 9 months ago
  • ๐Ÿ‡ฎ๐Ÿ‡นItaly mondrake ๐Ÿ‡ฎ๐Ÿ‡น

    Actually https://github.com/Jan0707/phpstan-prophecy/pull/334 allowing PHPUnit 10 & 11 was already merged (yesterday!), so it's a matter of waiting for a new point release.

  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia mstrelan

    Does this need a dependency evaluation also?

  • ๐Ÿ‡ฎ๐Ÿ‡นItaly mondrake ๐Ÿ‡ฎ๐Ÿ‡น

    Added a dependency evaluation

  • ๐Ÿ‡ฎ๐Ÿ‡นItaly mondrake ๐Ÿ‡ฎ๐Ÿ‡น

    phpstan-prophecy just released 1.0.2 that is PHPUnit 10 compatible, bumping

  • Pipeline finished with Failed
    8 months ago
    #147819
  • Status changed to Needs review 8 months ago
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom catch

    Back to NR?

  • ๐Ÿ‡ฎ๐Ÿ‡นItaly mondrake ๐Ÿ‡ฎ๐Ÿ‡น

    I refrained from self-RTBC, but it should be a quick check to do it.

    It will certainly require frequent rebasing.

  • Pipeline finished with Success
    8 months ago
    Total: 1788s
    #147850
  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia mstrelan

    Does this still need framework manager review? Otherwise RTBC from me.

  • Status changed to RTBC 8 months ago
  • ๐Ÿ‡ณ๐Ÿ‡ฑNetherlands spokje

    Let's bump it to RTBC and see if the core committers think we still need FM approval and if so, if they can ping them :)

  • Status changed to Needs work 8 months ago
  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia mstrelan

    We probably need a change record :D

  • Status changed to Needs review 8 months ago
  • ๐Ÿ‡ณ๐Ÿ‡ฑNetherlands spokje

    Added two CRs.

  • Status changed to RTBC 8 months ago
  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia mstrelan

    Thanks for creating the CRs. This looks good. Baseline may need to be regenerated when committing.

  • Merge request !7547Resolve #3425412 "Phpstan 2 quick fixes" โ†’ (Open) created by longwave
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom longwave UK

    @alexpott was concerned about the size of the baseline, a handful of quick fixes in MR!7547 cut it down by about 25%.

  • Pipeline finished with Failed
    8 months ago
    Total: 210s
    #149315
  • Pipeline finished with Failed
    8 months ago
    Total: 216s
    #149322
  • ๐Ÿ‡ณ๐Ÿ‡ฑNetherlands spokje

    a handful of quick fixes in MR!7547 cut it down by about 25%.

    Great, but not sure this is part of this issue? Or even what an acceptable amount of added errors to a baseline would be?

    Personally, (n=1, yadayada) this seems like follow-up issue material to me and might be derailing the original intent?
    But what do I know? _shrug_

  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom longwave UK

    Oh yeah, that's why I made a separate MR, only as a demo. But maybe we move that to a separate issue and try and pick off these and any other widespread ones caused by traits or base classes, before doing this, if there are concerns about baseline size.

  • Status changed to Postponed 8 months ago
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom catch

    Let's do what #42 suggests, it's not just the actual baseline size, but that'll also help to avoid some MR conflicts potentially later on.

  • ๐Ÿ‡ฎ๐Ÿ‡นItaly mondrake ๐Ÿ‡ฎ๐Ÿ‡น

    Added inline comments, even though it's for the separate issue now I think.

Production build 0.71.5 2024