|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace MSpirkov\Yii2\PHPStan\Rules; |
| 6 | + |
| 7 | +use MSpirkov\Yii2\PHPStan\Analyzers\ExpressionTypeAnalyzer; |
| 8 | +use MSpirkov\Yii2\PHPStan\Resolvers\ExpressionValueResolver; |
| 9 | +use PhpParser\Node; |
| 10 | +use PhpParser\Node\Arg; |
| 11 | +use PhpParser\Node\Expr\Array_; |
| 12 | +use PhpParser\Node\Expr\MethodCall; |
| 13 | +use PhpParser\Node\Identifier; |
| 14 | +use PHPStan\Analyser\Scope; |
| 15 | +use PHPStan\Rules\IdentifierRuleError; |
| 16 | +use PHPStan\Rules\Rule; |
| 17 | +use yii\db\ActiveQueryInterface; |
| 18 | +use yii\db\QueryInterface; |
| 19 | + |
| 20 | +/** |
| 21 | + * @implements Rule<MethodCall> |
| 22 | + */ |
| 23 | +final class QueryConditionValidationRule implements Rule |
| 24 | +{ |
| 25 | + /** @var list<string> */ |
| 26 | + private const METHODS = ['where', 'andwhere', 'orwhere']; |
| 27 | + |
| 28 | + /** @var list<class-string> */ |
| 29 | + private const QUERY_CLASSES = [ |
| 30 | + ActiveQueryInterface::class, |
| 31 | + QueryInterface::class, |
| 32 | + ]; |
| 33 | + |
| 34 | + /** @var list<string> */ |
| 35 | + private const CONJUNCTION_OPERATORS = ['AND', 'OR', 'NOT']; |
| 36 | + |
| 37 | + /** @var array<string, array{exact: int}|array{atLeast: int}> */ |
| 38 | + private const OPERAND_REQUIREMENTS = [ |
| 39 | + 'AND' => ['atLeast' => 1], |
| 40 | + 'OR' => ['atLeast' => 1], |
| 41 | + 'NOT' => ['exact' => 1], |
| 42 | + 'BETWEEN' => ['atLeast' => 3], |
| 43 | + 'NOT BETWEEN' => ['atLeast' => 3], |
| 44 | + 'IN' => ['atLeast' => 2], |
| 45 | + 'NOT IN' => ['atLeast' => 2], |
| 46 | + 'LIKE' => ['atLeast' => 2], |
| 47 | + 'NOT LIKE' => ['atLeast' => 2], |
| 48 | + 'OR LIKE' => ['atLeast' => 2], |
| 49 | + 'OR NOT LIKE' => ['atLeast' => 2], |
| 50 | + 'EXISTS' => ['atLeast' => 1], |
| 51 | + 'NOT EXISTS' => ['atLeast' => 1], |
| 52 | + '=' => ['exact' => 2], |
| 53 | + '!=' => ['exact' => 2], |
| 54 | + '<>' => ['exact' => 2], |
| 55 | + '>' => ['exact' => 2], |
| 56 | + '>=' => ['exact' => 2], |
| 57 | + '<' => ['exact' => 2], |
| 58 | + '<=' => ['exact' => 2], |
| 59 | + ]; |
| 60 | + |
| 61 | + private ExpressionTypeAnalyzer $expressionTypeAnalyzer; |
| 62 | + |
| 63 | + private ExpressionValueResolver $expressionValueResolver; |
| 64 | + |
| 65 | + public function __construct( |
| 66 | + ExpressionTypeAnalyzer $expressionTypeAnalyzer, |
| 67 | + ExpressionValueResolver $expressionValueResolver |
| 68 | + ) { |
| 69 | + $this->expressionTypeAnalyzer = $expressionTypeAnalyzer; |
| 70 | + $this->expressionValueResolver = $expressionValueResolver; |
| 71 | + } |
| 72 | + |
| 73 | + public function getNodeType(): string |
| 74 | + { |
| 75 | + return MethodCall::class; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @return list<IdentifierRuleError> |
| 80 | + */ |
| 81 | + public function processNode(Node $node, Scope $scope): array |
| 82 | + { |
| 83 | + if (!$node->name instanceof Identifier || !in_array(strtolower($node->name->name), self::METHODS, true)) { |
| 84 | + return []; |
| 85 | + } |
| 86 | + |
| 87 | + if (!$this->expressionTypeAnalyzer->isTypeAnyOf($scope->getType($node->var), self::QUERY_CLASSES)) { |
| 88 | + return []; |
| 89 | + } |
| 90 | + |
| 91 | + if (!isset($node->args[0]) || !$node->args[0] instanceof Arg) { |
| 92 | + return []; |
| 93 | + } |
| 94 | + |
| 95 | + if (!$node->args[0]->value instanceof Array_) { |
| 96 | + return []; |
| 97 | + } |
| 98 | + |
| 99 | + return $this->validateConditionArray($node->args[0]->value, $node->name->name, $scope); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * @return list<IdentifierRuleError> |
| 104 | + */ |
| 105 | + private function validateConditionArray(Array_ $array, string $methodName, Scope $scope): array |
| 106 | + { |
| 107 | + if (!isset($array->items[0]) || $array->items[0]->unpack || $array->items[0]->key !== null) { |
| 108 | + return []; |
| 109 | + } |
| 110 | + |
| 111 | + foreach ($array->items as $item) { |
| 112 | + if ($item->unpack) { |
| 113 | + return []; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + $operatorName = $this->expressionValueResolver->getSingleStringValue($array->items[0]->value, $scope); |
| 118 | + if ($operatorName === null) { |
| 119 | + return []; |
| 120 | + } |
| 121 | + |
| 122 | + $operator = strtoupper($operatorName); |
| 123 | + $requirement = self::OPERAND_REQUIREMENTS[$operator] ?? null; |
| 124 | + if ($requirement === null) { |
| 125 | + return []; |
| 126 | + } |
| 127 | + |
| 128 | + $operandItems = array_slice($array->items, 1); |
| 129 | + $errors = $this->validateOperandCount( |
| 130 | + $operator, |
| 131 | + $requirement, |
| 132 | + count($operandItems), |
| 133 | + $methodName, |
| 134 | + $array |
| 135 | + ); |
| 136 | + |
| 137 | + if (!in_array($operator, self::CONJUNCTION_OPERATORS, true)) { |
| 138 | + return $errors; |
| 139 | + } |
| 140 | + |
| 141 | + foreach ($operandItems as $operandItem) { |
| 142 | + if ($operandItem->value instanceof Array_) { |
| 143 | + $errors = array_merge($errors, $this->validateConditionArray( |
| 144 | + $operandItem->value, |
| 145 | + $methodName, |
| 146 | + $scope |
| 147 | + )); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + return $errors; |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * @param array{exact: int}|array{atLeast: int} $requirement |
| 156 | + * |
| 157 | + * @return list<IdentifierRuleError> |
| 158 | + */ |
| 159 | + private function validateOperandCount( |
| 160 | + string $operator, |
| 161 | + array $requirement, |
| 162 | + int $actualCount, |
| 163 | + string $methodName, |
| 164 | + Array_ $array |
| 165 | + ): array { |
| 166 | + if (isset($requirement['exact']) && $actualCount !== $requirement['exact']) { |
| 167 | + return [$this->buildError($operator, $methodName, 'exactly', $requirement['exact'], $actualCount, $array)]; |
| 168 | + } |
| 169 | + |
| 170 | + if (isset($requirement['atLeast']) && $actualCount < $requirement['atLeast']) { |
| 171 | + return [$this->buildError($operator, $methodName, 'at least', $requirement['atLeast'], $actualCount, $array)]; |
| 172 | + } |
| 173 | + |
| 174 | + return []; |
| 175 | + } |
| 176 | + |
| 177 | + private function buildError( |
| 178 | + string $operator, |
| 179 | + string $methodName, |
| 180 | + string $quantifier, |
| 181 | + int $expectedCount, |
| 182 | + int $actualCount, |
| 183 | + Array_ $array |
| 184 | + ): IdentifierRuleError { |
| 185 | + return ErrorBuilder::build( |
| 186 | + sprintf( |
| 187 | + 'Operator \'%s\' in %s() requires %s %d operand%s, %d given.', |
| 188 | + $operator, |
| 189 | + $methodName, |
| 190 | + $quantifier, |
| 191 | + $expectedCount, |
| 192 | + $expectedCount === 1 ? '' : 's', |
| 193 | + $actualCount |
| 194 | + ), |
| 195 | + Identifiers::QUERY_CONDITION_VALIDATION, |
| 196 | + $array->getStartLine() |
| 197 | + ); |
| 198 | + } |
| 199 | +} |
0 commit comments