Skip to content

Commit 8532ceb

Browse files
staabmondrejmirtes
authored andcommitted
Make AssertSameNullExpectedRule auto-fixable
1 parent a4abfc1 commit 8532ceb

File tree

4 files changed

+71
-1
lines changed

4 files changed

+71
-1
lines changed

src/Rules/PHPUnit/AssertSameNullExpectedRule.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,32 @@ public function processNode(Node $node, Scope $scope): array
4747

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

5462
return [];
5563
}
5664

65+
/**
66+
* @param array<Node\Arg|Node\VariadicPlaceholder> $args
67+
* @return list<Node\Arg|Node\VariadicPlaceholder>
68+
*/
69+
private static function rewriteArgs(array $args): array
70+
{
71+
$newArgs = [];
72+
for ($i = 1; $i < count($args); $i++) {
73+
$newArgs[] = $args[$i];
74+
}
75+
return $newArgs;
76+
}
77+
5778
}

tests/Rules/PHPUnit/AssertSameNullExpectedRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ public function testRule(): void
3434
]);
3535
}
3636

37+
public function testFix(): void
38+
{
39+
$this->fix(__DIR__ . '/data/assert-same-null-expected-fixable.php', __DIR__ . '/data/assert-same-null-expected-fixable.php.fixed');
40+
}
41+
3742
/**
3843
* @return string[]
3944
*/
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace AssertSameNullTestCaseFix;
4+
5+
class AssertSameNullExpectedTestCase extends \PHPUnit\Framework\TestCase
6+
{
7+
/**
8+
* @return null
9+
*/
10+
public function returnNull()
11+
{
12+
return null;
13+
}
14+
15+
public function doFoo(): void
16+
{
17+
$this->assertSame(null, 'a');
18+
19+
\PHPUnit\Framework\Assert::assertSame($this->returnNull(), 'foo');
20+
}
21+
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace AssertSameNullTestCaseFix;
4+
5+
class AssertSameNullExpectedTestCase extends \PHPUnit\Framework\TestCase
6+
{
7+
/**
8+
* @return null
9+
*/
10+
public function returnNull()
11+
{
12+
return null;
13+
}
14+
15+
public function doFoo(): void
16+
{
17+
$this->assertNull('a');
18+
19+
\PHPUnit\Framework\Assert::assertSame($this->returnNull(), 'foo');
20+
}
21+
22+
}

0 commit comments

Comments
 (0)