Skip to content

Commit

Permalink
fix code review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
canvural authored and ondrejmirtes committed Jul 28, 2020
1 parent d686c82 commit a800264
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
8 changes: 8 additions & 0 deletions rules.neon
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
conditionalTags:
PHPStan\Rules\PHPUnit\ShouldCallParentMethodsRule:
phpstan.rules.rule: %featureToggles.bleedingEdge%

services:
-
class: PHPStan\Rules\PHPUnit\ShouldCallParentMethodsRule

rules:
- PHPStan\Rules\PHPUnit\AssertSameBooleanExpectedRule
- PHPStan\Rules\PHPUnit\AssertSameNullExpectedRule
Expand Down
16 changes: 8 additions & 8 deletions src/Rules/PHPUnit/ShouldCallParentMethodsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ public function getNodeType(): string

public function processNode(Node $node, Scope $scope): array
{
/** @var InClassMethodNode $node */
$node = $node;

if ($scope->getClassReflection() === null) {
return [];
}
Expand All @@ -42,16 +39,18 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

if (!in_array(strtolower($node->getOriginalNode()->name->name), ['setup', 'teardown'], true)) {
$methodName = $node->getOriginalNode()->name->name;

if (!in_array(strtolower($methodName), ['setup', 'teardown'], true)) {
return [];
}

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

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

/**
* @param Node\Stmt[]|null $stmts
* @param string $methodName
*
* @return bool
*/
private function hasParentClassCall(?array $stmts): bool
private function hasParentClassCall(?array $stmts, string $methodName): bool
{
if ($stmts === null) {
return false;
Expand Down Expand Up @@ -93,7 +93,7 @@ private function hasParentClassCall(?array $stmts): bool
continue;
}

if (in_array(strtolower($stmt->expr->name->name), ['setup', 'teardown'], true)) {
if (strtolower($stmt->expr->name->name) === $methodName) {
return true;
}
}
Expand Down
7 changes: 5 additions & 2 deletions tests/Rules/PHPUnit/ShouldCallParentMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ public function testRule(): void
{
$this->analyse([__DIR__ . '/data/missing-parent-method-calls.php'], [
[
'Missing call to parent::setUp method.',
'Missing call to parent::setUp() method.',
32,
],[
'Missing call to parent::setUp() method.',
55,
],
[
'Missing call to parent::tearDown method.',
'Missing call to parent::tearDown() method.',
63,
],
]);
Expand Down
2 changes: 1 addition & 1 deletion tests/Rules/PHPUnit/data/missing-parent-method-calls.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class FooBarBazTest extends BaseTestCase
public function setUp(): void
{
$result = 1 + 1;
parent::setUp();
parent::tearDown();

$this->fooBarBaz = $result;
}
Expand Down

0 comments on commit a800264

Please sign in to comment.