1212use PHPStan \Reflection \ParametersAcceptorSelector ;
1313use PHPStan \Reflection \Php \PhpMethodFromParserNodeReflection ;
1414use PHPStan \Rules \Rule ;
15+ use PHPStan \Rules \RuleError ;
1516use PHPStan \Rules \RuleErrorBuilder ;
1617use PHPStan \Type \ArrayType ;
1718use PHPStan \Type \IterableType ;
@@ -29,9 +30,19 @@ class OverridingMethodRule implements Rule
2930
3031 private PhpVersion $ phpVersion ;
3132
32- public function __construct (PhpVersion $ phpVersion )
33+ private MethodSignatureRule $ methodSignatureRule ;
34+
35+ private bool $ checkPhpDocMethodSignatures ;
36+
37+ public function __construct (
38+ PhpVersion $ phpVersion ,
39+ MethodSignatureRule $ methodSignatureRule ,
40+ bool $ checkPhpDocMethodSignatures
41+ )
3342 {
3443 $ this ->phpVersion = $ phpVersion ;
44+ $ this ->methodSignatureRule = $ methodSignatureRule ;
45+ $ this ->checkPhpDocMethodSignatures = $ checkPhpDocMethodSignatures ;
3546 }
3647
3748 public function getNodeType (): string
@@ -108,13 +119,13 @@ public function processNode(Node $node, Scope $scope): array
108119
109120 if (strtolower ($ method ->getName ()) === '__construct ' ) {
110121 if (!$ prototype ->isAbstract ()) {
111- return $ messages ;
122+ return $ this -> addErrors ( $ messages, $ node -> getOriginalNode (), $ scope ) ;
112123 }
113124 }
114125
115126 $ prototypeVariants = $ prototype ->getVariants ();
116127 if (count ($ prototypeVariants ) !== 1 ) {
117- return $ messages ;
128+ return $ this -> addErrors ( $ messages, $ node -> getOriginalNode (), $ scope ) ;
118129 }
119130
120131 $ prototypeVariant = $ prototypeVariants [0 ];
@@ -277,7 +288,7 @@ public function processNode(Node $node, Scope $scope): array
277288 $ methodReturnType = $ methodVariant ->getNativeReturnType ();
278289
279290 if (!$ prototypeVariant instanceof FunctionVariantWithPhpDocs) {
280- return $ messages ;
291+ return $ this -> addErrors ( $ messages, $ node -> getOriginalNode (), $ scope ) ;
281292 }
282293
283294 $ prototypeReturnType = $ prototypeVariant ->getNativeReturnType ();
@@ -306,7 +317,7 @@ public function processNode(Node $node, Scope $scope): array
306317 }
307318 }
308319
309- return $ messages ;
320+ return $ this -> addErrors ( $ messages, $ node -> getOriginalNode (), $ scope ) ;
310321 }
311322
312323 private function isTypeCompatible (Type $ methodParameterType , Type $ prototypeParameterType , bool $ supportsContravariance ): bool
@@ -339,4 +350,25 @@ private function isTypeCompatible(Type $methodParameterType, Type $prototypePara
339350 return $ methodParameterType ->isSuperTypeOf ($ prototypeParameterType )->yes ();
340351 }
341352
353+ /**
354+ * @param RuleError[] $errors
355+ * @return RuleError[]
356+ */
357+ private function addErrors (
358+ array $ errors ,
359+ Node \Stmt \ClassMethod $ classMethod ,
360+ Scope $ scope
361+ ): array
362+ {
363+ if (count ($ errors ) > 0 ) {
364+ return $ errors ;
365+ }
366+
367+ if (!$ this ->checkPhpDocMethodSignatures ) {
368+ return $ errors ;
369+ }
370+
371+ return $ this ->methodSignatureRule ->processNode ($ classMethod , $ scope );
372+ }
373+
342374}
0 commit comments