|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Rules\Doctrine\DBAL; |
| 4 | + |
| 5 | +use Doctrine\DBAL\Connection; |
| 6 | +use PhpParser\Node; |
| 7 | +use PHPStan\Analyser\Scope; |
| 8 | +use PHPStan\Rules\Rule; |
| 9 | +use PHPStan\Rules\RuleErrorBuilder; |
| 10 | +use PHPStan\Type\ConstantScalarType; |
| 11 | +use PHPStan\Type\ObjectType; |
| 12 | +use PHPStan\Type\VerbosityLevel; |
| 13 | +use function array_map; |
| 14 | +use function count; |
| 15 | +use function in_array; |
| 16 | +use function is_int; |
| 17 | + |
| 18 | +/** |
| 19 | + * @implements Rule<Node\Expr\MethodCall> |
| 20 | + */ |
| 21 | +class ArrayParameterTypeRule implements Rule |
| 22 | +{ |
| 23 | + |
| 24 | + private const CONNECTION_QUERY_METHODS_LOWER = [ |
| 25 | + 'fetchassociative', |
| 26 | + 'fetchnumeric', |
| 27 | + 'fetchone', |
| 28 | + 'delete', |
| 29 | + 'insert', |
| 30 | + 'fetchallnumeric', |
| 31 | + 'fetchallassociative', |
| 32 | + 'fetchallkeyvalue', |
| 33 | + 'fetchallassociativeindexed', |
| 34 | + 'fetchfirstcolumn', |
| 35 | + 'iteratenumeric', |
| 36 | + 'iterateassociative', |
| 37 | + 'iteratekeyvalue', |
| 38 | + 'iterateassociativeindexed', |
| 39 | + 'iteratecolumn', |
| 40 | + 'executequery', |
| 41 | + 'executecachequery', |
| 42 | + 'executestatement', |
| 43 | + ]; |
| 44 | + |
| 45 | + public function getNodeType(): string |
| 46 | + { |
| 47 | + return Node\Expr\MethodCall::class; |
| 48 | + } |
| 49 | + |
| 50 | + public function processNode(Node $node, Scope $scope): array |
| 51 | + { |
| 52 | + if (! $node->name instanceof Node\Identifier) { |
| 53 | + return []; |
| 54 | + } |
| 55 | + |
| 56 | + if (count($node->getArgs()) < 2) { |
| 57 | + return []; |
| 58 | + } |
| 59 | + |
| 60 | + $calledOnType = $scope->getType($node->var); |
| 61 | + |
| 62 | + $connection = 'Doctrine\DBAL\Connection'; |
| 63 | + if (! (new ObjectType($connection))->isSuperTypeOf($calledOnType)->yes()) { |
| 64 | + return []; |
| 65 | + } |
| 66 | + |
| 67 | + $methodName = $node->name->toLowerString(); |
| 68 | + if (! in_array($methodName, self::CONNECTION_QUERY_METHODS_LOWER, true)) { |
| 69 | + return []; |
| 70 | + } |
| 71 | + |
| 72 | + $params = $scope->getType($node->getArgs()[1]->value); |
| 73 | + |
| 74 | + $typesArray = $node->getArgs()[2] ?? null; |
| 75 | + $typesArrayType = $typesArray !== null |
| 76 | + ? $scope->getType($typesArray->value) |
| 77 | + : null; |
| 78 | + |
| 79 | + foreach ($params->getConstantArrays() as $arrayType) { |
| 80 | + $values = $arrayType->getValueTypes(); |
| 81 | + $keys = []; |
| 82 | + foreach ($values as $i => $value) { |
| 83 | + if (!$value->isArray()->yes()) { |
| 84 | + continue; |
| 85 | + } |
| 86 | + |
| 87 | + $keys[] = $arrayType->getKeyTypes()[$i]; |
| 88 | + } |
| 89 | + |
| 90 | + if ($keys === []) { |
| 91 | + continue; |
| 92 | + } |
| 93 | + |
| 94 | + $typeConstantArrays = $typesArrayType !== null |
| 95 | + ? $typesArrayType->getConstantArrays() |
| 96 | + : []; |
| 97 | + |
| 98 | + if ($typeConstantArrays === []) { |
| 99 | + return array_map( |
| 100 | + static function (ConstantScalarType $type) { |
| 101 | + return RuleErrorBuilder::message( |
| 102 | + 'Parameter at ' |
| 103 | + . $type->describe(VerbosityLevel::precise()) |
| 104 | + . ' is an array, but is not hinted as such to doctrine.' |
| 105 | + ) |
| 106 | + ->identifier('doctrine.parameterType') |
| 107 | + ->build(); |
| 108 | + }, |
| 109 | + $keys |
| 110 | + ); |
| 111 | + } |
| 112 | + |
| 113 | + foreach ($typeConstantArrays as $typeConstantArray) { |
| 114 | + $issueKeys = []; |
| 115 | + foreach ($keys as $key) { |
| 116 | + $valueType = $typeConstantArray->getOffsetValueType($key); |
| 117 | + |
| 118 | + $values = $valueType->getConstantScalarValues(); |
| 119 | + if ($values === []) { |
| 120 | + $issueKeys[] = $key; |
| 121 | + } |
| 122 | + |
| 123 | + foreach ($values as $scalarValue) { |
| 124 | + if (is_int($scalarValue) && !(($scalarValue & Connection::ARRAY_PARAM_OFFSET) !== Connection::ARRAY_PARAM_OFFSET)) { |
| 125 | + continue; |
| 126 | + } |
| 127 | + |
| 128 | + $issueKeys[] = $key; |
| 129 | + } |
| 130 | + |
| 131 | + return array_map( |
| 132 | + static function (ConstantScalarType $type) { |
| 133 | + return RuleErrorBuilder::message( |
| 134 | + 'Parameter at ' |
| 135 | + . $type->describe(VerbosityLevel::precise()) |
| 136 | + . ' is an array, but is not hinted as such to doctrine.' |
| 137 | + )->identifier('doctrine.parameterType') |
| 138 | + ->build(); |
| 139 | + }, |
| 140 | + $issueKeys |
| 141 | + ); |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + return []; |
| 147 | + } |
| 148 | + |
| 149 | +} |
0 commit comments