99use PhpParser \Node ;
1010use PhpParser \Node \Arg ;
1111use PhpParser \Node \Expr ;
12+ use PhpParser \Node \Expr \ArrowFunction ;
1213use PhpParser \Node \Expr \Closure ;
1314use PhpParser \Node \Expr \MethodCall ;
1415use PhpParser \Node \Expr \PropertyFetch ;
@@ -66,13 +67,13 @@ public function createEmptyFactory(string $name, Expr $expr): Class_
6667 return $ class ;
6768 }
6869
69- public function createDefinition (Closure $ closure ): ClassMethod
70+ public function createDefinition (Closure | ArrowFunction $ callable ): ClassMethod
7071 {
71- if (isset ($ closure ->params [0 ])) {
72- $ this ->fakerVariableToPropertyFetch ($ closure -> stmts , $ closure ->params [0 ]);
72+ if (isset ($ callable ->params [0 ])) {
73+ $ this ->fakerVariableToPropertyFetch ($ callable -> getStmts () , $ callable ->params [0 ]);
7374 }
7475
75- return $ this ->createPublicMethod ('definition ' , $ closure -> stmts );
76+ return $ this ->createPublicMethod ('definition ' , $ callable -> getStmts () );
7677 }
7778
7879 public function createStateMethod (MethodCall $ methodCall ): ?ClassMethod
@@ -87,8 +88,8 @@ public function createStateMethod(MethodCall $methodCall): ?ClassMethod
8788
8889 $ thirdArgValue = $ methodCall ->args [2 ]->value ;
8990 // the third argument may be closure or array
90- if ($ thirdArgValue instanceof Closure && isset ($ thirdArgValue ->params [0 ])) {
91- $ this ->fakerVariableToPropertyFetch ($ thirdArgValue ->stmts , $ thirdArgValue ->params [0 ]);
91+ if (( $ thirdArgValue instanceof Closure || $ thirdArgValue instanceof ArrowFunction) && isset ($ thirdArgValue ->params [0 ])) {
92+ $ this ->fakerVariableToPropertyFetch ($ thirdArgValue ->getStmts () , $ thirdArgValue ->params [0 ]);
9293 unset($ thirdArgValue ->params [0 ]);
9394 }
9495
@@ -117,11 +118,11 @@ public function createEmptyConfigure(): ClassMethod
117118 return $ this ->createPublicMethod ('configure ' , [$ return ]);
118119 }
119120
120- public function appendConfigure (ClassMethod $ classMethod , string $ name , Closure $ closure ): void
121+ public function appendConfigure (ClassMethod $ classMethod , string $ name , Closure | ArrowFunction $ callable ): void
121122 {
122123 $ this ->simpleCallableNodeTraverser ->traverseNodesWithCallable (
123124 (array ) $ classMethod ->stmts ,
124- function (Node $ node ) use ($ closure , $ name ): ?Return_ {
125+ function (Node $ node ) use ($ callable , $ name ): ?Return_ {
125126 if (! $ node instanceof Return_) {
126127 return null ;
127128 }
@@ -130,13 +131,13 @@ function (Node $node) use ($closure, $name): ?Return_ {
130131 return null ;
131132 }
132133
133- if (isset ($ closure ->params [1 ])) {
134- $ this ->fakerVariableToPropertyFetch ($ closure -> stmts , $ closure ->params [1 ]);
134+ if (isset ($ callable ->params [1 ])) {
135+ $ this ->fakerVariableToPropertyFetch ($ callable -> getStmts () , $ callable ->params [1 ]);
135136 // remove argument $faker
136- unset($ closure ->params [1 ]);
137+ unset($ callable ->params [1 ]);
137138 }
138139
139- $ node ->expr = $ this ->nodeFactory ->createMethodCall ($ node ->expr , $ name , [$ closure ]);
140+ $ node ->expr = $ this ->nodeFactory ->createMethodCall ($ node ->expr , $ name , [$ callable ]);
140141
141142 return $ node ;
142143 }
0 commit comments