Skip to content

Commit b47d200

Browse files
committed
skip named args
1 parent b0f9a78 commit b47d200

3 files changed

Lines changed: 45 additions & 2 deletions

File tree

‎src/Rules/PHPUnit/AssertSameWithCountRule.php‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Countable;
66
use PhpParser\Node;
77
use PhpParser\Node\Expr\CallLike;
8+
use PhpParser\NodeAbstract;
89
use PHPStan\Analyser\Scope;
910
use PHPStan\Rules\Rule;
1011
use PHPStan\Rules\RuleErrorBuilder;
@@ -40,6 +41,11 @@ public function processNode(Node $node, Scope $scope): array
4041
if (!$node->name instanceof Node\Identifier || $node->name->toLowerString() !== 'assertsame') {
4142
return [];
4243
}
44+
foreach ($node->getArgs() as $arg) {
45+
if ($arg->name !== null) {
46+
return [];
47+
}
48+
}
4349

4450
if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) {
4551
return [];
@@ -132,8 +138,9 @@ private static function isNormalCount(Node\Expr\FuncCall $countFuncCall, Type $c
132138
}
133139

134140
/**
135-
* @param array<Node\Arg|Node\VariadicPlaceholder> $args
136-
* @return list<Node\Arg|Node\VariadicPlaceholder>
141+
* @template T of NodeAbstract
142+
* @param array<T> $args
143+
* @return list<T|Node\Arg>
137144
*/
138145
private static function rewriteArgs(array $args, Scope $scope): ?array
139146
{

‎tests/Rules/PHPUnit/AssertSameWithCountRuleTest.php‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PHPStan\Rules\Rule;
66
use PHPStan\Testing\RuleTestCase;
7+
use const PHP_VERSION_ID;
78

89
/**
910
* @extends RuleTestCase<AssertSameWithCountRule>
@@ -47,6 +48,15 @@ public function testFix(): void
4748
$this->fix(__DIR__ . '/data/assert-same-count-fixable.php', __DIR__ . '/data/assert-same-count-fixable.php.fixed');
4849
}
4950

51+
public function testNamedArguments(): void
52+
{
53+
if (PHP_VERSION_ID < 80000) {
54+
self::markTestSkipped('Named arguments require PHP 8.0.');
55+
}
56+
57+
$this->analyse([__DIR__ . '/data/assert-same-count-named-arguments.php'], []);
58+
}
59+
5060
/**
5161
* @return string[]
5262
*/
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php // lint >= 8.0
2+
3+
namespace ExampleTestCaseFixNamedArguments;
4+
5+
class AssertSameWithCountTestCase extends \PHPUnit\Framework\TestCase
6+
{
7+
8+
public function skipNamedArguments(Bar $bar): void
9+
{
10+
$this->assertSame(expected: 5, actual: count([1, 2, 3]), message: 'message');
11+
$this->assertSame(message: 'message', actual: count(value: [1, 2, 3]), expected: 5);
12+
self::assertSame(actual: $bar->count(), expected: 5);
13+
$this->assertSame(5, actual: count([1, 2, 3]), message: 'message');
14+
}
15+
16+
}
17+
18+
class Bar implements \Countable
19+
{
20+
21+
public function count(): int
22+
{
23+
return 1;
24+
}
25+
26+
}

0 commit comments

Comments
 (0)