Skip to content

Commit 046688e

Browse files
committed
refactor
1 parent d155a48 commit 046688e

1 file changed

Lines changed: 26 additions & 14 deletions

File tree

‎src/Rules/PHPUnit/AssertSameWithCountRule.php‎

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,16 @@ public function processNode(Node $node, Scope $scope): array
4141
if (!$node->name instanceof Node\Identifier || $node->name->toLowerString() !== 'assertsame') {
4242
return [];
4343
}
44-
foreach ($node->getArgs() as $arg) {
45-
if ($arg->name !== null) {
46-
return [];
47-
}
44+
if (self::hasNamedArgs($node->getArgs())) {
45+
return [];
4846
}
4947

5048
if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) {
5149
return [];
5250
}
5351

5452
$right = $node->getArgs()[1]->value;
55-
if (self::isCountFunctionCall($right, $scope)) {
53+
if (self::isFixableCountFunctionCall($right, $scope)) {
5654
return [
5755
RuleErrorBuilder::message('You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, count($variable)).')
5856
->identifier('phpunit.assertCount')
@@ -71,7 +69,7 @@ public function processNode(Node $node, Scope $scope): array
7169
];
7270
}
7371

74-
if (self::isCountableMethodCall($right, $scope)) {
72+
if (self::isFixableCountableMethodCall($right, $scope)) {
7573
return [
7674
RuleErrorBuilder::message('You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, $variable->count()).')
7775
->identifier('phpunit.assertCount')
@@ -96,7 +94,7 @@ public function processNode(Node $node, Scope $scope): array
9694
/**
9795
* @phpstan-assert-if-true Node\Expr\FuncCall $expr
9896
*/
99-
private static function isCountFunctionCall(Node\Expr $expr, Scope $scope): bool
97+
private static function isFixableCountFunctionCall(Node\Expr $expr, Scope $scope): bool
10098
{
10199
if (!$expr instanceof Node\Expr\FuncCall
102100
|| !$expr->name instanceof Node\Name
@@ -106,10 +104,8 @@ private static function isCountFunctionCall(Node\Expr $expr, Scope $scope): bool
106104
}
107105

108106
$args = $expr->getArgs();
109-
foreach ($args as $arg) {
110-
if ($arg->name !== null) {
111-
return false;
112-
}
107+
if (self::hasNamedArgs($args)) {
108+
return false;
113109
}
114110

115111
return count($args) >= 1
@@ -119,7 +115,7 @@ private static function isCountFunctionCall(Node\Expr $expr, Scope $scope): bool
119115
/**
120116
* @phpstan-assert-if-true Node\Expr\MethodCall $expr
121117
*/
122-
private static function isCountableMethodCall(Node\Expr $expr, Scope $scope): bool
118+
private static function isFixableCountableMethodCall(Node\Expr $expr, Scope $scope): bool
123119
{
124120
if (
125121
$expr instanceof Node\Expr\MethodCall
@@ -148,6 +144,20 @@ private static function isNormalCount(Node\Expr\FuncCall $countFuncCall, Type $c
148144
return $isNormalCount;
149145
}
150146

147+
/**
148+
* @param array<Node\Arg> $args
149+
*/
150+
private static function hasNamedArgs(array $args): bool
151+
{
152+
foreach ($args as $arg) {
153+
if ($arg->name !== null) {
154+
return true;
155+
}
156+
}
157+
158+
return false;
159+
}
160+
151161
/**
152162
* @template T of NodeAbstract
153163
* @param array<T> $args
@@ -163,17 +173,19 @@ private static function rewriteArgs(array $args, Scope $scope): ?array
163173
&& $args[$i]->value instanceof CallLike
164174
) {
165175
$value = $args[$i]->value;
166-
if (self::isCountFunctionCall($value, $scope)) {
176+
if (self::isFixableCountFunctionCall($value, $scope)) {
167177
if (count($value->getArgs()) !== 1) {
168178
return null;
169179
}
170180

171181
$newArgs[] = new Node\Arg($value->getArgs()[0]->value);
172182
continue;
173-
} elseif (self::isCountableMethodCall($value, $scope)) {
183+
} elseif (self::isFixableCountableMethodCall($value, $scope)) {
174184
$newArgs[] = new Node\Arg($value->var);
175185
continue;
176186
}
187+
188+
return null;
177189
}
178190

179191
$newArgs[] = $args[$i];

0 commit comments

Comments
 (0)