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" 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..3f429a2d 100644 --- a/tests/Rules/PHPUnit/data/missing-parent-method-calls.php +++ b/tests/Rules/PHPUnit/data/missing-parent-method-calls.php @@ -162,3 +162,78 @@ public function setUp(): void }; } } + +class TerminatingSetUpSelfMarkSkippedTest extends BaseTestCase +{ + public function setUp(): void + { + self::markTestSkipped('Skipped'); + } +} + +class TerminatingSetUpThisMarkSkippedTest extends BaseTestCase +{ + public function setUp(): void + { + $this->markTestSkipped('Skipped'); + } +} + +class TerminatingSetUpMarkIncompleteTest extends BaseTestCase +{ + public function setUp(): void + { + $this->markTestIncomplete('Incomplete'); + } +} + +class TerminatingSetUpFailTest extends BaseTestCase +{ + public function setUp(): void + { + $this->fail('Failed'); + } +} + +class TerminatingSetUpThrowTest extends BaseTestCase +{ + public function setUp(): void + { + throw new \RuntimeException('Error'); + } +} + +class TerminatingTearDownTest extends BaseTestCase +{ + public function tearDown(): void + { + $this->fail('Failed'); + } +} + +class NonTerminatingConditionalSkipMissingParentTest extends BaseTestCase +{ + private bool $shouldSkip = false; + + public function setUp(): void + { + if ($this->shouldSkip) { + self::markTestSkipped('Skipped'); + } + } +} + +class NonTerminatingConditionalWithMissingParentTest extends BaseTestCase +{ + private bool $shouldSkip = false; + + public function setUp(): void + { + if ($this->shouldSkip) { + self::markTestSkipped('Skipped'); + } + parent::setUp(); + } +} + +