|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace mglaman\PHPStanDrupal\Rules\Drupal; |
| 6 | + |
| 7 | +use Drupal\Core\Access\AccessResult; |
| 8 | +use PhpParser\Node; |
| 9 | +use PHPStan\Analyser\Scope; |
| 10 | +use PHPStan\Rules\Rule; |
| 11 | +use PHPStan\Rules\RuleErrorBuilder; |
| 12 | +use PHPStan\Type\VerbosityLevel; |
| 13 | + |
| 14 | +/** |
| 15 | + * @implements Rule<Node\Expr\StaticCall> |
| 16 | + */ |
| 17 | +final class AccessResultConditionRule implements Rule |
| 18 | +{ |
| 19 | + |
| 20 | + /** @var bool */ |
| 21 | + private $treatPhpDocTypesAsCertain; |
| 22 | + |
| 23 | + /** |
| 24 | + * @param bool $treatPhpDocTypesAsCertain |
| 25 | + */ |
| 26 | + public function __construct($treatPhpDocTypesAsCertain) |
| 27 | + { |
| 28 | + $this->treatPhpDocTypesAsCertain = $treatPhpDocTypesAsCertain; |
| 29 | + } |
| 30 | + |
| 31 | + public function getNodeType(): string |
| 32 | + { |
| 33 | + return Node\Expr\StaticCall::class; |
| 34 | + } |
| 35 | + |
| 36 | + public function processNode(Node $node, Scope $scope): array |
| 37 | + { |
| 38 | + if (!$node->name instanceof Node\Identifier) { |
| 39 | + return []; |
| 40 | + } |
| 41 | + $methodName = $node->name->toString(); |
| 42 | + if (!in_array($methodName, ['allowedIf', 'forbiddenIf'], true)) { |
| 43 | + return []; |
| 44 | + } |
| 45 | + if (!$node->class instanceof Node\Name) { |
| 46 | + return []; |
| 47 | + } |
| 48 | + $className = $scope->resolveName($node->class); |
| 49 | + if ($className !== AccessResult::class) { |
| 50 | + return []; |
| 51 | + } |
| 52 | + $args = $node->getArgs(); |
| 53 | + if (count($args) === 0) { |
| 54 | + return []; |
| 55 | + } |
| 56 | + $condition = $args[0]->value; |
| 57 | + if (!$condition instanceof Node\Expr\BinaryOp\Identical && !$condition instanceof Node\Expr\BinaryOp\NotIdentical) { |
| 58 | + return []; |
| 59 | + } |
| 60 | + $conditionType = $this->treatPhpDocTypesAsCertain ? $scope->getType($condition) : $scope->getNativeType($condition); |
| 61 | + $bool = $conditionType->toBoolean(); |
| 62 | + |
| 63 | + if ($bool->isTrue()->or($bool->isFalse())->yes()) { |
| 64 | + $leftType = $this->treatPhpDocTypesAsCertain ? $scope->getType($condition->left) : $scope->getNativeType($condition->left); |
| 65 | + $rightType = $this->treatPhpDocTypesAsCertain ? $scope->getType($condition->right) : $scope->getNativeType($condition->right); |
| 66 | + |
| 67 | + return [ |
| 68 | + RuleErrorBuilder::message(sprintf( |
| 69 | + 'Strict comparison using %s between %s and %s will always evaluate to %s.', |
| 70 | + $condition->getOperatorSigil(), |
| 71 | + $leftType->describe(VerbosityLevel::value()), |
| 72 | + $rightType->describe(VerbosityLevel::value()), |
| 73 | + $bool->describe(VerbosityLevel::value()), |
| 74 | + ))->identifier(sprintf('%s.alwaysFalse', $condition instanceof Node\Expr\BinaryOp\Identical ? 'identical' : 'notIdentical'))->build(), |
| 75 | + ]; |
| 76 | + } |
| 77 | + return []; |
| 78 | + } |
| 79 | +} |
0 commit comments