Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9a9b161

Browse files
Make AssertEqualsIsDiscouragedRule auto-fixable
1 parent 2e07610 commit 9a9b161

File tree

4 files changed

+68
-2
lines changed

4 files changed

+68
-2
lines changed

‎src/Rules/PHPUnit/AssertEqualsIsDiscouragedRule.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,27 @@ public function processNode(Node $node, Scope $scope): array
6060
&& ($leftType->isSuperTypeOf($rightType)->yes())
6161
&& ($rightType->isSuperTypeOf($leftType)->yes())
6262
) {
63+
$correctName = strtolower($node->name->name) === 'assertnotequals' ? 'assertNotSame' : 'assertSame';
6364
return [
6465
RuleErrorBuilder::message(
6566
sprintf(
6667
'You should use %s() instead of %s(), because both values are scalars of the same type',
67-
strtolower($node->name->name) === 'assertnotequals' ? 'assertNotSame' : 'assertSame',
68+
$correctName,
6869
$node->name->name,
6970
),
70-
)->identifier('phpunit.assertEquals')->build(),
71+
)->identifier('phpunit.assertEquals')
72+
->fixNode($node, static function (CallLike $node) use ($correctName) {
73+
if ($node instanceof Node\Expr\MethodCall) {
74+
$node->name = new Node\Identifier($correctName);
75+
}
76+
77+
if ($node instanceof Node\Expr\StaticCall) {
78+
$node->name = new Node\Identifier($correctName);
79+
}
80+
81+
return $node;
82+
})
83+
->build(),
7184
];
7285
}
7386

‎tests/Rules/PHPUnit/AssertEqualsIsDiscouragedRuleTest.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-equals-is-discouraged-fixable.php', __DIR__ . '/data/assert-equals-is-discouraged-fixable.php.fixed');
40+
}
41+
3742
protected function getRule(): Rule
3843
{
3944
return new AssertEqualsIsDiscouragedRule();
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SameAssertEqualsTestFix;
6+
7+
use PHPUnit\Framework\TestCase;
8+
9+
class Foo extends TestCase
10+
{
11+
12+
public function doFoo(string $s, string $t): void
13+
{
14+
$this->assertEquals('', $s);
15+
$this->assertNotEquals('', $t);
16+
}
17+
18+
public function doFoo2(string $s, string $t): void
19+
{
20+
self::assertEquals('', $s);
21+
self::assertNotEquals('', $t);
22+
}
23+
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SameAssertEqualsTestFix;
6+
7+
use PHPUnit\Framework\TestCase;
8+
9+
class Foo extends TestCase
10+
{
11+
12+
public function doFoo(string $s, string $t): void
13+
{
14+
$this->assertSame('', $s);
15+
$this->assertNotSame('', $t);
16+
}
17+
18+
public function doFoo2(string $s, string $t): void
19+
{
20+
self::assertSame('', $s);
21+
self::assertNotSame('', $t);
22+
}
23+
24+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /