Problem/Motivation

When adding a new test that uses Drupal\Tests\UpdatePathTestTrait 10 new phpstan errors are thrown for that test because of missing return types. This means the baseline will continue to grow as new tests use this trait.

E.g https://git.drupalcode.org/issue/drupal-3529274/-/jobs/5520794

Steps to reproduce

Add a new test that uses the trait

Proposed resolution

Add return types, tidy up baseline

Remaining tasks

Do it

API changes

Added return types to UpdateTestTrait - should be no BC concerns here.

Issue fork drupal-3529504

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

acbramley created an issue. See original summary.

acbramley’s picture

Status: Active » Needs review
phenaproxima’s picture

Status: Needs review » Reviewed & tested by the community

Get a load of that diffstat! 😍😍😍

xjm credited cmlara.

xjm credited nicxvan.

xjm’s picture

Issue tags: +Needs followup

Gorgeous!

Adding credits for this Slack discussion also, since I took it into account when considering the issue scope:

nicxvan
Today at 6:53 PM
Wow! (edited)
6:54
Are there other traits like that? Or are the rest not in tests

acbramley
1 hour ago
there are almost definitely other traits without return types, I've fixed one other recently in the same issue I found the problem in. The issue is that phpstan reports the errors in the file that imports the trait, not the trait itself so adding a trait that has no return types into a new test means adding to the baseline :weary: (edited)

nicxvan
40 minutes ago
Well the original trait must have the error in baseline too, right?
7:13
I'll search later

cmlara
12 minutes ago
IIRC traits are scanned in the context of the files that use them, not independently. (A trait can not be independently executed unlike a class, and thus must be scanned in a full file.)
That said a phpstan run (not a baseline) does output that it is from a trait IIRC so one could identify them easily enough.

While I am quite sure there are probably other traits in core missing their void return typehint, the sheer scope of the baseline reduction here is enough for me to commit this regardless. Saving issue credits.

  • xjm committed f5dcaae0 on 11.x
    Issue #3529504 by acbramley, cmlara, nicxvan: Fix phpstan errors in...

  • xjm committed 3835dd84 on 11.2.x
    Issue #3529504 by acbramley, cmlara, nicxvan: Fix phpstan errors in...
xjm’s picture

Status: Reviewed & tested by the community » Patch (to be ported)

Committed to 11.x and 11.2.x. Hooray!

We'll want a 10.5.x version of this also, so setting PTBP. Thanks!

xjm’s picture

I should note that, technically, test traits are considered API, and changing the function signature to add a return typehint (even a void return typehint) is technically a BC break.

However, I stil think this is an acceptable change for the following reasons:

  1. Writing upgrade path tests is probably very, very uncommon outside the core issue queue. (Heck, it's uncommon even in the core issue queue.)
  2. For someone to actually be disrupted by this, I think they'd have to be overriding the trait methods rather than merely calling them, or maybe treating them as some false-ish non-null value. I'd be very surprised if anyone ever does the former, and I wouldn't even support the latter.
  3. The reduction in technical debt is significant for a small, very theoretical disruption.
  4. The change is being made only in minors during the beta phase, and it's actually an ideal sort of beta target issue.

All that said, we shouldn't use this as precedent to commit other return typehint additions to test traits without RM signoff. Test traits were excluded from the scope of #3455548: [meta] Add return typehinting to the test codebase on purpose. So if/when we file followups to add the appropriate void typehints to any test traits, we should make sure that the BC policy and potential disruption are given due consideration before spending 10 min at a time generating new PHPStan baselines and such. :D

xjm’s picture

xjm’s picture

xjm’s picture

Version: 11.x-dev » 10.5.x-dev

Oops.

acbramley’s picture

Status: Patch (to be ported) » Needs review

These missing return types don't flag errors currently on 10.5.x so I don't know if it's even worth bothering. I guess if we eventually bump phpstan levels on 10.5 then it would be but that seems unlikely?

nicxvan’s picture

Yeah that's a much smaller diff!

I've reviewed it, I'll mark it if it passes and committers can decide to close this or merge it.

nicxvan’s picture

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

Ahh interesting. So it's not as compelling a 10.5.x backport, BUT given:

However, I'm pretty sure if you override a method from a trait then the trait method effectively does not exist, so you can do whatever you want with the signature and PHP won't care. See https://3v4l.org/kW8DR

That means it's as non-disruptive as a baby bunny in its nest! So, might as well backport for, you know, parity.

d.o, y u uncredit commmitter? Saving. No wonder my clients are making sad faces at me.

  • xjm committed f82ca85c on 10.5.x
    Issue #3529504 by acbramley, xjm, nicxvan, cmlara: Fix phpstan errors in...
xjm’s picture

Status: Reviewed & tested by the community » Fixed

All done!

  • catch committed 26381154 on 10.6.x authored by xjm
    Issue #3529504 by acbramley, xjm, nicxvan, cmlara: Fix phpstan errors in...

Status: Fixed » Closed (fixed)

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