|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Rules\PhpDoc; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PhpParser\Node\Expr\Variable; |
| 7 | +use PhpParser\Node\FunctionLike; |
| 8 | +use PHPStan\Analyser\Scope; |
| 9 | +use PHPStan\Rules\Rule; |
| 10 | +use PHPStan\Rules\RuleErrorBuilder; |
| 11 | +use PHPStan\ShouldNotHappenException; |
| 12 | +use PHPStan\Type\FileTypeMapper; |
| 13 | +use PHPStan\Type\VerbosityLevel; |
| 14 | +use function is_string; |
| 15 | +use function sprintf; |
| 16 | +use function trim; |
| 17 | + |
| 18 | +/** |
| 19 | + * @implements Rule<FunctionLike> |
| 20 | + */ |
| 21 | +final class IncompatibleParamImmediatelyInvokedCallableRule implements Rule |
| 22 | +{ |
| 23 | + |
| 24 | + public function __construct( |
| 25 | + private FileTypeMapper $fileTypeMapper, |
| 26 | + ) |
| 27 | + { |
| 28 | + } |
| 29 | + |
| 30 | + public function getNodeType(): string |
| 31 | + { |
| 32 | + return FunctionLike::class; |
| 33 | + } |
| 34 | + |
| 35 | + public function processNode(Node $node, Scope $scope): array |
| 36 | + { |
| 37 | + if ($node instanceof Node\Stmt\ClassMethod) { |
| 38 | + $functionName = $node->name->name; |
| 39 | + } elseif ($node instanceof Node\Stmt\Function_) { |
| 40 | + $functionName = trim($scope->getNamespace() . '\\' . $node->name->name, '\\'); |
| 41 | + } else { |
| 42 | + return []; |
| 43 | + } |
| 44 | + |
| 45 | + $docComment = $node->getDocComment(); |
| 46 | + if ($docComment === null) { |
| 47 | + return []; |
| 48 | + } |
| 49 | + |
| 50 | + $resolvedPhpDoc = $this->fileTypeMapper->getResolvedPhpDoc( |
| 51 | + $scope->getFile(), |
| 52 | + $scope->isInClass() ? $scope->getClassReflection()->getName() : null, |
| 53 | + $scope->isInTrait() ? $scope->getTraitReflection()->getName() : null, |
| 54 | + $functionName, |
| 55 | + $docComment->getText(), |
| 56 | + ); |
| 57 | + $nativeParameterTypes = []; |
| 58 | + foreach ($node->getParams() as $parameter) { |
| 59 | + if (!$parameter->var instanceof Variable || !is_string($parameter->var->name)) { |
| 60 | + throw new ShouldNotHappenException(); |
| 61 | + } |
| 62 | + $nativeParameterTypes[$parameter->var->name] = $scope->getFunctionType( |
| 63 | + $parameter->type, |
| 64 | + $scope->isParameterValueNullable($parameter), |
| 65 | + false, |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + $errors = []; |
| 70 | + foreach ($resolvedPhpDoc->getParamsImmediatelyInvokedCallable() as $parameterName => $immediately) { |
| 71 | + $tagName = $immediately ? '@param-immediately-invoked-callable' : '@param-later-invoked-callable'; |
| 72 | + if (!isset($nativeParameterTypes[$parameterName])) { |
| 73 | + $errors[] = RuleErrorBuilder::message(sprintf( |
| 74 | + 'PHPDoc tag %s references unknown parameter: $%s', |
| 75 | + $tagName, |
| 76 | + $parameterName, |
| 77 | + ))->identifier('parameter.notFound')->build(); |
| 78 | + } elseif ($nativeParameterTypes[$parameterName]->isCallable()->no()) { |
| 79 | + $errors[] = RuleErrorBuilder::message(sprintf( |
| 80 | + 'PHPDoc tag %s is for parameter $%s with non-callable type %s.', |
| 81 | + $tagName, |
| 82 | + $parameterName, |
| 83 | + $nativeParameterTypes[$parameterName]->describe(VerbosityLevel::typeOnly()), |
| 84 | + ))->identifier(sprintf( |
| 85 | + '%s.nonCallable', |
| 86 | + $immediately ? 'paramImmediatelyInvokedCallable' : 'paramLaterInvokedCallable', |
| 87 | + ))->build(); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + return $errors; |
| 92 | + } |
| 93 | + |
| 94 | +} |
0 commit comments