Skip to content

Commit 36cb9a6

Browse files
authored
Make AssertSameBooleanExpectedRule auto-fixable
1 parent 450bfc0 commit 36cb9a6

File tree

5 files changed

+79
-9
lines changed

5 files changed

+79
-9
lines changed

src/Rules/PHPUnit/AssertEqualsIsDiscouragedRule.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,7 @@ public function processNode(Node $node, Scope $scope): array
7373
),
7474
)->identifier('phpunit.assertEquals')
7575
->fixNode($node, static function (CallLike $node) use ($correctName) {
76-
if ($node instanceof Node\Expr\MethodCall) {
77-
$node->name = new Node\Identifier($correctName);
78-
}
79-
80-
if ($node instanceof Node\Expr\StaticCall) {
81-
$node->name = new Node\Identifier($correctName);
82-
}
76+
$node->name = new Node\Identifier($correctName);
8377

8478
return $node;
8579
})

src/Rules/PHPUnit/AssertSameBooleanExpectedRule.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,46 @@ public function processNode(Node $node, Scope $scope): array
4747

4848
if ($expectedArgumentValue->name->toLowerString() === 'true') {
4949
return [
50-
RuleErrorBuilder::message('You should use assertTrue() instead of assertSame() when expecting "true"')->identifier('phpunit.assertTrue')->build(),
50+
RuleErrorBuilder::message('You should use assertTrue() instead of assertSame() when expecting "true"')
51+
->identifier('phpunit.assertTrue')
52+
->fixNode($node, static function (CallLike $node) {
53+
$node->name = new Node\Identifier('assertTrue');
54+
$node->args = self::rewriteArgs($node->args);
55+
56+
return $node;
57+
})
58+
->build(),
5159
];
5260
}
5361

5462
if ($expectedArgumentValue->name->toLowerString() === 'false') {
5563
return [
56-
RuleErrorBuilder::message('You should use assertFalse() instead of assertSame() when expecting "false"')->identifier('phpunit.assertFalse')->build(),
64+
RuleErrorBuilder::message('You should use assertFalse() instead of assertSame() when expecting "false"')
65+
->identifier('phpunit.assertFalse')
66+
->fixNode($node, static function (CallLike $node) {
67+
$node->name = new Node\Identifier('assertFalse');
68+
$node->args = self::rewriteArgs($node->args);
69+
70+
return $node;
71+
})
72+
->build(),
5773
];
5874
}
5975

6076
return [];
6177
}
6278

79+
/**
80+
* @param array<Node\Arg|Node\VariadicPlaceholder> $args
81+
* @return list<Node\Arg|Node\VariadicPlaceholder>
82+
*/
83+
private static function rewriteArgs(array $args): array
84+
{
85+
$newArgs = [];
86+
for ($i = 1; $i < count($args); $i++) {
87+
$newArgs[] = $args[$i];
88+
}
89+
return $newArgs;
90+
}
91+
6392
}

tests/Rules/PHPUnit/AssertSameBooleanExpectedRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ public function testRule(): void
4242
]);
4343
}
4444

45+
public function testFix(): void
46+
{
47+
$this->fix(__DIR__ . '/data/assert-same-boolean-expected-fixable.php', __DIR__ . '/data/assert-same-boolean-expected-fixable.php.fixed');
48+
}
49+
4550
/**
4651
* @return string[]
4752
*/
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace AssertSameBooleanTestCaseFix;
4+
5+
class AssertSameBooleanExpectedTestCase extends \PHPUnit\Framework\TestCase
6+
{
7+
public function returnsBool(): bool
8+
{
9+
if (rand(0, 1)) {
10+
return false;
11+
}
12+
return true;
13+
}
14+
15+
public function doFoo(): void
16+
{
17+
$this->assertSame(true, $this->returnBool());
18+
self::assertSame(false, $this->returnBool());
19+
}
20+
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace AssertSameBooleanTestCaseFix;
4+
5+
class AssertSameBooleanExpectedTestCase extends \PHPUnit\Framework\TestCase
6+
{
7+
public function returnsBool(): bool
8+
{
9+
if (rand(0, 1)) {
10+
return false;
11+
}
12+
return true;
13+
}
14+
15+
public function doFoo(): void
16+
{
17+
$this->assertTrue($this->returnBool());
18+
self::assertFalse($this->returnBool());
19+
}
20+
21+
}

0 commit comments

Comments
 (0)