Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Rules/PHPUnit/AssertEqualsIsDiscouragedRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 12 additions & 0 deletions src/Rules/PHPUnit/AssertRuleHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

}
4 changes: 4 additions & 0 deletions src/Rules/PHPUnit/AssertSameBooleanExpectedRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 4 additions & 0 deletions src/Rules/PHPUnit/AssertSameNullExpectedRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php // lint >= 8.0

declare(strict_types=1);

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php // lint >= 8.0

declare(strict_types=1);

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php declare(strict_types = 1);
<?php // lint >= 8.0

namespace AssertSameBooleanTestCaseFix;

Expand All @@ -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());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php declare(strict_types = 1);
<?php // lint >= 8.0

namespace AssertSameBooleanTestCaseFix;

Expand All @@ -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());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php declare(strict_types = 1);
<?php // lint >= 8.0

namespace AssertSameNullTestCaseFix;

Expand All @@ -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');
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php declare(strict_types = 1);
<?php // lint >= 8.0

namespace AssertSameNullTestCaseFix;

Expand All @@ -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');
}
Expand Down
Loading