From 61d978048ba9a0e3376f0988aa565be3eadfa991 Mon Sep 17 00:00:00 2001 From: lyn1dev <78355114+lyn1dev@users.noreply.github.com> Date: Mon, 23 Nov 2026 23:05:45 +0100 Subject: [PATCH 1/3] Do not require parent setUp/tearDown when method is always terminating 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. --- .../PHPUnit/ShouldCallParentMethodsRule.php | 20 +++--- .../ShouldCallParentMethodsRuleTest.php | 4 ++ .../data/missing-parent-method-calls.php | 61 +++++++++++++++++++ 3 files changed, 76 insertions(+), 9 deletions(-) diff --git a/src/Rules/PHPUnit/ShouldCallParentMethodsRule.php b/src/Rules/PHPUnit/ShouldCallParentMethodsRule.php index f272cc5..7254dad 100644 --- a/src/Rules/PHPUnit/ShouldCallParentMethodsRule.php +++ b/src/Rules/PHPUnit/ShouldCallParentMethodsRule.php @@ -7,7 +7,7 @@ use PhpParser\NodeVisitor; use PhpParser\NodeVisitorAbstract; use PHPStan\Analyser\Scope; -use PHPStan\Node\InClassMethodNode; +use PHPStan\Node\MethodReturnStatementsNode; use PHPStan\Rules\Rule; use PHPStan\Rules\RuleErrorBuilder; use PHPUnit\Framework\TestCase; @@ -16,31 +16,33 @@ use function strtolower; /** - * @implements Rule + * @implements Rule */ class ShouldCallParentMethodsRule implements Rule { public function getNodeType(): string { - return InClassMethodNode::class; + return MethodReturnStatementsNode::class; } public function processNode(Node $node, Scope $scope): array { - $methodName = $node->getOriginalNode()->name->name; + $methodName = $node->getMethodName(); if (!in_array(strtolower($methodName), ['setup', 'teardown'], true)) { return []; } - if ($scope->getClassReflection() === null) { + + if ($node->getStatementResult()->isAlwaysTerminating()) { return []; } - if (!$scope->getClassReflection()->is(TestCase::class)) { + $classReflection = $node->getClassReflection(); + if (!$classReflection->is(TestCase::class)) { return []; } - $parentClass = $scope->getClassReflection()->getParentClass(); + $parentClass = $classReflection->getParentClass(); if ($parentClass === null) { return []; @@ -54,13 +56,13 @@ public function processNode(Node $node, Scope $scope): array return []; } - $hasParentCall = $this->hasParentClassCall($node->getOriginalNode()->getStmts(), strtolower($methodName)); + $hasParentCall = $this->hasParentClassCall($node->getStatements(), strtolower($methodName)); if (!$hasParentCall) { return [ RuleErrorBuilder::message( sprintf('Missing call to parent::%s() method.', $methodName), - )->identifier('phpunit.callParent')->build(), + )->line($node->getStartLine())->identifier('phpunit.callParent')->build(), ]; } diff --git a/tests/Rules/PHPUnit/ShouldCallParentMethodsRuleTest.php b/tests/Rules/PHPUnit/ShouldCallParentMethodsRuleTest.php index 76705d1..7d891f9 100644 --- a/tests/Rules/PHPUnit/ShouldCallParentMethodsRuleTest.php +++ b/tests/Rules/PHPUnit/ShouldCallParentMethodsRuleTest.php @@ -44,6 +44,10 @@ public function testRule(): void 'Missing call to parent::setUp() method.', 155, ], + [ + 'Missing call to parent::setUp() method.', + 218, + ], ]); } diff --git a/tests/Rules/PHPUnit/data/missing-parent-method-calls.php b/tests/Rules/PHPUnit/data/missing-parent-method-calls.php index 12d9ce0..9a41dbb 100644 --- a/tests/Rules/PHPUnit/data/missing-parent-method-calls.php +++ b/tests/Rules/PHPUnit/data/missing-parent-method-calls.php @@ -162,3 +162,64 @@ public function setUp(): void }; } } + +class TerminatingSetUpSelfMarkSkippedTest extends BaseTestCase +{ + protected function setUp(): void + { + self::markTestSkipped('Skipped'); + } +} + +class TerminatingSetUpThisMarkSkippedTest extends BaseTestCase +{ + protected function setUp(): void + { + $this->markTestSkipped('Skipped'); + } +} + +class TerminatingSetUpMarkIncompleteTest extends BaseTestCase +{ + protected function setUp(): void + { + $this->markTestIncomplete('Incomplete'); + } +} + +class TerminatingSetUpFailTest extends BaseTestCase +{ + protected function setUp(): void + { + $this->fail('Failed'); + } +} + +class TerminatingSetUpThrowTest extends BaseTestCase +{ + protected function setUp(): void + { + throw new \RuntimeException('Error'); + } +} + +class TerminatingTearDownTest extends BaseTestCase +{ + protected function tearDown(): void + { + $this->fail('Failed'); + } +} + +class NonTerminatingConditionalSkipMissingParentTest extends BaseTestCase +{ + private bool $shouldSkip = false; + + protected function setUp(): void + { + if ($this->shouldSkip) { + self::markTestSkipped('Skipped'); + } + } +} + From 811322c03d91ffd5fc7665ea7b48d5f3129bed3a Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Tue, 24 Nov 2026 06:36:33 +0100 Subject: [PATCH 2/3] public method cannot be overridden by protected one --- .../PHPUnit/data/missing-parent-method-calls.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/Rules/PHPUnit/data/missing-parent-method-calls.php b/tests/Rules/PHPUnit/data/missing-parent-method-calls.php index 9a41dbb..1b5e51f 100644 --- a/tests/Rules/PHPUnit/data/missing-parent-method-calls.php +++ b/tests/Rules/PHPUnit/data/missing-parent-method-calls.php @@ -165,7 +165,7 @@ public function setUp(): void class TerminatingSetUpSelfMarkSkippedTest extends BaseTestCase { - protected function setUp(): void + public function setUp(): void { self::markTestSkipped('Skipped'); } @@ -173,7 +173,7 @@ protected function setUp(): void class TerminatingSetUpThisMarkSkippedTest extends BaseTestCase { - protected function setUp(): void + public function setUp(): void { $this->markTestSkipped('Skipped'); } @@ -181,7 +181,7 @@ protected function setUp(): void class TerminatingSetUpMarkIncompleteTest extends BaseTestCase { - protected function setUp(): void + public function setUp(): void { $this->markTestIncomplete('Incomplete'); } @@ -189,7 +189,7 @@ protected function setUp(): void class TerminatingSetUpFailTest extends BaseTestCase { - protected function setUp(): void + public function setUp(): void { $this->fail('Failed'); } @@ -197,7 +197,7 @@ protected function setUp(): void class TerminatingSetUpThrowTest extends BaseTestCase { - protected function setUp(): void + public function setUp(): void { throw new \RuntimeException('Error'); } @@ -205,7 +205,7 @@ protected function setUp(): void class TerminatingTearDownTest extends BaseTestCase { - protected function tearDown(): void + public function tearDown(): void { $this->fail('Failed'); } @@ -215,7 +215,7 @@ class NonTerminatingConditionalSkipMissingParentTest extends BaseTestCase { private bool $shouldSkip = false; - protected function setUp(): void + public function setUp(): void { if ($this->shouldSkip) { self::markTestSkipped('Skipped'); From fb9723e174a453c6e5c15668572b20106d385757 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Tue, 24 Nov 2026 06:37:45 +0100 Subject: [PATCH 3/3] test conditional-skip with proper parent::setUp() call --- .../PHPUnit/data/missing-parent-method-calls.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/Rules/PHPUnit/data/missing-parent-method-calls.php b/tests/Rules/PHPUnit/data/missing-parent-method-calls.php index 1b5e51f..3f429a2 100644 --- a/tests/Rules/PHPUnit/data/missing-parent-method-calls.php +++ b/tests/Rules/PHPUnit/data/missing-parent-method-calls.php @@ -223,3 +223,17 @@ public function setUp(): void } } +class NonTerminatingConditionalWithMissingParentTest extends BaseTestCase +{ + private bool $shouldSkip = false; + + public function setUp(): void + { + if ($this->shouldSkip) { + self::markTestSkipped('Skipped'); + } + parent::setUp(); + } +} + +