2121 * - Check if the types of the parameters match the expected types.
2222 * - If an object type is expected, it can match a specific class or a pattern.
2323 * - Supports union types with "oneOf" (one type must match) and "allOf" (all types must match).
24+ *
25+ * @implements Rule<Class_>
2426 */
2527class MethodMustReturnTypeRule implements Rule
2628{
@@ -60,7 +62,7 @@ public function getNodeType(): string
6062 /**
6163 * @param Class_ $node
6264 * @param Scope $scope
63- * @return array
65+ * @return array<\PHPStan\Rules\RuleError>
6466 */
6567 public function processNode (Node $ node , Scope $ scope ): array
6668 {
@@ -143,8 +145,8 @@ public function processNode(Node $node, Scope $scope): array
143145 /**
144146 * Normalize configuration with defaults
145147 *
146- * @param array $config
147- * @return array
148+ * @param array<string, mixed> $config
149+ * @return array<string, mixed>
148150 */
149151 private function normalizeConfig (array $ config ): array
150152 {
@@ -163,12 +165,15 @@ private function normalizeConfig(array $config): array
163165 return $ normalized ;
164166 }
165167
168+ /**
169+ * @param array<string, mixed> $patternConfig
170+ */
166171 private function shouldErrorOnVoid (array $ patternConfig , ?string $ returnType ): bool
167172 {
168173 return $ patternConfig ['void ' ] && $ returnType !== 'void ' ;
169174 }
170175
171- private function buildVoidError (string $ fullName , int $ line )
176+ private function buildVoidError (string $ fullName , int $ line ): \ PHPStan \ Rules \ RuleError
172177 {
173178 return RuleErrorBuilder::message (
174179 sprintf (
@@ -186,6 +191,9 @@ private function shouldErrorOnMissingReturnType(?string $returnType): bool
186191 return $ returnType === null ;
187192 }
188193
194+ /**
195+ * @param array<string, mixed> $patternConfig
196+ */
189197 private function getExpectedTypeDescription (array $ patternConfig ): string
190198 {
191199 if (isset ($ patternConfig ['oneOf ' ])) {
@@ -197,7 +205,7 @@ private function getExpectedTypeDescription(array $patternConfig): string
197205 return $ patternConfig ['type ' ] ?? 'void ' ;
198206 }
199207
200- private function buildMissingReturnTypeError (string $ fullName , string $ expectedType , int $ line )
208+ private function buildMissingReturnTypeError (string $ fullName , string $ expectedType , int $ line ): \ PHPStan \ Rules \ RuleError
201209 {
202210 return RuleErrorBuilder::message (
203211 sprintf (
@@ -211,12 +219,15 @@ private function buildMissingReturnTypeError(string $fullName, string $expectedT
211219 ->build ();
212220 }
213221
222+ /**
223+ * @param array<string, mixed> $patternConfig
224+ */
214225 private function shouldErrorOnNullability (array $ patternConfig , bool $ isNullable ): bool
215226 {
216227 return $ patternConfig ['nullable ' ] !== $ isNullable ;
217228 }
218229
219- private function buildNullabilityError (string $ fullName , bool $ expectedNullable , int $ line )
230+ private function buildNullabilityError (string $ fullName , bool $ expectedNullable , int $ line ): \ PHPStan \ Rules \ RuleError
220231 {
221232 return RuleErrorBuilder::message (
222233 sprintf (
@@ -230,6 +241,9 @@ private function buildNullabilityError(string $fullName, bool $expectedNullable,
230241 ->build ();
231242 }
232243
244+ /**
245+ * @param array<string> $expectedTypes
246+ */
233247 private function shouldErrorOnOneOf (array $ expectedTypes , ?string $ returnType ): bool
234248 {
235249 if ($ returnType === null ) {
@@ -245,7 +259,10 @@ private function shouldErrorOnOneOf(array $expectedTypes, ?string $returnType):
245259 return true ;
246260 }
247261
248- private function buildOneOfError (string $ fullName , array $ expectedTypes , ?string $ actualType , int $ line )
262+ /**
263+ * @param array<string> $expectedTypes
264+ */
265+ private function buildOneOfError (string $ fullName , array $ expectedTypes , ?string $ actualType , int $ line ): \PHPStan \Rules \RuleError
249266 {
250267 return RuleErrorBuilder::message (
251268 sprintf (
@@ -260,6 +277,9 @@ private function buildOneOfError(string $fullName, array $expectedTypes, ?string
260277 ->build ();
261278 }
262279
280+ /**
281+ * @param array<string> $expectedTypes
282+ */
263283 private function shouldErrorOnAllOf (array $ expectedTypes , ?string $ returnType ): bool
264284 {
265285 if ($ returnType === null ) {
@@ -286,7 +306,10 @@ private function shouldErrorOnAllOf(array $expectedTypes, ?string $returnType):
286306 return false ;
287307 }
288308
289- private function buildAllOfError (string $ fullName , array $ expectedTypes , ?string $ actualType , int $ line )
309+ /**
310+ * @param array<string> $expectedTypes
311+ */
312+ private function buildAllOfError (string $ fullName , array $ expectedTypes , ?string $ actualType , int $ line ): \PHPStan \Rules \RuleError
290313 {
291314 return RuleErrorBuilder::message (
292315 sprintf (
@@ -301,6 +324,9 @@ private function buildAllOfError(string $fullName, array $expectedTypes, ?string
301324 ->build ();
302325 }
303326
327+ /**
328+ * @return array<string>
329+ */
304330 private function parseUnionType (?string $ type ): array
305331 {
306332 if ($ type === null ) {
@@ -354,13 +380,16 @@ private function isTypeMatch(?string $actual, string $expected): bool
354380 return false ;
355381 }
356382
383+ /**
384+ * @param array<string, mixed> $patternConfig
385+ */
357386 private function shouldErrorOnObjectTypePattern (array $ patternConfig , string $ objectType ): bool
358387 {
359388 return $ patternConfig ['objectTypePattern ' ] !== null &&
360389 !preg_match ($ patternConfig ['objectTypePattern ' ], $ objectType );
361390 }
362391
363- private function buildObjectTypePatternError (string $ fullName , string $ pattern , string $ objectType , int $ line )
392+ private function buildObjectTypePatternError (string $ fullName , string $ pattern , string $ objectType , int $ line ): \ PHPStan \ Rules \ RuleError
364393 {
365394 return RuleErrorBuilder::message (
366395 sprintf (
@@ -375,7 +404,7 @@ private function buildObjectTypePatternError(string $fullName, string $pattern,
375404 ->build ();
376405 }
377406
378- private function buildObjectTypeError (string $ fullName , int $ line )
407+ private function buildObjectTypeError (string $ fullName , int $ line ): \ PHPStan \ Rules \ RuleError
379408 {
380409 return RuleErrorBuilder::message (
381410 sprintf (
@@ -393,7 +422,7 @@ private function shouldErrorOnTypeMismatch(?string $returnType, string $expected
393422 return !$ this ->isTypeMatch ($ returnType , $ expectedType );
394423 }
395424
396- private function buildTypeMismatchError (string $ fullName , string $ expectedType , ?string $ actualType , int $ line )
425+ private function buildTypeMismatchError (string $ fullName , string $ expectedType , ?string $ actualType , int $ line ): \ PHPStan \ Rules \ RuleError
397426 {
398427 return RuleErrorBuilder::message (
399428 sprintf (
@@ -424,7 +453,7 @@ private function getTypeAsString(mixed $type): ?string
424453 };
425454 }
426455
427- private function isNullableType ($ type ): bool
456+ private function isNullableType (mixed $ type ): bool
428457 {
429458 return $ type instanceof NullableType;
430459 }
0 commit comments