1919use PhpParser \Node \Stmt \Expression ;
2020use PhpParser \Node \Stmt \Property ;
2121use PHPStan \Type \ObjectType ;
22+ use Rector \PhpParser \Node \BetterNodeFinder ;
2223use Rector \PhpParser \NodeFinder \PropertyFetchFinder ;
2324use Rector \PHPUnit \CodeQuality \NodeAnalyser \StubPropertyResolver ;
2425use Rector \PHPUnit \CodeQuality \NodeFinder \PropertyFetchUsageFinder ;
@@ -39,6 +40,7 @@ public function __construct(
3940 private readonly PropertyFetchFinder $ propertyFetchFinder ,
4041 private readonly PropertyFetchUsageFinder $ propertyFetchUsageFinder ,
4142 private readonly StubPropertyResolver $ stubPropertyResolver ,
43+ private readonly BetterNodeFinder $ betterNodeFinder ,
4244 ) {
4345 }
4446
@@ -149,7 +151,32 @@ public function refactor(Node $node): ?Node
149151
150152 // 3. replace property fetch calls, with createStub()
151153 $ stubClassName = $ propertyNamesToStubClasses [$ propertyName ];
152- $ this ->traverseNodesWithCallable ($ node ->getMethods (), function (Node $ node ) use (
154+ foreach ($ node ->getMethods () as $ classMethod ) {
155+ $ this ->refactorClassMethod ($ classMethod , $ propertyName , $ stubClassName );
156+ }
157+ }
158+
159+ if ($ hasChanged === false ) {
160+ return null ;
161+ }
162+
163+ return $ node ;
164+ }
165+
166+ private function refactorClassMethod (ClassMethod $ classMethod , string $ propertyName , string $ stubClassName ): void
167+ {
168+ $ propertyFetches = $ this ->betterNodeFinder ->find (
169+ (array ) $ classMethod ->stmts ,
170+ fn (Node $ node ): bool => $ node instanceof PropertyFetch && $ this ->isName ($ node ->name , $ propertyName )
171+ );
172+
173+ if ($ propertyFetches === []) {
174+ return ;
175+ }
176+
177+ // single use → inline directly
178+ if (count ($ propertyFetches ) === 1 ) {
179+ $ this ->traverseNodesWithCallable ($ classMethod , function (Node $ node ) use (
153180 $ propertyName ,
154181 $ stubClassName
155182 ): ?MethodCall {
@@ -161,19 +188,74 @@ public function refactor(Node $node): ?Node
161188 return null ;
162189 }
163190
164- $ classConstFetch = new ClassConstFetch (new FullyQualified ($ stubClassName ), 'class ' );
165-
166- return new MethodCall (new Variable ('this ' ), new Identifier ('createStub ' ), [
167- new Arg ($ classConstFetch ),
168- ]);
191+ return $ this ->createCreateStubMethodCall ($ stubClassName );
169192 });
193+
194+ return ;
170195 }
171196
172- if ($ hasChanged === false ) {
173- return null ;
197+ // multiple uses → assign to a local variable above first use and reuse it
198+ $ variableName = $ this ->resolveVariableName ($ propertyName );
199+
200+ $ this ->traverseNodesWithCallable ($ classMethod , function (Node $ node ) use (
201+ $ propertyName ,
202+ $ variableName
203+ ): ?Variable {
204+ if (! $ node instanceof PropertyFetch) {
205+ return null ;
206+ }
207+
208+ if (! $ this ->isName ($ node ->name , $ propertyName )) {
209+ return null ;
210+ }
211+
212+ return new Variable ($ variableName );
213+ });
214+
215+ $ assignExpression = new Expression (
216+ new Assign (new Variable ($ variableName ), $ this ->createCreateStubMethodCall ($ stubClassName ))
217+ );
218+
219+ $ this ->insertBeforeFirstVariableUse ($ classMethod , $ variableName , $ assignExpression );
220+ }
221+
222+ private function insertBeforeFirstVariableUse (
223+ ClassMethod $ classMethod ,
224+ string $ variableName ,
225+ Expression $ assignExpression
226+ ): void {
227+ $ stmts = (array ) $ classMethod ->stmts ;
228+
229+ foreach ($ stmts as $ key => $ stmt ) {
230+ $ firstVariable = $ this ->betterNodeFinder ->findFirst (
231+ $ stmt ,
232+ fn (Node $ node ): bool => $ node instanceof Variable && $ this ->isName ($ node , $ variableName )
233+ );
234+
235+ if (! $ firstVariable instanceof Variable) {
236+ continue ;
237+ }
238+
239+ array_splice ($ stmts , $ key , 0 , [$ assignExpression ]);
240+ $ classMethod ->stmts = $ stmts ;
241+ return ;
174242 }
243+ }
175244
176- return $ node ;
245+ private function createCreateStubMethodCall (string $ stubClassName ): MethodCall
246+ {
247+ $ classConstFetch = new ClassConstFetch (new FullyQualified ($ stubClassName ), 'class ' );
248+
249+ return new MethodCall (new Variable ('this ' ), new Identifier ('createStub ' ), [new Arg ($ classConstFetch )]);
250+ }
251+
252+ private function resolveVariableName (string $ propertyName ): string
253+ {
254+ if (str_ends_with (strtolower ($ propertyName ), 'stub ' )) {
255+ return $ propertyName ;
256+ }
257+
258+ return $ propertyName . 'Stub ' ;
177259 }
178260
179261 private function removeSetupPropertyFetchByPropertyName (ClassMethod $ setUpClassMethod , string $ propertyName ): void
0 commit comments