55use PhpParser \Node ;
66use PhpParser \Node \Stmt \ClassMethod ;
77use PhpParser \Node \Stmt \Function_ ;
8- use PhpParser \NodeFinder ;
98use PHPStan \Analyser \NameScope ;
109use PHPStan \Analyser \Scope ;
11- use PHPStan \DependencyInjection \AutowiredParameter ;
1210use PHPStan \DependencyInjection \RegisteredRule ;
1311use PHPStan \Node \CollectedDataNode ;
14- use PHPStan \Parser \Parser ;
15- use PHPStan \Parser \ParserErrorsException ;
1612use PHPStan \PhpDoc \TypeStringResolver ;
1713use PHPStan \Reflection \ReflectionProvider ;
1814use PHPStan \Rules \TooWideTypehints \OverridableMemberHelper ;
3228 * so that \$this is static. Its return type covers what it returns in all of them, they share one fix.
3329 *
3430 * @phpstan-import-type ReturnInfo from MissingReturnTypeFixer
35- * @phpstan-type Record array{key: string, traitMethod: string|null, class: string|null, overrides: list<string>, overridable: bool, returnInfo: array{values: string, void: bool, returnsValue: bool}|null, parentReturnType: string|false|null, parentNativeReturnType: string|false|null, nativeReturnType: string|null, fixedNatively: bool, errors: list<array{string, string, string|null}>, file: string, fileDescription: string, line: int, startPos: int}
31+ * @phpstan-type Record array{key: string, traitMethod: string|null, class: string|null, overrides: list<string>, overridable: bool, returnInfo: array{values: string, void: bool, returnsValue: bool}|null, parentReturnType: string|false|null, parentNativeReturnType: string|false|null, nativeReturnType: string|false| null, fixedNatively: bool, errors: list<array{string, string, string|null}>, file: string, fileDescription: string, line: int, startPos: int, endPos : int}
3632 * @implements Rule<CollectedDataNode>
3733 */
3834#[RegisteredRule(level: 6 )]
@@ -61,8 +57,6 @@ public function __construct(
6157 private OverridableMemberHelper $ overridableMemberHelper ,
6258 private TypeStringResolver $ typeStringResolver ,
6359 private ReflectionProvider $ reflectionProvider ,
64- #[AutowiredParameter(ref: '@defaultAnalysisParser ' )]
65- private Parser $ parser ,
6660 )
6761 {
6862 }
@@ -116,10 +110,7 @@ public function processNode(Node $node, Scope $scope): array
116110 $ builder ->tip ($ tip );
117111 }
118112 if ($ fix !== null ) {
119- $ functionNode = $ this ->findFunctionNode ($ record );
120- if ($ functionNode !== null ) {
121- $ builder ->fixNode ($ functionNode , $ fix , $ record ['file ' ]);
122- }
113+ $ builder ->fixNode (self ::createFunctionNode ($ record ), $ fix , $ record ['file ' ]);
123114 }
124115
125116 $ errors [] = $ builder ->build ();
@@ -153,6 +144,10 @@ private function getFix(string $key): ?callable
153144 private function createFix (string $ key ): ?callable
154145 {
155146 $ record = $ this ->records [$ key ];
147+ if ($ record ['nativeReturnType ' ] === false ) {
148+ return null ;
149+ }
150+
156151 $ traitMethod = $ record ['traitMethod ' ];
157152 // the classes using a trait might not be analysed
158153 if ($ traitMethod !== null && $ this ->isOnlyFilesAnalysis ) {
@@ -170,7 +165,7 @@ private function createFix(string $key): ?callable
170165 }
171166
172167 // the fixed return type has to be within the return types of the methods it overrides in every class
173- if ($ contextRecord ['parentReturnType ' ] === false || $ contextRecord ['parentNativeReturnType ' ] === false ) {
168+ if ($ contextRecord ['parentReturnType ' ] === false || $ contextRecord ['parentNativeReturnType ' ] === false || $ contextRecord [ ' nativeReturnType ' ] === false ) {
174169 return null ;
175170 }
176171 $ parentReturnType = $ this ->resolve ($ contextRecord ['parentReturnType ' ], $ contextRecord ['class ' ]);
@@ -210,6 +205,10 @@ private function createFix(string $key): ?callable
210205 return null ;
211206 }
212207
208+ if ($ overridingRecord ['nativeReturnType ' ] === false ) {
209+ return null ;
210+ }
211+
213212 $ overridingReturnInfo = $ this ->getReturnInfo ($ overridingKey );
214213 $ overridingNativeType = $ this ->resolve ($ overridingRecord ['nativeReturnType ' ], $ overridingRecord ['class ' ]);
215214 $ overridingNativeReturnInfo = $ overridingNativeType !== null ? $ this ->createNativeReturnInfo ($ overridingNativeType ) : null ;
@@ -243,17 +242,12 @@ private function createFix(string $key): ?callable
243242 return null ;
244243 }
245244
246- $ functionNode = $ this ->findFunctionNode ($ record );
247- if ($ functionNode === null ) {
248- return null ;
249- }
250-
251245 $ classReflection = $ record ['class ' ] !== null && $ this ->reflectionProvider ->hasClass ($ record ['class ' ])
252246 ? $ this ->reflectionProvider ->getClass ($ record ['class ' ])
253247 : null ;
254248
255249 return $ this ->fixer ->createFix (
256- $ functionNode ,
250+ $ this -> resolve ( $ record [ ' nativeReturnType ' ], $ record [ ' class ' ]) ,
257251 $ phpDocType ,
258252 $ nativeSourceType ,
259253 $ classReflection ,
@@ -320,23 +314,14 @@ private function resolve(?string $type, ?string $className): ?Type
320314 }
321315
322316 /**
317+ * The node the fix is made on, found in the parsed file by its position, see ReplacingNodeVisitor.
318+ * The records are made from the analysed nodes, so the file is not parsed again here.
319+ *
323320 * @param Record $record
324321 */
325- private function findFunctionNode (array $ record ): ClassMethod | Function_ | null
322+ private static function createFunctionNode (array $ record ): ClassMethod
326323 {
327- try {
328- $ stmts = $ this ->parser ->parseFile ($ record ['file ' ]);
329- } catch (ParserErrorsException ) {
330- return null ;
331- }
332-
333- $ startPos = $ record ['startPos ' ];
334- $ functionNode = (new NodeFinder ())->findFirst ($ stmts , static fn (Node $ node ): bool => ($ node instanceof ClassMethod || $ node instanceof Function_) && $ node ->getStartFilePos () === $ startPos );
335- if (!$ functionNode instanceof ClassMethod && !$ functionNode instanceof Function_) {
336- return null ;
337- }
338-
339- return $ functionNode ;
324+ return new ClassMethod ('fixed ' , attributes: ['startFilePos ' => $ record ['startPos ' ], 'endFilePos ' => $ record ['endPos ' ]]);
340325 }
341326
342327}
0 commit comments