|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Rules\Methods; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PHPStan\Analyser\Scope; |
| 7 | +use PHPStan\Node\InClassMethodNode; |
| 8 | +use PHPStan\Rules\MissingTypehintCheck; |
| 9 | +use PHPStan\Rules\Rule; |
| 10 | +use PHPStan\Rules\RuleErrorBuilder; |
| 11 | +use PHPStan\Type\VerbosityLevel; |
| 12 | +use function implode; |
| 13 | +use function sprintf; |
| 14 | + |
| 15 | +/** |
| 16 | + * @implements Rule<InClassMethodNode> |
| 17 | + */ |
| 18 | +final class MissingMethodSelfOutTypeRule implements Rule |
| 19 | +{ |
| 20 | + |
| 21 | + public function __construct( |
| 22 | + private MissingTypehintCheck $missingTypehintCheck, |
| 23 | + ) |
| 24 | + { |
| 25 | + } |
| 26 | + |
| 27 | + public function getNodeType(): string |
| 28 | + { |
| 29 | + return InClassMethodNode::class; |
| 30 | + } |
| 31 | + |
| 32 | + public function processNode(Node $node, Scope $scope): array |
| 33 | + { |
| 34 | + $methodReflection = $node->getMethodReflection(); |
| 35 | + $selfOutType = $methodReflection->getSelfOutType(); |
| 36 | + |
| 37 | + if ($selfOutType === null) { |
| 38 | + return []; |
| 39 | + } |
| 40 | + |
| 41 | + $classReflection = $methodReflection->getDeclaringClass(); |
| 42 | + $phpDocTagMessage = 'PHPDoc tag @phpstan-self-out'; |
| 43 | + |
| 44 | + $messages = []; |
| 45 | + foreach ($this->missingTypehintCheck->getIterableTypesWithMissingValueTypehint($selfOutType) as $iterableType) { |
| 46 | + $iterableTypeDescription = $iterableType->describe(VerbosityLevel::typeOnly()); |
| 47 | + $messages[] = RuleErrorBuilder::message(sprintf( |
| 48 | + 'Method %s::%s() has %s with no value type specified in iterable type %s.', |
| 49 | + $classReflection->getDisplayName(), |
| 50 | + $methodReflection->getName(), |
| 51 | + $phpDocTagMessage, |
| 52 | + $iterableTypeDescription, |
| 53 | + )) |
| 54 | + ->tip(MissingTypehintCheck::MISSING_ITERABLE_VALUE_TYPE_TIP) |
| 55 | + ->identifier('missingType.iterableValue') |
| 56 | + ->build(); |
| 57 | + } |
| 58 | + |
| 59 | + foreach ($this->missingTypehintCheck->getNonGenericObjectTypesWithGenericClass($selfOutType) as [$name, $genericTypeNames]) { |
| 60 | + $messages[] = RuleErrorBuilder::message(sprintf( |
| 61 | + 'Method %s::%s() has %s with generic %s but does not specify its types: %s', |
| 62 | + $classReflection->getDisplayName(), |
| 63 | + $methodReflection->getName(), |
| 64 | + $phpDocTagMessage, |
| 65 | + $name, |
| 66 | + implode(', ', $genericTypeNames), |
| 67 | + )) |
| 68 | + ->identifier('missingType.generics') |
| 69 | + ->build(); |
| 70 | + } |
| 71 | + |
| 72 | + foreach ($this->missingTypehintCheck->getCallablesWithMissingSignature($selfOutType) as $callableType) { |
| 73 | + $messages[] = RuleErrorBuilder::message(sprintf( |
| 74 | + 'Method %s::%s() has %s with no signature specified for %s.', |
| 75 | + $classReflection->getDisplayName(), |
| 76 | + $methodReflection->getName(), |
| 77 | + $phpDocTagMessage, |
| 78 | + $callableType->describe(VerbosityLevel::typeOnly()), |
| 79 | + ))->identifier('missingType.callable')->build(); |
| 80 | + } |
| 81 | + |
| 82 | + return $messages; |
| 83 | + } |
| 84 | + |
| 85 | +} |
0 commit comments