Skip to content

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

Merged
staabm merged 4 commits into
phpstan:2.1.xfrom
lyn1dev:fix-292-terminating-setup
Nov 26, 2026
Merged

staabm merged 4 commits into
phpstan:2.1.xfrom
lyn1dev:fix-292-terminating-setup

Conversation

@lyn1dev

@lyn1dev lyn1dev commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

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.

@staabm

staabm commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

needs rebase

lyn1dev and others added 3 commits November 26, 2026 02:00
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.
@lyn1dev
lyn1dev force-pushed the fix-292-terminating-setup branch from fb9723e to a9076e5 Compare November 26, 2026 01:01
@lyn1dev

lyn1dev commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

@staabm Rebased onto 2.1.x (head �9076e5), integrating the RuleErrorBuilder::fixNode() changes from #356 and guarding the always-terminating check on getReturnStatements()/getReturnStatementsAfterFinally() so
eturn; paths still trigger and auto-fix. �endor/bin/phpunit (128 tests, 142 assertions, 1 skipped), �endor/bin/phpstan analyse -c phpstan.neon, �endor/bin/parallel-lint, and �endor/bin/detect-collisions pass locally.

@staabm
staabm merged commit a5027a5 into phpstan:2.1.x Nov 26, 2026
114 checks passed
@staabm

staabm commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

thank you!

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

3 participants