diff --git a/src/Rules/PHPUnit/AssertEqualsIsDiscouragedRule.php b/src/Rules/PHPUnit/AssertEqualsIsDiscouragedRule.php index ed6470e..56be120 100644 --- a/src/Rules/PHPUnit/AssertEqualsIsDiscouragedRule.php +++ b/src/Rules/PHPUnit/AssertEqualsIsDiscouragedRule.php @@ -73,6 +73,10 @@ public function processNode(Node $node, Scope $scope): array ), )->identifier('phpunit.assertEquals') ->fixNode($node, static function (CallLike $node) use ($correctName) { + if (AssertRuleHelper::hasNamedOrUnpackedArguments($node)) { + return $node; + } + $node->name = new Node\Identifier($correctName); return $node; diff --git a/src/Rules/PHPUnit/AssertRuleHelper.php b/src/Rules/PHPUnit/AssertRuleHelper.php index ecaec91..5971fc0 100644 --- a/src/Rules/PHPUnit/AssertRuleHelper.php +++ b/src/Rules/PHPUnit/AssertRuleHelper.php @@ -3,6 +3,7 @@ namespace PHPStan\Rules\PHPUnit; use PhpParser\Node; +use PhpParser\Node\Expr\CallLike; use PHPStan\Analyser\Scope; use PHPStan\Type\ObjectType; use function in_array; @@ -46,4 +47,15 @@ public static function isMethodOrStaticCallOnAssert(Node $node, Scope $scope): b return $testCaseType->isSuperTypeOf($calledOnType)->yes(); } + public static function hasNamedOrUnpackedArguments(CallLike $call): bool + { + foreach ($call->getArgs() as $arg) { + if ($arg->name !== null || $arg->unpack) { + return true; + } + } + + return false; + } + } diff --git a/src/Rules/PHPUnit/AssertSameBooleanExpectedRule.php b/src/Rules/PHPUnit/AssertSameBooleanExpectedRule.php index 8907879..4954337 100644 --- a/src/Rules/PHPUnit/AssertSameBooleanExpectedRule.php +++ b/src/Rules/PHPUnit/AssertSameBooleanExpectedRule.php @@ -51,6 +51,10 @@ public function processNode(Node $node, Scope $scope): array RuleErrorBuilder::message('You should use assertTrue() instead of assertSame() when expecting "true"') ->identifier('phpunit.assertTrue') ->fixNode($node, static function (CallLike $node) { + if (AssertRuleHelper::hasNamedOrUnpackedArguments($node)) { + return $node; + } + $node->name = new Node\Identifier('assertTrue'); $node->args = self::rewriteArgs($node->args); diff --git a/src/Rules/PHPUnit/AssertSameNullExpectedRule.php b/src/Rules/PHPUnit/AssertSameNullExpectedRule.php index 2d5fda4..ee9e56b 100644 --- a/src/Rules/PHPUnit/AssertSameNullExpectedRule.php +++ b/src/Rules/PHPUnit/AssertSameNullExpectedRule.php @@ -51,6 +51,10 @@ public function processNode(Node $node, Scope $scope): array RuleErrorBuilder::message('You should use assertNull() instead of assertSame(null, $actual).') ->identifier('phpunit.assertNull') ->fixNode($node, static function (CallLike $node) { + if (AssertRuleHelper::hasNamedOrUnpackedArguments($node)) { + return $node; + } + $node->name = new Node\Identifier('assertNull'); $node->args = self::rewriteArgs($node->args); diff --git a/tests/Rules/PHPUnit/data/assert-equals-is-discouraged-fixable.php b/tests/Rules/PHPUnit/data/assert-equals-is-discouraged-fixable.php index 5c0c993..8f3bf25 100644 --- a/tests/Rules/PHPUnit/data/assert-equals-is-discouraged-fixable.php +++ b/tests/Rules/PHPUnit/data/assert-equals-is-discouraged-fixable.php @@ -1,4 +1,4 @@ -= 8.0 declare(strict_types=1); @@ -13,6 +13,8 @@ public function doFoo(string $s, string $t): void { $this->assertEquals('', $s); $this->assertNotEquals('', $t); + $this->assertEquals(expected: '', actual: $s); + $this->assertEquals('', $s, ...func_get_args()); } public function doFoo2(string $s, string $t): void diff --git a/tests/Rules/PHPUnit/data/assert-equals-is-discouraged-fixable.php.fixed b/tests/Rules/PHPUnit/data/assert-equals-is-discouraged-fixable.php.fixed index 9217e3e..3100e62 100644 --- a/tests/Rules/PHPUnit/data/assert-equals-is-discouraged-fixable.php.fixed +++ b/tests/Rules/PHPUnit/data/assert-equals-is-discouraged-fixable.php.fixed @@ -1,4 +1,4 @@ -= 8.0 declare(strict_types=1); @@ -13,6 +13,8 @@ class Foo extends TestCase { $this->assertSame('', $s); $this->assertNotSame('', $t); + $this->assertEquals(expected: '', actual: $s); + $this->assertEquals('', $s, ...func_get_args()); } public function doFoo2(string $s, string $t): void diff --git a/tests/Rules/PHPUnit/data/assert-same-boolean-expected-fixable.php b/tests/Rules/PHPUnit/data/assert-same-boolean-expected-fixable.php index 5d5a3ba..65ad146 100644 --- a/tests/Rules/PHPUnit/data/assert-same-boolean-expected-fixable.php +++ b/tests/Rules/PHPUnit/data/assert-same-boolean-expected-fixable.php @@ -1,4 +1,4 @@ -= 8.0 namespace AssertSameBooleanTestCaseFix; @@ -15,6 +15,8 @@ public function returnsBool(): bool public function doFoo(): void { $this->assertSame(true, $this->returnBool()); + $this->assertSame(expected: true, actual: $this->returnBool()); + $this->assertSame(true, ...func_get_args()); self::assertSame(false, $this->returnBool()); } diff --git a/tests/Rules/PHPUnit/data/assert-same-boolean-expected-fixable.php.fixed b/tests/Rules/PHPUnit/data/assert-same-boolean-expected-fixable.php.fixed index d0bb802..27ae08b 100644 --- a/tests/Rules/PHPUnit/data/assert-same-boolean-expected-fixable.php.fixed +++ b/tests/Rules/PHPUnit/data/assert-same-boolean-expected-fixable.php.fixed @@ -1,4 +1,4 @@ -= 8.0 namespace AssertSameBooleanTestCaseFix; @@ -15,6 +15,8 @@ class AssertSameBooleanExpectedTestCase extends \PHPUnit\Framework\TestCase public function doFoo(): void { $this->assertTrue($this->returnBool()); + $this->assertSame(expected: true, actual: $this->returnBool()); + $this->assertSame(true, ...func_get_args()); self::assertFalse($this->returnBool()); } diff --git a/tests/Rules/PHPUnit/data/assert-same-null-expected-fixable.php b/tests/Rules/PHPUnit/data/assert-same-null-expected-fixable.php index f95fadd..59bbea4 100644 --- a/tests/Rules/PHPUnit/data/assert-same-null-expected-fixable.php +++ b/tests/Rules/PHPUnit/data/assert-same-null-expected-fixable.php @@ -1,4 +1,4 @@ -= 8.0 namespace AssertSameNullTestCaseFix; @@ -15,6 +15,8 @@ public function returnNull() public function doFoo(): void { $this->assertSame(null, 'a'); + $this->assertSame(expected: null, actual: 'named'); + $this->assertSame(null, ...func_get_args()); \PHPUnit\Framework\Assert::assertSame($this->returnNull(), 'foo'); } diff --git a/tests/Rules/PHPUnit/data/assert-same-null-expected-fixable.php.fixed b/tests/Rules/PHPUnit/data/assert-same-null-expected-fixable.php.fixed index a3c9104..db4739a 100644 --- a/tests/Rules/PHPUnit/data/assert-same-null-expected-fixable.php.fixed +++ b/tests/Rules/PHPUnit/data/assert-same-null-expected-fixable.php.fixed @@ -1,4 +1,4 @@ -= 8.0 namespace AssertSameNullTestCaseFix; @@ -15,6 +15,8 @@ class AssertSameNullExpectedTestCase extends \PHPUnit\Framework\TestCase public function doFoo(): void { $this->assertNull('a'); + $this->assertSame(expected: null, actual: 'named'); + $this->assertSame(null, ...func_get_args()); \PHPUnit\Framework\Assert::assertSame($this->returnNull(), 'foo'); }