Skip to content

Do not require parent setUp/tearDown when method is always terminating - #360

Open
lyn1dev wants to merge 3 commits into
phpstan:2.1.xfrom
lyn1dev:fix-292-terminating-setup
Open

lyn1dev wants to merge 3 commits into
phpstan:2.1.xfrom
lyn1dev:fix-292-terminating-setup

Conversation

@lyn1dev

@lyn1dev lyn1dev commented Nov 24, 2026

Copy link
Copy Markdown

When setUp() or tearDown() always terminates early (for example, by calling markTestSkipped(), markTestIncomplete(), fail(), or by unconditionally throwing an exception), PHPStan's ShouldCallParentMethodsRule previously reported a missing call to parent::setUp() or parent::tearDown(). Requiring parent setup when the test is unconditionally skipped or fails early causes unnecessary work before termination, or dead-code errors if placed after.

Reproducer

class FailingTest extends ParentTestCase
{
    protected function setUp(): void
    {
        self::markTestSkipped('Skipped');
    }
}

Previously reported:

Missing call to parent::setUp() method.

Solution

  • Switch ShouldCallParentMethodsRule to inspect MethodReturnStatementsNode instead of InClassMethodNode.
  • Check $node->getStatementResult()->isAlwaysTerminating() and return early if the method unconditionally terminates.
  • Keep reporting missing parent calls when the skip or exit is conditional (e.g. inside an if block).

Test Plan

  • Added unit tests covering self::markTestSkipped(), $this->markTestSkipped(), markTestIncomplete(), fail(), throw, and conditional skipping.
  • vendor/bin/phpunit tests/Rules/PHPUnit/ShouldCallParentMethodsRuleTest.php passes.
  • vendor/bin/phpstan analyse -c phpstan.neon passes with 0 errors.
  • Coding standards (phpcs) and parallel-lint pass.

Closes #292

AI was used to write this code.

lyn1dev and others added 3 commits November 23, 2026 23:05
When setUp() or tearDown() always terminates early (e.g. via markTestSkipped(), markTestIncomplete(), fail(), or an unconditional exception), a call to the parent method is not required.

Switch ShouldCallParentMethodsRule to inspect MethodReturnStatementsNode and check whether the method's statement result is always terminating.
@staabm

staabm commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

needs rebase

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

False positive missing call to parent when setUp never returns

2 participants