|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Rules\Pure; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PHPStan\Analyser\Scope; |
| 7 | +use PHPStan\Rules\Rule; |
| 8 | +use PHPStan\Rules\RuleErrorBuilder; |
| 9 | +use PHPStan\Type\FileTypeMapper; |
| 10 | +use function array_keys; |
| 11 | +use function sprintf; |
| 12 | +use function trim; |
| 13 | + |
| 14 | +/** |
| 15 | + * Reports a docblock that marks a function or method pure or impure and also |
| 16 | + * makes its purity conditional with a @pure-unless-* tag. Only the docblock |
| 17 | + * itself counts, not tags inherited from a parent or set on the class. |
| 18 | + * |
| 19 | + * @implements Rule<Node\FunctionLike> |
| 20 | + */ |
| 21 | +final class ConflictingPurityTagsRule implements Rule |
| 22 | +{ |
| 23 | + |
| 24 | + public function __construct(private FileTypeMapper $fileTypeMapper) |
| 25 | + { |
| 26 | + } |
| 27 | + |
| 28 | + public function getNodeType(): string |
| 29 | + { |
| 30 | + return Node\FunctionLike::class; |
| 31 | + } |
| 32 | + |
| 33 | + public function processNode(Node $node, Scope $scope): array |
| 34 | + { |
| 35 | + if ($node instanceof Node\Stmt\ClassMethod) { |
| 36 | + if (!$scope->isInClass()) { |
| 37 | + return []; |
| 38 | + } |
| 39 | + $functionName = $node->name->name; |
| 40 | + $description = sprintf('Method %s::%s()', $scope->getClassReflection()->getDisplayName(), $functionName); |
| 41 | + $identifier = 'pureMethod.conflictingPurityTags'; |
| 42 | + } elseif ($node instanceof Node\Stmt\Function_) { |
| 43 | + $functionName = trim($scope->getNamespace() . '\\' . $node->name->name, '\\'); |
| 44 | + $description = sprintf('Function %s()', $functionName); |
| 45 | + $identifier = 'pureFunction.conflictingPurityTags'; |
| 46 | + } else { |
| 47 | + return []; |
| 48 | + } |
| 49 | + |
| 50 | + $docComment = $node->getDocComment(); |
| 51 | + if ($docComment === null) { |
| 52 | + return []; |
| 53 | + } |
| 54 | + |
| 55 | + $resolvedPhpDoc = $this->fileTypeMapper->getResolvedPhpDoc( |
| 56 | + $scope->getFile(), |
| 57 | + $scope->isInClass() ? $scope->getClassReflection()->getName() : null, |
| 58 | + $scope->isInTrait() ? $scope->getTraitReflection()->getName() : null, |
| 59 | + $functionName, |
| 60 | + $docComment->getText(), |
| 61 | + ); |
| 62 | + |
| 63 | + $isPure = $resolvedPhpDoc->isPure(); |
| 64 | + if ($isPure === null) { |
| 65 | + return []; |
| 66 | + } |
| 67 | + |
| 68 | + $errors = []; |
| 69 | + foreach ([ |
| 70 | + '@pure-unless-callable-is-impure' => $resolvedPhpDoc->getParamsPureUnlessCallableIsImpure(), |
| 71 | + '@pure-unless-parameter-passed' => $resolvedPhpDoc->getParamsPureUnlessParameterPassed(), |
| 72 | + ] as $tagName => $parameters) { |
| 73 | + foreach (array_keys($parameters) as $parameterName) { |
| 74 | + $errors[] = RuleErrorBuilder::message(sprintf( |
| 75 | + '%s is marked as %s, which conflicts with %s for parameter $%s.', |
| 76 | + $description, |
| 77 | + $isPure ? 'pure' : 'impure', |
| 78 | + $tagName, |
| 79 | + $parameterName, |
| 80 | + ))->identifier($identifier)->build(); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + return $errors; |
| 85 | + } |
| 86 | + |
| 87 | +} |
0 commit comments