Determine if there are other TestTraits we can add return types to.

Created on 11 June 2025, 3 days ago

Problem/Motivation

πŸ“Œ Fix phpstan errors in UpdatePathTestTrait Active was a massive reduction in baseline. #3529504-11: Fix phpstan errors in UpdatePathTestTrait β†’ Pointed out that we can't just go through all test traits and update them blindly.

Check a list of TestTraits to see if there are other quick wins.

Unreviewed

  • core/modules/system/tests/src/Functional/Entity/Traits/EntityDefinitionTestTrait.php
  • core/modules/system/tests/src/Traits/TestTrait.php
  • core/modules/menu_link_content/tests/src/Kernel/Migrate/MigrateMenuLinkTestTrait.php
  • core/modules/basic_auth/tests/src/Traits/BasicAuthTestTrait.php
  • core/modules/rest/tests/src/Functional/CookieResourceTestTrait.php
  • core/modules/rest/tests/src/Functional/AnonResourceTestTrait.php
  • core/modules/rest/tests/src/Functional/BasicAuthResourceTestTrait.php
  • core/modules/media/tests/src/Traits/OEmbedTestTrait.php
  • core/modules/package_manager/tests/src/Traits/ValidationTestTrait.php
  • core/modules/comment/src/Tests/CommentTestTrait.php
  • core/modules/jsonapi/tests/src/Functional/ResourceResponseTestTrait.php
  • core/modules/migrate_drupal/tests/src/Traits/ValidateMigrationStateTestTrait.php
  • core/modules/migrate_drupal/tests/src/Traits/FieldDiscoveryTestTrait.php
  • core/modules/migrate_drupal/tests/src/Traits/NodeMigrateTypeTestTrait.php
  • core/modules/migrate_drupal/src/Tests/StubTestTrait.php
  • core/modules/content_moderation/tests/src/Traits/ContentModerationTestTrait.php
  • core/modules/serialization/tests/src/Unit/Normalizer/InternalTypedDataTestTrait.php
  • core/modules/ckeditor5/tests/src/Traits/CKEditor5TestTrait.php
  • core/modules/file/tests/src/Kernel/Migrate/d6/FileMigrationTestTrait.php
  • core/modules/field_ui/tests/src/Traits/FieldUiTestTrait.php
  • core/modules/taxonomy/tests/src/Functional/TaxonomyTranslationTestTrait.php
  • core/modules/taxonomy/tests/src/Traits/TaxonomyTestTrait.php
  • core/modules/views/tests/src/Unit/Plugin/HandlerTestTrait.php
  • core/modules/workspaces/tests/src/Kernel/WorkspaceTestTrait.php
  • core/tests/Drupal/FunctionalJavascriptTests/SortableTestTrait.php
  • core/tests/Drupal/Tests/SessionTestTrait.php
  • core/tests/Drupal/Tests/Core/Database/SchemaIntrospectionTestTrait.php
  • core/tests/Drupal/Tests/PerformanceTestTrait.php
  • core/tests/Drupal/Tests/Traits/Core/PathAliasTestTrait.php
  • core/tests/Drupal/Tests/ConfigTestTrait.php
  • core/tests/Drupal/KernelTests/Core/Hook/HookOrderTestTrait.php
  • core/profiles/standard/tests/src/Traits/StandardTestTrait.php

Can be updated

Can NOT be updated

Steps to reproduce

N/A

Proposed resolution

Review list and identify traits we can update in this round.

Remaining tasks

Review
Update trait method return types.

User interface changes

N/A

Introduced terminology

N/A

API changes

N/A

Data model changes

N/A

Release notes snippet

N/A

πŸ“Œ Task
Status

Active

Version

11.0 πŸ”₯

Component

phpunit

Created by

πŸ‡ΊπŸ‡ΈUnited States nicxvan

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

