Skip to content

Commit a5027a5

Browse files
lyn1devstaabm
andauthored
Do not require parent setUp/tearDown when method is always terminating (#360)
* 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. * public method cannot be overridden by protected one * test conditional-skip with proper parent::setUp() call * raise min-phpstan version --------- Co-authored-by: Markus Staab <maggus.staab@googlemail.com>
1 parent 056385f commit a5027a5

4 files changed

Lines changed: 96 additions & 11 deletions

File tree

‎composer.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"require": {
1010
"php": "^7.4 || ^8.0",
1111
"phar-io/version": "^3.2",
12-
"phpstan/phpstan": "^2.3.0"
12+
"phpstan/phpstan": "^2.3.6"
1313
},
1414
"conflict": {
1515
"phpunit/phpunit": "<7.0"

‎src/Rules/PHPUnit/ShouldCallParentMethodsRule.php‎

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use PhpParser\NodeVisitor;
99
use PhpParser\NodeVisitorAbstract;
1010
use PHPStan\Analyser\Scope;
11-
use PHPStan\Node\InClassMethodNode;
11+
use PHPStan\Node\MethodReturnStatementsNode;
1212
use PHPStan\Rules\Rule;
1313
use PHPStan\Rules\RuleErrorBuilder;
1414
use PHPUnit\Framework\TestCase;
@@ -21,31 +21,37 @@
2121
use function strtolower;
2222

2323
/**
24-
* @implements Rule<InClassMethodNode>
24+
* @implements Rule<MethodReturnStatementsNode>
2525
*/
2626
class ShouldCallParentMethodsRule implements Rule
2727
{
2828

2929
public function getNodeType(): string
3030
{
31-
return InClassMethodNode::class;
31+
return MethodReturnStatementsNode::class;
3232
}
3333

3434
public function processNode(Node $node, Scope $scope): array
3535
{
36-
$methodName = $node->getOriginalNode()->name->name;
36+
$methodName = $node->getMethodName();
3737
if (!in_array(strtolower($methodName), ['setup', 'teardown'], true)) {
3838
return [];
3939
}
40-
if ($scope->getClassReflection() === null) {
40+
41+
if (
42+
$node->getStatementResult()->isAlwaysTerminating()
43+
&& $node->getReturnStatements() === []
44+
&& $node->getReturnStatementsAfterFinally() === []
45+
) {
4146
return [];
4247
}
4348

44-
if (!$scope->getClassReflection()->is(TestCase::class)) {
49+
$classReflection = $node->getClassReflection();
50+
if (!$classReflection->is(TestCase::class)) {
4551
return [];
4652
}
4753

48-
$parentClass = $scope->getClassReflection()->getParentClass();
54+
$parentClass = $classReflection->getParentClass();
4955

5056
if ($parentClass === null) {
5157
return [];
@@ -59,14 +65,14 @@ public function processNode(Node $node, Scope $scope): array
5965
return [];
6066
}
6167

62-
$stmts = $node->getOriginalNode()->getStmts() ?? [];
68+
$stmts = $node->getStatements();
6369
if ($this->hasParentClassCall($stmts, strtolower($methodName))) {
6470
return [];
6571
}
6672

6773
$errorBuilder = RuleErrorBuilder::message(
6874
sprintf('Missing call to parent::%s() method.', $methodName),
69-
)->identifier('phpunit.callParent');
75+
)->line($node->getStartLine())->identifier('phpunit.callParent');
7076

7177
if ($scope->isInTrait()) {
7278
return [$errorBuilder->build()];
@@ -78,7 +84,7 @@ public function processNode(Node $node, Scope $scope): array
7884
}
7985

8086
$parentMethodName = $parentMethod->getName();
81-
$errorBuilder->fixNode($node->getOriginalNode(), static function (ClassMethod $methodNode) use ($position, $parentMethodName): ClassMethod {
87+
$errorBuilder->fixNode($node->getClassMethod(), static function (ClassMethod $methodNode) use ($position, $parentMethodName): ClassMethod {
8288
$stmts = $methodNode->stmts ?? [];
8389
array_splice($stmts, $position, 0, [
8490
new Node\Stmt\Expression(

‎tests/Rules/PHPUnit/ShouldCallParentMethodsRuleTest.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ public function testRule(): void
4444
'Missing call to parent::setUp() method.',
4545
155,
4646
],
47+
[
48+
'Missing call to parent::setUp() method.',
49+
218,
50+
],
4751
]);
4852
}
4953

‎tests/Rules/PHPUnit/data/missing-parent-method-calls.php‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,78 @@ public function setUp(): void
162162
};
163163
}
164164
}
165+
166+
class TerminatingSetUpSelfMarkSkippedTest extends BaseTestCase
167+
{
168+
public function setUp(): void
169+
{
170+
self::markTestSkipped('Skipped');
171+
}
172+
}
173+
174+
class TerminatingSetUpThisMarkSkippedTest extends BaseTestCase
175+
{
176+
public function setUp(): void
177+
{
178+
$this->markTestSkipped('Skipped');
179+
}
180+
}
181+
182+
class TerminatingSetUpMarkIncompleteTest extends BaseTestCase
183+
{
184+
public function setUp(): void
185+
{
186+
$this->markTestIncomplete('Incomplete');
187+
}
188+
}
189+
190+
class TerminatingSetUpFailTest extends BaseTestCase
191+
{
192+
public function setUp(): void
193+
{
194+
$this->fail('Failed');
195+
}
196+
}
197+
198+
class TerminatingSetUpThrowTest extends BaseTestCase
199+
{
200+
public function setUp(): void
201+
{
202+
throw new \RuntimeException('Error');
203+
}
204+
}
205+
206+
class TerminatingTearDownTest extends BaseTestCase
207+
{
208+
public function tearDown(): void
209+
{
210+
$this->fail('Failed');
211+
}
212+
}
213+
214+
class NonTerminatingConditionalSkipMissingParentTest extends BaseTestCase
215+
{
216+
private bool $shouldSkip = false;
217+
218+
public function setUp(): void
219+
{
220+
if ($this->shouldSkip) {
221+
self::markTestSkipped('Skipped');
222+
}
223+
}
224+
}
225+
226+
class NonTerminatingConditionalWithMissingParentTest extends BaseTestCase
227+
{
228+
private bool $shouldSkip = false;
229+
230+
public function setUp(): void
231+
{
232+
if ($this->shouldSkip) {
233+
self::markTestSkipped('Skipped');
234+
}
235+
parent::setUp();
236+
}
237+
}
238+
239+

0 commit comments

Comments
 (0)