Add void return typehints to protected test helper methods

Created on 24 June 2024, 4 months ago

Problem/Motivation

See 🌱 Add return typehints to protected test helper methods Active

Steps to reproduce

Proposed resolution

Use \Rector\TypeDeclaration\Rector\ClassMethod\AddVoidReturnTypeWhereNoReturnRector to add void return type to protected test methods. This rule may need manipulating to skip public methods.

private function shouldSkipClassMethod(ClassMethod $classMethod) : bool
{
    // Add this to skip public methods.
    if ($classMethod->isPublic()) {
        return \true;
    }
     
    // The rest of the method body remains here ...

    //  Comment out this bit.
    // if ($classMethod->isProtected()) {
    //     return !$this->classModifierChecker->isInsideFinalClass($classMethod);
    // }

    return $this->classModifierChecker->isInsideAbstractClass($classMethod) && $classMethod->getStmts() === [];
}

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

📌 Task
Status

Active

Version

11.0 🔥

Component
PHPUnit  →

Last updated 1 day ago

Created by

🇦🇺Australia mstrelan

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

Merge Requests

Comments & Activities

  • Issue created by @mstrelan
  • Pipeline finished with Failed
    4 months ago
    Total: 191s
    #206434
  • Pipeline finished with Failed
    4 months ago
    Total: 202s
    #206436
  • Status changed to Needs review 4 months ago
  • 🇦🇺Australia mstrelan

    Have included private methods here too.

  • Pipeline finished with Success
    4 months ago
    Total: 597s
    #206441
  • Status changed to RTBC 4 months ago
  • 🇺🇸United States smustgrave

    Was a larger MR but appears all changes have been done to the protected functions.

    Was expected a 1 to 1 for additions to subtractions but see some phpstan things were fixed in the process. Not super clear how adding void to doTestGrouping() fixed those but the checks are all green so won't think too much into it.

    LGTM

  • 🇦🇺Australia mstrelan

    Was expected a 1 to 1 for additions to subtractions but see some phpstan things were fixed in the process. Not super clear how adding void to doTestGrouping() fixed those but the checks are all green so won't think too much into it.

    Most of the phpstan issues were due to rector adding a return to the parent method but not the child method, likely due to the order they were processed. So phpstan would complain that they are not covariant, which adding the type fixed.

    The other issues were when a caller attempted to use the return value of something that returns void, for example, $this->assertNull($this->somethingVoid()).

  • Pipeline finished with Success
    4 months ago
    Total: 786s
    #213510
  • Status changed to Needs work 3 months ago
  • 🇬🇧United Kingdom longwave UK

    Annoyingly as this didn't make 11.0.0-rc1 we can't just commit this as-is. Discussed with @xjm and although we only provide best-effort BC in tests there are some base classes and common traits touched here that I think we should avoid changing except in a major release, ie they will have to wait for 12.x now.

    Some files that I think we should revert here are:

    core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php
    core/modules/migrate/tests/src/Kernel/MigrateTestBase.php
    core/modules/rest/tests/src/Functional/ResourceTestBase.php
    core/tests/Drupal/FunctionalJavascriptTests/WebDriverTestBase.php
    core/tests/Drupal/FunctionalTests/Installer/InstallerTestBase.php
    core/tests/Drupal/FunctionalTests/Update/UpdatePathTestBase.php
    core/tests/Drupal/KernelTests/AssertContentTrait.php
    core/tests/Drupal/KernelTests/KernelTestBase.php
    core/tests/Drupal/Tests/BrowserTestBase.php
    core/tests/Drupal/Tests/UiHelperTrait.php
    

    These are just the ones that I know are widely used as base classes/traits, there may be more, although there's also a tradeoff in considering which methods are really likely to be overridden in subclasses.

  • 🇺🇸United States xjm

    I just mentioned to @longwave that IMO this actually isn't tied to RC at all -- we would want to backport as much of this as possible to 10.3 to cut down on merge conflicts. I apologize for not giving better/clearer advice to start! Hopefully it's just a matter of tweaking a filename matching pattern or two to exclude things called *Base.php or *Trait.php.

  • 🇺🇸United States xjm

    Oh also: We can discuss what to do with the leftover methods once the easy/non-disruptive ones are committed. E.g., breaking ResourceTestBase or MigrateTestBase causes pain for a small handful of contributors who are already very active enough to address it quickly, but breaking BTB methods themselves is probably not something we should do in 11.1. So that's the only example where the RC does make a difference, I think. But that's a few lines out of thousands here that we can clean up from the parent(s) once the bulk of the work is committed.

  • 🇺🇸United States xjm

    Oh FWIW regarding:

    Was expected a 1 to 1 for additions to subtractions but see some phpstan things were fixed in the process. Not super clear how adding void to doTestGrouping() fixed those but the checks are all green so won't think too much into it.

    If the only thing that's not adding : void is deleting lines from our PHPStan baseline, that's better than good. If there are other changes to actual code, though, that would be something to keep an eye on and document.

  • Pipeline finished with Canceled
    3 months ago
    Total: 66s
    #224131
  • Pipeline finished with Failed
    3 months ago
    Total: 285s
    #224137
  • Pipeline finished with Failed
    3 months ago
    Total: 565s
    #224144
  • Status changed to Needs review 3 months ago
  • 🇦🇺Australia mstrelan

    Re-ran the rector and decided to just git add core/*Test.php as that was easier than filtering out all the other paths, like traits, base classes, test modules, and random helpers in core/tests. That should address #6 and #7. We can discuss #8 in the parent.

  • Pipeline finished with Failed
    3 months ago
    Total: 460s
    #224151
  • Pipeline finished with Success
    3 months ago
    Total: 1074s
    #224165
  • Status changed to RTBC 3 months ago
  • 🇺🇸United States smustgrave

    The addition to subtraction is equal so confident in the use of :void.

    I spot checked a few dozen and change appears to be as advertised.

    Didn't run a grep but this seems like a good chunk and any stragglers I think could be handled later if needed.

  • Status changed to Needs work 3 months ago
  • 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.

  • Pipeline finished with Failed
    7 days ago
    Total: 121s
    #306782
  • Status changed to Needs review 7 days ago
  • 🇦🇺Australia mstrelan

    Rebased on HEAD. Since ✨ Enforce return types in all new methods and functions RTBC we also need to regenerate the phpstan baseline, so this is a nice reduction of 266KB.

  • Pipeline finished with Success
    7 days ago
    Total: 1153s
    #306790
  • 🇺🇸United States smustgrave

    Rebase seems good.

Production build 0.71.5 2024