From ef75c9f33638d706cc1350b198ce6e2e70678cba 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/4] 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 | 26 +++++--- .../ShouldCallParentMethodsRuleTest.php | 4 ++ .../data/missing-parent-method-calls.php | 61 +++++++++++++++++++ 3 files changed, 81 insertions(+), 10 deletions(-) diff --git a/src/Rules/PHPUnit/ShouldCallParentMethodsRule.php b/src/Rules/PHPUnit/ShouldCallParentMethodsRule.php index 7f3d082f..5c5a6c8b 100644 --- a/src/Rules/PHPUnit/ShouldCallParentMethodsRule.php +++ b/src/Rules/PHPUnit/ShouldCallParentMethodsRule.php @@ -8,7 +8,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; @@ -21,31 +21,37 @@ 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() + && $node->getReturnStatements() === [] + && $node->getReturnStatementsAfterFinally() === [] + ) { 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 []; @@ -59,14 +65,14 @@ public function processNode(Node $node, Scope $scope): array return []; } - $stmts = $node->getOriginalNode()->getStmts() ?? []; + $stmts = $node->getStatements(); if ($this->hasParentClassCall($stmts, strtolower($methodName))) { return []; } $errorBuilder = RuleErrorBuilder::message( sprintf('Missing call to parent::%s() method.', $methodName), - )->identifier('phpunit.callParent'); + )->line($node->getStartLine())->identifier('phpunit.callParent'); if ($scope->isInTrait()) { return [$errorBuilder->build()]; @@ -78,7 +84,7 @@ public function processNode(Node $node, Scope $scope): array } $parentMethodName = $parentMethod->getName(); - $errorBuilder->fixNode($node->getOriginalNode(), static function (ClassMethod $methodNode) use ($position, $parentMethodName): ClassMethod { + $errorBuilder->fixNode($node->getClassMethod(), static function (ClassMethod $methodNode) use ($position, $parentMethodName): ClassMethod { $stmts = $methodNode->stmts ?? []; array_splice($stmts, $position, 0, [ new Node\Stmt\Expression( diff --git a/tests/Rules/PHPUnit/ShouldCallParentMethodsRuleTest.php b/tests/Rules/PHPUnit/ShouldCallParentMethodsRuleTest.php index 51fff4ef..df67ba6b 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 12d9ce06..9a41dbb4 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 2ce25d29551cffe559c296c4a7ef1784dc437126 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Tue, 24 Nov 2026 06:36:33 +0100 Subject: [PATCH 2/4] 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 9a41dbb4..1b5e51f1 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 a9076e5d7f08e83ae8ef7be04114c7e4a28f8c78 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Tue, 24 Nov 2026 06:37:45 +0100 Subject: [PATCH 3/4] 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 1b5e51f1..3f429a2d 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(); + } +} + + From 5d8a0306003cb4c26e456f7bdd57639e712cd6a1 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Thu, 26 Nov 2026 07:17:33 +0100 Subject: [PATCH 4/4] raise min-phpstan version --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 6fc29aa1..7306cd11 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ "require": { "php": "^7.4 || ^8.0", "phar-io/version": "^3.2", - "phpstan/phpstan": "^2.3.0" + "phpstan/phpstan": "^2.3.6" }, "conflict": { "phpunit/phpunit": "<7.0"