Skip to content

Commit

Permalink
fixup! AssertEqualsIsDiscouragedRule
Browse files Browse the repository at this point in the history
  • Loading branch information
mhujer committed Dec 28, 2017
1 parent bef077d commit bb05c60
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
22 changes: 18 additions & 4 deletions src/Rules/PHPUnit/AssertEqualsIsDiscouragedRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,29 @@ public function processNode(Node $node, Scope $scope): array
];
}

$fileContents = explode("\n", file_get_contents($scope->getFile()));
$previousLine = $fileContents[$node->getLine() - 2];

if (!preg_match('~^\s+//\s+assertEquals because(.*)~', $previousLine)) {
if (!$node->hasAttribute('comments')) {
return [
'You should use assertSame instead of assertEquals. Or it should have a comment above with explanation: // assertEquals because ...',
];
}

/** @var \PhpParser\Comment[] $comments */
$comments = $node->getAttribute('comments');
$comment = $comments[count($comments) - 1];

// the comment should be on the line above the assertEquals()
if ($comment->getLine() !== ($node->getLine() - 1)) {
return [
'You should use assertSame instead of assertEquals. Or it should have a comment above with explanation: // assertEquals because ... (The comment is not directly above the assertEquals)',
];
}

if (!preg_match('~^//\s+assertEquals because(.*)~', $comment->getReformattedText())) {
return [
'You should use assertSame instead of assertEquals. Or it should have a comment above with explanation: // assertEquals because ... (There is a different comment)',
];
}

return [];
}

Expand Down
8 changes: 6 additions & 2 deletions tests/Rules/PHPUnit/AssertEqualsIsDiscouragedRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,21 @@ public function testRule(): void
16,
],
[
'You should use assertSame instead of assertEquals. Or it should have a comment above with explanation: // assertEquals because ...',
'You should use assertSame instead of assertEquals. Or it should have a comment above with explanation: // assertEquals because ... (There is a different comment)',
19,
],
[
'You should use assertSame instead of assertEquals. Or it should have a comment above with explanation: // assertEquals because ...',
21,
],
[
'You should use assertSame instead of assertEquals. Or it should have a comment above with explanation: // assertEquals because ...',
'You should use assertSame instead of assertEquals. Or it should have a comment above with explanation: // assertEquals because ... (There is a different comment)',
24,
],
[
'You should use assertSame instead of assertEquals. Or it should have a comment above with explanation: // assertEquals because ... (The comment is not directly above the assertEquals)',
28,
],
]);
}

Expand Down
4 changes: 4 additions & 0 deletions tests/Rules/PHPUnit/data/assert-equals-is-discouraged.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public function testAssertEqualsIsDiscouraged()
// with incorrect comment
$this->assertEquals(1, '1');

// assertEquals because I want it! But sadly, the comment is not just above the assert.

$this->assertEquals(1, '1');

// assertEquals because I want it!
$this->assertEquals(1, '1');
}
Expand Down

0 comments on commit bb05c60

Please sign in to comment.