Skip to content

Commit a800264

Browse files
canvuralondrejmirtes
authored andcommitted
fix code review comments
1 parent d686c82 commit a800264

File tree

4 files changed

+22
-11
lines changed

4 files changed

+22
-11
lines changed

rules.neon

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
conditionalTags:
2+
PHPStan\Rules\PHPUnit\ShouldCallParentMethodsRule:
3+
phpstan.rules.rule: %featureToggles.bleedingEdge%
4+
5+
services:
6+
-
7+
class: PHPStan\Rules\PHPUnit\ShouldCallParentMethodsRule
8+
19
rules:
210
- PHPStan\Rules\PHPUnit\AssertSameBooleanExpectedRule
311
- PHPStan\Rules\PHPUnit\AssertSameNullExpectedRule

src/Rules/PHPUnit/ShouldCallParentMethodsRule.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ public function getNodeType(): string
2121

2222
public function processNode(Node $node, Scope $scope): array
2323
{
24-
/** @var InClassMethodNode $node */
25-
$node = $node;
26-
2724
if ($scope->getClassReflection() === null) {
2825
return [];
2926
}
@@ -42,16 +39,18 @@ public function processNode(Node $node, Scope $scope): array
4239
return [];
4340
}
4441

45-
if (!in_array(strtolower($node->getOriginalNode()->name->name), ['setup', 'teardown'], true)) {
42+
$methodName = $node->getOriginalNode()->name->name;
43+
44+
if (!in_array(strtolower($methodName), ['setup', 'teardown'], true)) {
4645
return [];
4746
}
4847

49-
$hasParentCall = $this->hasParentClassCall($node->getOriginalNode()->getStmts());
48+
$hasParentCall = $this->hasParentClassCall($node->getOriginalNode()->getStmts(), strtolower($methodName));
5049

5150
if (!$hasParentCall) {
5251
return [
5352
RuleErrorBuilder::message(
54-
sprintf('Missing call to parent::%s method.', $node->getOriginalNode()->name->name)
53+
sprintf('Missing call to parent::%s() method.', $methodName)
5554
)->build(),
5655
];
5756
}
@@ -61,10 +60,11 @@ public function processNode(Node $node, Scope $scope): array
6160

6261
/**
6362
* @param Node\Stmt[]|null $stmts
63+
* @param string $methodName
6464
*
6565
* @return bool
6666
*/
67-
private function hasParentClassCall(?array $stmts): bool
67+
private function hasParentClassCall(?array $stmts, string $methodName): bool
6868
{
6969
if ($stmts === null) {
7070
return false;
@@ -93,7 +93,7 @@ private function hasParentClassCall(?array $stmts): bool
9393
continue;
9494
}
9595

96-
if (in_array(strtolower($stmt->expr->name->name), ['setup', 'teardown'], true)) {
96+
if (strtolower($stmt->expr->name->name) === $methodName) {
9797
return true;
9898
}
9999
}

tests/Rules/PHPUnit/ShouldCallParentMethodsRuleTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@ public function testRule(): void
1919
{
2020
$this->analyse([__DIR__ . '/data/missing-parent-method-calls.php'], [
2121
[
22-
'Missing call to parent::setUp method.',
22+
'Missing call to parent::setUp() method.',
2323
32,
24+
],[
25+
'Missing call to parent::setUp() method.',
26+
55,
2427
],
2528
[
26-
'Missing call to parent::tearDown method.',
29+
'Missing call to parent::tearDown() method.',
2730
63,
2831
],
2932
]);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class FooBarBazTest extends BaseTestCase
5555
public function setUp(): void
5656
{
5757
$result = 1 + 1;
58-
parent::setUp();
58+
parent::tearDown();
5959

6060
$this->fooBarBaz = $result;
6161
}

0 commit comments

Comments
 (0)