|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace ShipMonk\PHPStan\Rule; |
| 4 | + |
| 5 | +use Generator; |
| 6 | +use PhpParser\Node; |
| 7 | +use PHPStan\Analyser\Scope; |
| 8 | +use PHPStan\Node\ClosureReturnStatementsNode; |
| 9 | +use PHPStan\Node\MethodReturnStatementsNode; |
| 10 | +use PHPStan\Node\ReturnStatementsNode; |
| 11 | +use PHPStan\Reflection\ParametersAcceptorSelector; |
| 12 | +use PHPStan\Rules\Rule; |
| 13 | +use PHPStan\Rules\RuleError; |
| 14 | +use PHPStan\Rules\RuleErrorBuilder; |
| 15 | +use PHPStan\Type\MixedType; |
| 16 | +use PHPStan\Type\Type; |
| 17 | +use function in_array; |
| 18 | + |
| 19 | +/** |
| 20 | + * @implements Rule<ReturnStatementsNode> |
| 21 | + */ |
| 22 | +class ForbidReturnValueInYieldingMethodRule implements Rule |
| 23 | +{ |
| 24 | + |
| 25 | + private bool $reportRegardlessOfReturnType; |
| 26 | + |
| 27 | + public function __construct(bool $reportRegardlessOfReturnType) |
| 28 | + { |
| 29 | + $this->reportRegardlessOfReturnType = $reportRegardlessOfReturnType; |
| 30 | + } |
| 31 | + |
| 32 | + public function getNodeType(): string |
| 33 | + { |
| 34 | + return ReturnStatementsNode::class; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * @param ReturnStatementsNode $node |
| 39 | + * @return list<RuleError> |
| 40 | + */ |
| 41 | + public function processNode( |
| 42 | + Node $node, |
| 43 | + Scope $scope |
| 44 | + ): array |
| 45 | + { |
| 46 | + if (!$node->getStatementResult()->hasYield()) { |
| 47 | + return []; |
| 48 | + } |
| 49 | + |
| 50 | + if (!$this->reportRegardlessOfReturnType) { |
| 51 | + $methodReturnType = $this->getReturnType($node, $scope); |
| 52 | + |
| 53 | + if (in_array(Generator::class, $methodReturnType->getObjectClassNames(), true)) { |
| 54 | + return []; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + $errors = []; |
| 59 | + |
| 60 | + foreach ($node->getReturnStatements() as $returnStatement) { |
| 61 | + $returnNode = $returnStatement->getReturnNode(); |
| 62 | + |
| 63 | + if ($returnNode->expr === null) { |
| 64 | + continue; |
| 65 | + } |
| 66 | + |
| 67 | + $suffix = $this->reportRegardlessOfReturnType |
| 68 | + ? 'this approach is denied' |
| 69 | + : 'but this method is not marked to return Generator'; |
| 70 | + |
| 71 | + $callType = $node instanceof MethodReturnStatementsNode // @phpstan-ignore-line ignore bc promise |
| 72 | + ? 'method' |
| 73 | + : 'function'; |
| 74 | + |
| 75 | + $errors[] = RuleErrorBuilder::message("Returned value from yielding $callType can be accessed only via Generator::getReturn, $suffix.") |
| 76 | + ->line($returnNode->getLine()) |
| 77 | + ->build(); |
| 78 | + } |
| 79 | + |
| 80 | + return $errors; |
| 81 | + } |
| 82 | + |
| 83 | + private function getReturnType(ReturnStatementsNode $node, Scope $scope): Type |
| 84 | + { |
| 85 | + $methodReflection = $scope->getFunction(); |
| 86 | + |
| 87 | + if ($node instanceof ClosureReturnStatementsNode) { // @phpstan-ignore-line ignore bc promise |
| 88 | + return $scope->getFunctionType($node->getClosureExpr()->getReturnType(), false, false); |
| 89 | + } |
| 90 | + |
| 91 | + if ($methodReflection !== null) { |
| 92 | + return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType(); |
| 93 | + } |
| 94 | + |
| 95 | + return new MixedType(); |
| 96 | + } |
| 97 | + |
| 98 | +} |
0 commit comments