Skip to content

Commit ac3237e

Browse files
authored
Skip named and unpack arguments when autofixing
1 parent a31e9de commit ac3237e

10 files changed

Lines changed: 42 additions & 6 deletions

‎src/Rules/PHPUnit/AssertEqualsIsDiscouragedRule.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ public function processNode(Node $node, Scope $scope): array
7373
),
7474
)->identifier('phpunit.assertEquals')
7575
->fixNode($node, static function (CallLike $node) use ($correctName) {
76+
if (AssertRuleHelper::hasNamedOrUnpackedArguments($node)) {
77+
return $node;
78+
}
79+
7680
$node->name = new Node\Identifier($correctName);
7781

7882
return $node;

‎src/Rules/PHPUnit/AssertRuleHelper.php‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace PHPStan\Rules\PHPUnit;
44

55
use PhpParser\Node;
6+
use PhpParser\Node\Expr\CallLike;
67
use PHPStan\Analyser\Scope;
78
use PHPStan\Type\ObjectType;
89
use function in_array;
@@ -46,4 +47,15 @@ public static function isMethodOrStaticCallOnAssert(Node $node, Scope $scope): b
4647
return $testCaseType->isSuperTypeOf($calledOnType)->yes();
4748
}
4849

50+
public static function hasNamedOrUnpackedArguments(CallLike $call): bool
51+
{
52+
foreach ($call->getArgs() as $arg) {
53+
if ($arg->name !== null || $arg->unpack) {
54+
return true;
55+
}
56+
}
57+
58+
return false;
59+
}
60+
4961
}

‎src/Rules/PHPUnit/AssertSameBooleanExpectedRule.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ public function processNode(Node $node, Scope $scope): array
5151
RuleErrorBuilder::message('You should use assertTrue() instead of assertSame() when expecting "true"')
5252
->identifier('phpunit.assertTrue')
5353
->fixNode($node, static function (CallLike $node) {
54+
if (AssertRuleHelper::hasNamedOrUnpackedArguments($node)) {
55+
return $node;
56+
}
57+
5458
$node->name = new Node\Identifier('assertTrue');
5559
$node->args = self::rewriteArgs($node->args);
5660

‎src/Rules/PHPUnit/AssertSameNullExpectedRule.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ public function processNode(Node $node, Scope $scope): array
5151
RuleErrorBuilder::message('You should use assertNull() instead of assertSame(null, $actual).')
5252
->identifier('phpunit.assertNull')
5353
->fixNode($node, static function (CallLike $node) {
54+
if (AssertRuleHelper::hasNamedOrUnpackedArguments($node)) {
55+
return $node;
56+
}
57+
5458
$node->name = new Node\Identifier('assertNull');
5559
$node->args = self::rewriteArgs($node->args);
5660

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php // lint >= 8.0
22

33
declare(strict_types=1);
44

@@ -13,6 +13,8 @@ public function doFoo(string $s, string $t): void
1313
{
1414
$this->assertEquals('', $s);
1515
$this->assertNotEquals('', $t);
16+
$this->assertEquals(expected: '', actual: $s);
17+
$this->assertEquals('', $s, ...func_get_args());
1618
}
1719

1820
public function doFoo2(string $s, string $t): void

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php // lint >= 8.0
22

33
declare(strict_types=1);
44

@@ -13,6 +13,8 @@ class Foo extends TestCase
1313
{
1414
$this->assertSame('', $s);
1515
$this->assertNotSame('', $t);
16+
$this->assertEquals(expected: '', actual: $s);
17+
$this->assertEquals('', $s, ...func_get_args());
1618
}
1719

1820
public function doFoo2(string $s, string $t): void

‎tests/Rules/PHPUnit/data/assert-same-boolean-expected-fixable.php‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php declare(strict_types = 1);
1+
<?php // lint >= 8.0
22

33
namespace AssertSameBooleanTestCaseFix;
44

@@ -15,6 +15,8 @@ public function returnsBool(): bool
1515
public function doFoo(): void
1616
{
1717
$this->assertSame(true, $this->returnBool());
18+
$this->assertSame(expected: true, actual: $this->returnBool());
19+
$this->assertSame(true, ...func_get_args());
1820
self::assertSame(false, $this->returnBool());
1921
}
2022

‎tests/Rules/PHPUnit/data/assert-same-boolean-expected-fixable.php.fixed‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php declare(strict_types = 1);
1+
<?php // lint >= 8.0
22

33
namespace AssertSameBooleanTestCaseFix;
44

@@ -15,6 +15,8 @@ class AssertSameBooleanExpectedTestCase extends \PHPUnit\Framework\TestCase
1515
public function doFoo(): void
1616
{
1717
$this->assertTrue($this->returnBool());
18+
$this->assertSame(expected: true, actual: $this->returnBool());
19+
$this->assertSame(true, ...func_get_args());
1820
self::assertFalse($this->returnBool());
1921
}
2022

‎tests/Rules/PHPUnit/data/assert-same-null-expected-fixable.php‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php declare(strict_types = 1);
1+
<?php // lint >= 8.0
22

33
namespace AssertSameNullTestCaseFix;
44

@@ -15,6 +15,8 @@ public function returnNull()
1515
public function doFoo(): void
1616
{
1717
$this->assertSame(null, 'a');
18+
$this->assertSame(expected: null, actual: 'named');
19+
$this->assertSame(null, ...func_get_args());
1820

1921
\PHPUnit\Framework\Assert::assertSame($this->returnNull(), 'foo');
2022
}

‎tests/Rules/PHPUnit/data/assert-same-null-expected-fixable.php.fixed‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php declare(strict_types = 1);
1+
<?php // lint >= 8.0
22

33
namespace AssertSameNullTestCaseFix;
44

@@ -15,6 +15,8 @@ class AssertSameNullExpectedTestCase extends \PHPUnit\Framework\TestCase
1515
public function doFoo(): void
1616
{
1717
$this->assertNull('a');
18+
$this->assertSame(expected: null, actual: 'named');
19+
$this->assertSame(null, ...func_get_args());
1820

1921
\PHPUnit\Framework\Assert::assertSame($this->returnNull(), 'foo');
2022
}

0 commit comments

Comments
 (0)