@@ -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,17 @@ 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+ $ newArgs = self ::rewriteArgs ($ node ->args , $ scope );
74+ if ($ newArgs === null ) {
75+ return $ node ;
76+ }
77+
78+ $ node ->name = new Node \Identifier ('assertCount ' );
79+ $ node ->args = $ newArgs ;
80+
81+ return $ node ;
82+ })
6183 ->build (),
6284 ];
6385 }
@@ -109,4 +131,36 @@ private static function isNormalCount(Node\Expr\FuncCall $countFuncCall, Type $c
109131 return $ isNormalCount ;
110132 }
111133
134+ /**
135+ * @param array<Node\Arg|Node\VariadicPlaceholder> $args
136+ * @return list<Node\Arg|Node\VariadicPlaceholder>
137+ */
138+ private static function rewriteArgs (array $ args , Scope $ scope ): ?array
139+ {
140+ $ newArgs = [];
141+ for ($ i = 0 ; $ i < count ($ args ); $ i ++) {
142+
143+ if (
144+ $ args [$ i ] instanceof Node \Arg
145+ && $ args [$ i ]->value instanceof CallLike
146+ ) {
147+ $ value = $ args [$ i ]->value ;
148+ if (self ::isCountFunctionCall ($ value , $ scope )) {
149+ if (count ($ value ->getArgs ()) !== 1 ) {
150+ return null ;
151+ }
152+
153+ $ newArgs [] = new Node \Arg ($ value ->getArgs ()[0 ]->value );
154+ continue ;
155+ } elseif (self ::isCountableMethodCall ($ value , $ scope )) {
156+ $ newArgs [] = new Node \Arg ($ value ->var );
157+ continue ;
158+ }
159+ }
160+
161+ $ newArgs [] = $ args [$ i ];
162+ }
163+ return $ newArgs ;
164+ }
165+
112166}
0 commit comments