|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +/** |
| 5 | + * Copyright 2024, Cake Development Corporation (https://www.cakedc.com) |
| 6 | + * |
| 7 | + * Licensed under The MIT License |
| 8 | + * Redistributions of files must retain the above copyright notice. |
| 9 | + * |
| 10 | + * @copyright Copyright 2024, Cake Development Corporation (https://www.cakedc.com) |
| 11 | + * @license MIT License (http://www.opensource.org/licenses/mit-license.php) |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CakeDC\PHPStan\Rule\Controller; |
| 15 | + |
| 16 | +use Cake\Controller\Controller; |
| 17 | +use PhpParser\Node; |
| 18 | +use PhpParser\Node\Expr\MethodCall; |
| 19 | +use PhpParser\Node\Stmt\Expression; |
| 20 | +use PHPStan\Analyser\Scope; |
| 21 | +use PHPStan\Rules\Rule; |
| 22 | +use PHPStan\Rules\RuleErrorBuilder; |
| 23 | + |
| 24 | +/** |
| 25 | + * Rule to enforce that certain controller methods must be used |
| 26 | + * (returned or assigned) to prevent unreachable code. |
| 27 | + */ |
| 28 | +class ControllerMethodMustBeUsedRule implements Rule |
| 29 | +{ |
| 30 | + /** |
| 31 | + * Methods that must be used (returned or assigned) |
| 32 | + * |
| 33 | + * @var array<string> |
| 34 | + */ |
| 35 | + protected array $methodsRequiringUsage = [ |
| 36 | + 'render', |
| 37 | + 'redirect', |
| 38 | + ]; |
| 39 | + |
| 40 | + /** |
| 41 | + * @inheritDoc |
| 42 | + */ |
| 43 | + public function getNodeType(): string |
| 44 | + { |
| 45 | + return Expression::class; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @param \PhpParser\Node $node |
| 50 | + * @param \PHPStan\Analyser\Scope $scope |
| 51 | + * @return list<\PHPStan\Rules\IdentifierRuleError> |
| 52 | + */ |
| 53 | + public function processNode(Node $node, Scope $scope): array |
| 54 | + { |
| 55 | + assert($node instanceof Expression); |
| 56 | + |
| 57 | + // Check if this is a method call |
| 58 | + if (!$node->expr instanceof MethodCall) { |
| 59 | + return []; |
| 60 | + } |
| 61 | + |
| 62 | + $methodCall = $node->expr; |
| 63 | + |
| 64 | + // Check if the method name is one we care about |
| 65 | + if (!$methodCall->name instanceof Node\Identifier) { |
| 66 | + return []; |
| 67 | + } |
| 68 | + |
| 69 | + $methodName = $methodCall->name->toString(); |
| 70 | + if (!in_array($methodName, $this->methodsRequiringUsage, true)) { |
| 71 | + return []; |
| 72 | + } |
| 73 | + |
| 74 | + // Check if the method is being called on $this |
| 75 | + $callerType = $scope->getType($methodCall->var); |
| 76 | + if (!$callerType->isObject()->yes()) { |
| 77 | + return []; |
| 78 | + } |
| 79 | + |
| 80 | + // Check if it's a Controller class |
| 81 | + $classReflections = $callerType->getObjectClassReflections(); |
| 82 | + $isController = false; |
| 83 | + foreach ($classReflections as $classReflection) { |
| 84 | + if ($classReflection->is(Controller::class)) { |
| 85 | + $isController = true; |
| 86 | + break; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + if (!$isController) { |
| 91 | + return []; |
| 92 | + } |
| 93 | + |
| 94 | + // If we reach here, it means the method call is wrapped in an Expression node |
| 95 | + // which means it's used as a statement (not returned or assigned) |
| 96 | + return [ |
| 97 | + RuleErrorBuilder::message(sprintf( |
| 98 | + 'Method `%s()` must be used to prevent unreachable code. ' . |
| 99 | + 'Use `return $this->%s()` or assign it to a variable.', |
| 100 | + $methodName, |
| 101 | + $methodName, |
| 102 | + )) |
| 103 | + ->identifier('cake.controller.' . $methodName . 'MustBeUsed') |
| 104 | + ->build(), |
| 105 | + ]; |
| 106 | + } |
| 107 | +} |
0 commit comments