Skip to content

False positive missing call to parent when setUp never returns #292

Description

@axlon

When creating a test that has a setup method that never returns (e.g. because the whole test should be skipped), PHPStan will complain that the parent::setUp() should be called.

In this case, we have to choose between either setting up the parent before we skip the test (doing useless work in the process), setting up the parent after the skip, which will trigger an error about dead code, or baselining the original error. All of these solutions seem wrong to me and I think in this PHPStan just shouldn't tell you to call the parent.

Example:

final class MyTest extends TestCase
{
    protected function setUp(): never
    {
        self::markTestSkipped('Currently broken due to XYZ');
    }
}

Activity

  1. staabm commented on May 25, 2026

    @staabm
    Contributor

    sorry for leeting this alone without feedback for so long. I just found this issue.
    I tried reproducing it but had no luck.

    could you provide a small runnable example or provide a small repository which shows the problem?
    the problem might only occur in certain phpunit versions or similar.

    thank you

  2. axlon commented on Jun 17, 2026

    @axlon
    Author

    @staabm thanks for taking a look at this, to reproduce it seems that the test needs to extend a class that defines a setup method, for example:

    class PassingTest extends PHPUnit\Framework\TestCase
    {
        protected function setUp(): void
        {
            self::markTestSkipped();
        }
    }
    
    class ParentTestCase extends PHPUnit\Framework\TestCase
    {
        protected function setUp(): void
        {
            parent::setUp();
        }
    }
    
    class FailingTest extends ParentTestCase
    {
        protected function setUp(): void
        {
            self::markTestSkipped();
        }
    }

    For me FailingTest::setUp() triggers the error, but PassingTest::setUp() does not:

      Line   tmp.php                                  
     ------ ----------------------------------------- 
      21     Missing call to parent::setUp() method.  
             🪪  phpunit.callParent                   
             at tmp.php:21                            
     ------ ----------------------------------------- 
    

    About my configuration:

    phpstan/phpstan: 2.1.40
    phpstan/phpstan-deprecation-rules: 2.0.4
    phpstan/phpstan-mockery: 2.0.0
    phpstan/phpstan-phpunit: 2.0.16
    phpstan/phpstan-strict-rules: 2.0.10
    phpstan/phpstan-webmozart-assert: 2.0.0
    

    Hope this helps

  3. staabm commented on Jun 25, 2026

    @staabm
    Contributor

    thank you, I can reproduce the problem.

    we need to think about expectations for tearDown, markTestIncomplete, markAsRisky methods.

    in addition we need to make sure we also instance method variance of all involved static methods, e.g. $this->markTestSkipped()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions