Skip to content

Commit bb05c60

Browse files
committed
fixup! AssertEqualsIsDiscouragedRule
1 parent bef077d commit bb05c60

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

src/Rules/PHPUnit/AssertEqualsIsDiscouragedRule.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,29 @@ public function processNode(Node $node, Scope $scope): array
6363
];
6464
}
6565

66-
$fileContents = explode("\n", file_get_contents($scope->getFile()));
67-
$previousLine = $fileContents[$node->getLine() - 2];
68-
69-
if (!preg_match('~^\s+//\s+assertEquals because(.*)~', $previousLine)) {
66+
if (!$node->hasAttribute('comments')) {
7067
return [
7168
'You should use assertSame instead of assertEquals. Or it should have a comment above with explanation: // assertEquals because ...',
7269
];
7370
}
7471

72+
/** @var \PhpParser\Comment[] $comments */
73+
$comments = $node->getAttribute('comments');
74+
$comment = $comments[count($comments) - 1];
75+
76+
// the comment should be on the line above the assertEquals()
77+
if ($comment->getLine() !== ($node->getLine() - 1)) {
78+
return [
79+
'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)',
80+
];
81+
}
82+
83+
if (!preg_match('~^//\s+assertEquals because(.*)~', $comment->getReformattedText())) {
84+
return [
85+
'You should use assertSame instead of assertEquals. Or it should have a comment above with explanation: // assertEquals because ... (There is a different comment)',
86+
];
87+
}
88+
7589
return [];
7690
}
7791

tests/Rules/PHPUnit/AssertEqualsIsDiscouragedRuleTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,21 @@ public function testRule(): void
3232
16,
3333
],
3434
[
35-
'You should use assertSame instead of assertEquals. Or it should have a comment above with explanation: // assertEquals because ...',
35+
'You should use assertSame instead of assertEquals. Or it should have a comment above with explanation: // assertEquals because ... (There is a different comment)',
3636
19,
3737
],
3838
[
3939
'You should use assertSame instead of assertEquals. Or it should have a comment above with explanation: // assertEquals because ...',
4040
21,
4141
],
4242
[
43-
'You should use assertSame instead of assertEquals. Or it should have a comment above with explanation: // assertEquals because ...',
43+
'You should use assertSame instead of assertEquals. Or it should have a comment above with explanation: // assertEquals because ... (There is a different comment)',
4444
24,
4545
],
46+
[
47+
'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)',
48+
28,
49+
],
4650
]);
4751
}
4852

tests/Rules/PHPUnit/data/assert-equals-is-discouraged.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ public function testAssertEqualsIsDiscouraged()
2323
// with incorrect comment
2424
$this->assertEquals(1, '1');
2525

26+
// assertEquals because I want it! But sadly, the comment is not just above the assert.
27+
28+
$this->assertEquals(1, '1');
29+
2630
// assertEquals because I want it!
2731
$this->assertEquals(1, '1');
2832
}

0 commit comments

Comments
 (0)