1313use PhpParser \Node \Expr \Instanceof_ ;
1414use PhpParser \Node \Expr \Isset_ ;
1515use PhpParser \Node \Expr \MethodCall ;
16+ use PhpParser \Node \Expr \StaticCall ;
1617use PhpParser \Node \Name ;
1718use PhpParser \Node \Name \FullyQualified ;
1819use PhpParser \Node \Scalar \Int_ ;
@@ -33,14 +34,14 @@ public function __construct(
3334 * @param Expr[] $exprs
3435 * @return Stmt[]
3536 */
36- public function create (array $ exprs ): array
37+ public function create (array $ exprs, bool $ isStaticClosure = false ): array
3738 {
3839 $ assertMethodCalls = [];
3940
4041 foreach ($ exprs as $ expr ) {
4142 // implicit bool compare
4243 if ($ expr instanceof MethodCall) {
43- $ assertMethodCalls [] = $ this ->nodeFactory -> createMethodCall ( ' this ' , 'assertTrue ' , [$ expr ]);
44+ $ assertMethodCalls [] = $ this ->createAssertMethodCall ( $ isStaticClosure , 'assertTrue ' , [$ expr ]);
4445
4546 continue ;
4647 }
@@ -51,8 +52,8 @@ public function create(array $exprs): array
5152 $ dimExpr = $ expr ->getArgs ()[0 ]
5253 ->value ;
5354
54- $ assertMethodCalls [] = $ this ->nodeFactory -> createMethodCall (
55- ' this ' ,
55+ $ assertMethodCalls [] = $ this ->createAssertMethodCall (
56+ $ isStaticClosure ,
5657 'assertArrayHasKey ' ,
5758 [$ dimExpr , $ variableExpr ]
5859 );
@@ -63,8 +64,8 @@ public function create(array $exprs): array
6364 if ($ expr instanceof Isset_) {
6465 foreach ($ expr ->vars as $ issetVariable ) {
6566 if ($ issetVariable instanceof ArrayDimFetch) {
66- $ assertMethodCalls [] = $ this ->nodeFactory -> createMethodCall (
67- ' this ' ,
67+ $ assertMethodCalls [] = $ this ->createAssertMethodCall (
68+ $ isStaticClosure ,
6869 'assertArrayHasKey ' ,
6970 [$ issetVariable ->dim , $ issetVariable ->var ]
7071 );
@@ -86,8 +87,8 @@ public function create(array $exprs): array
8687 $ classNameExpr = $ expr ->class ;
8788 }
8889
89- $ assertMethodCalls [] = $ this ->nodeFactory -> createMethodCall (
90- ' this ' ,
90+ $ assertMethodCalls [] = $ this ->createAssertMethodCall (
91+ $ isStaticClosure ,
9192 'assertInstanceOf ' ,
9293 [$ classNameExpr , $ expr ->expr ]
9394 );
@@ -102,8 +103,8 @@ public function create(array $exprs): array
102103 ->value ;
103104
104105 // create assertCount()
105- $ assertMethodCalls [] = $ this ->nodeFactory -> createMethodCall (
106- ' this ' ,
106+ $ assertMethodCalls [] = $ this ->createAssertMethodCall (
107+ $ isStaticClosure ,
107108 'assertCount ' ,
108109 [$ expr ->right , $ countedExpr ]
109110 );
@@ -116,8 +117,8 @@ public function create(array $exprs): array
116117 }
117118
118119 // create assertSame()
119- $ assertMethodCalls [] = $ this ->nodeFactory -> createMethodCall (
120- ' this ' ,
120+ $ assertMethodCalls [] = $ this ->createAssertMethodCall (
121+ $ isStaticClosure ,
121122 $ expr instanceof Identical ? 'assertSame ' : 'assertEquals ' ,
122123 [$ expr ->right , $ expr ->left ]
123124 );
@@ -141,4 +142,16 @@ public function create(array $exprs): array
141142
142143 return $ stmts ;
143144 }
145+
146+ /**
147+ * @param Expr[] $args
148+ */
149+ private function createAssertMethodCall (bool $ isStaticClosure , string $ method , array $ args ): MethodCall |StaticCall
150+ {
151+ if ($ isStaticClosure ) {
152+ return new StaticCall (new Name ('self ' ), $ method , $ this ->nodeFactory ->createArgs ($ args ));
153+ }
154+
155+ return $ this ->nodeFactory ->createMethodCall ('this ' , $ method , $ args );
156+ }
144157}
0 commit comments