@@ -50,6 +50,17 @@ public function processNode(Node $node, Scope $scope): array
5050 return [
5151 RuleErrorBuilder::message ('You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, count($variable)). ' )
5252 ->identifier ('phpunit.assertCount ' )
53+ ->fixNode ($ node , static function (CallLike $ node ) use ($ scope ) {
54+ $ newArgs = self ::rewriteArgs ($ node ->args , $ scope );
55+ if ($ newArgs === null ) {
56+ return $ node ;
57+ }
58+
59+ $ node ->name = new Node \Identifier ('assertCount ' );
60+ $ node ->args = $ newArgs ;
61+
62+ return $ node ;
63+ })
5364 ->build (),
5465 ];
5566 }
@@ -58,6 +69,12 @@ public function processNode(Node $node, Scope $scope): array
5869 return [
5970 RuleErrorBuilder::message ('You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, $variable->count()). ' )
6071 ->identifier ('phpunit.assertCount ' )
72+ ->fixNode ($ node , static function (CallLike $ node ) use ($ scope ) {
73+ $ node ->name = new Node \Identifier ('assertCount ' );
74+ $ node ->args = self ::rewriteArgs ($ node ->args , $ scope );
75+
76+ return $ node ;
77+ })
6178 ->build (),
6279 ];
6380 }
@@ -109,4 +126,36 @@ private static function isNormalCount(Node\Expr\FuncCall $countFuncCall, Type $c
109126 return $ isNormalCount ;
110127 }
111128
129+ /**
130+ * @param array<Node\Arg|Node\VariadicPlaceholder> $args
131+ * @return list<Node\Arg|Node\VariadicPlaceholder>
132+ */
133+ private static function rewriteArgs (array $ args , Scope $ scope ): ?array
134+ {
135+ $ newArgs = [];
136+ for ($ i = 0 ; $ i < count ($ args ); $ i ++) {
137+
138+ if (
139+ $ args [$ i ] instanceof Node \Arg
140+ && $ args [$ i ]->value instanceof CallLike
141+ ) {
142+ $ value = $ args [$ i ]->value ;
143+ if (self ::isCountFunctionCall ($ value , $ scope )) {
144+ if (count ($ value ->getArgs ()) !== 1 ) {
145+ return null ;
146+ }
147+
148+ $ newArgs [] = new Node \Arg ($ value ->getArgs ()[0 ]->value );
149+ continue ;
150+ } elseif (self ::isCountableMethodCall ($ value , $ scope )) {
151+ $ newArgs [] = new Node \Arg ($ value ->var );
152+ continue ;
153+ }
154+ }
155+
156+ $ newArgs [] = $ args [$ i ];
157+ }
158+ return $ newArgs ;
159+ }
160+
112161}
0 commit comments