Comments & Activities

  • Issue created by @nicxvan
  • πŸ‡ΊπŸ‡ΈUnited States nicxvan

    Added the number of times core uses the traits.

  • πŸ‡ΊπŸ‡ΈUnited States xjm
  • πŸ‡ΊπŸ‡ΈUnited States xjm

    The juiciest targets in the list -- the REST stuff -- are actually also potentially the riskiest, because REST is actually tested heavily in in contrib in various extension modules, because Wim.

    OTOH, CommentTestTrait is not that likely to get used; it was factored out of the legacy monstrosity that was descended from the original Comment module SimpleTests. (Which were also the very first automated tests of any kind written for core, so... yeah. (We had things to learn back then.) So that single method is probably fair game as a quick win.

    All that said, before we start weighing this versus that, we should probably consider at a higher level whether we're actually concerned about the disruption of changing these signatures with a return typehint that matches the current behavior. It's disruptive if:

    1. You use the trait, and
    2. You override the method without it also already having a void return typehint on the signature.

    If that happens, you get a nice clean fatal telling you that declaration of blah doesn't match blah and the line number.

    So the questions are:

    1. How many people are overriding these test trait methods ever?
    2. Do we care if we give them fatals in their tests as part of reducing both the baseline and the scope of what has to be typehinted in fiddly ways in πŸ“Œ Introduce a @return-type-will-be-added annotation to indicate return type addition in next major; enforce with a PHPStan rule Active ?
    3. If we sort of care, should we make the changes in a way that mitigates disruption, such as grouping them as beta targets (so no patch backport divergence to worry about nor bridging of multiple versions for contrib)?
  • πŸ‡¦πŸ‡ΊAustralia mstrelan

    Is this a dupe of πŸ“Œ [meta] Add return types to all test Traits Active ?

  • πŸ‡¦πŸ‡ΊAustralia mstrelan

    #4.3 (if we sort of care) - we can use @return instead and phpstan will stop complaining, at least while we have treatPhpDocTypesAsCertain. Then we can figure out when to actually make the change.

    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

  • πŸ‡¦πŸ‡ΊAustralia acbramley

    However, I'm pretty sure if you override a method from a trait then the trait method effectively does not exist

    TIL! Great point.

    It does look like a dupe

  • πŸ‡ΊπŸ‡ΈUnited States nicxvan

    I wonder who made the duplicate...

    I think we move the files over here and close the other even though it's older, there is better info here.

  • πŸ‡¦πŸ‡ΊAustralia mstrelan

    Starting from the top, I opened πŸ“Œ Add return types to EntityDefinitionTestTrait Active . While there are only 11 usages of the trait, the trait has 30 functions in it, so it's a much bigger cleanup than CommentTestTrait with only 1 function.

  • πŸ‡¦πŸ‡ΊAustralia mstrelan

    Ranking the remaining from juiciest to least juicy:

    CookieResourceTestTrait - 1746 deletions
    AnonResourceTestTrait - 1224 deletions
    BasicAuthResourceTestTrait - 1140 deletions
    FieldUiTestTrait - 810 deletions
    WorkspaceTestTrait - 312 deletions
    CommentTestTrait - 270 deletions
    ContentModerationTestTrait - 228 deletions
    PathAliasTestTrait - 216 deletions
    OEmbedTestTrait - 108 deletions

    Everything else is less than 100 lines deleted. Any suggestions for how to split?

  • πŸ‡ΊπŸ‡ΈUnited States xjm

    We need to address #4 and the RM review first. :)

    But also:

    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

    πŸ’‘ Neat! But also wait, why did I get a different result? Was I a silly pumpkin using classes instead of traits when I meant to test traits? Clearly I was.

    I tested it with void specifically and confirmed that PHP doesn't care. Double neat! Also weird!

    Thus: RM review of disruption hereby completed πŸͺ„ and #4 is addressed. Leaving the tag on for the scope discussion. Proceeding thereto:

    The main factor for the reviewability of these isn't the number of deletions (now that we know that deleting and regenerating the baseline is way faster and presumably would be the same amount of time regardless of scope), nor the usages (because doing weird false-y things with a void return method is unsupported nonsense), but the number of actual methods under review. We simply need to open the methods and verify that they don't return anything under any circumstance. It sounds like we don't even need to look for anyone overriding those methods to see if overridden method call anything (which would normally be the next thing I would say), so we can focus only on the actual methods themselves. We also don't need to read the baseline itself since it's an auto-generated asset that's reviewed with CI and CLI tooling.

    Since we need to review the whole methods to verify that they are, indeed, intended to be void, the line length of the methods is the factor we want to look at (so far fewer changes per MR than when we were bulk-adding the typehints to actual test methods where returning anything woudl be illogical. We should target logically grouped contexts that give us roughly 100-200 LOC of methods that are updated. All of it is one pattern, so there is no need to separate it up by different types of changes; therefore, we can group it by subsystem or logical groups of subsystems, since the code style is more likely to be internally consistent.

    If I remember that comment trait, it's probably that long by itself. The REST and JSONAPI methods together probably form a group of about the right size, although I didn't actually check. It doesn't need to be exact, but scan the traits in given subsystems or logical groups of subsystems and use your judgement therefrom. :) We'll try the first few issues like that, and then we can decide if we want to adjust.

    OK, now actually removing the tag. πŸͺ„ 🌈

  • πŸ‡ΊπŸ‡ΈUnited States xjm

    @mstrelan, your 3v4l of trait overrides led to core committers posting extra punctuation, e.g.:

    So we can add return type hints to every trait in core whenever? (!?!?!)

  • πŸ‡ΊπŸ‡ΈUnited States xjm

    I closed πŸ“Œ [meta] Add return types to all test Traits Active as a duplicate since this issue has more detail now. Attaching the assets from that issue here.

  • πŸ‡ΊπŸ‡ΈUnited States xjm
Production build 0.71.5 2024