|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Rules\Functions; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PhpParser\Node\Expr\FuncCall; |
| 7 | +use PHPStan\Analyser\Scope; |
| 8 | +use PHPStan\Reflection\ReflectionProvider; |
| 9 | +use PHPStan\Rules\Rule; |
| 10 | +use PHPStan\Rules\RuleErrorBuilder; |
| 11 | +use PHPStan\Type\NeverType; |
| 12 | +use PHPStan\Type\Php\ArrayColumnHelper; |
| 13 | +use PHPStan\Type\VerbosityLevel; |
| 14 | +use function count; |
| 15 | +use function sprintf; |
| 16 | + |
| 17 | +/** |
| 18 | + * @implements Rule<Node\Expr\FuncCall> |
| 19 | + */ |
| 20 | +final class ArrayColumnRule implements Rule |
| 21 | +{ |
| 22 | + |
| 23 | + public function __construct( |
| 24 | + private readonly ReflectionProvider $reflectionProvider, |
| 25 | + private readonly bool $treatPhpDocTypesAsCertain, |
| 26 | + private readonly bool $treatPhpDocTypesAsCertainTip, |
| 27 | + private readonly ArrayColumnHelper $arrayColumnHelper, |
| 28 | + ) |
| 29 | + { |
| 30 | + } |
| 31 | + |
| 32 | + public function getNodeType(): string |
| 33 | + { |
| 34 | + return FuncCall::class; |
| 35 | + } |
| 36 | + |
| 37 | + public function processNode(Node $node, Scope $scope): array |
| 38 | + { |
| 39 | + if (!($node->name instanceof Node\Name)) { |
| 40 | + return []; |
| 41 | + } |
| 42 | + |
| 43 | + $args = $node->getArgs(); |
| 44 | + if (count($args) < 2) { |
| 45 | + return []; |
| 46 | + } |
| 47 | + |
| 48 | + if (!$this->reflectionProvider->hasFunction($node->name, $scope)) { |
| 49 | + return []; |
| 50 | + } |
| 51 | + |
| 52 | + $functionReflection = $this->reflectionProvider->getFunction($node->name, $scope); |
| 53 | + if ($functionReflection->getName() !== 'array_column') { |
| 54 | + return []; |
| 55 | + } |
| 56 | + |
| 57 | + $indexKeyType = null; |
| 58 | + if ($this->treatPhpDocTypesAsCertain) { |
| 59 | + $arrayType = $scope->getType($args[0]->value); |
| 60 | + $columnKeyType = $scope->getType($args[1]->value); |
| 61 | + if (count($args) >= 3) { |
| 62 | + $indexKeyType = $scope->getType($args[2]->value); |
| 63 | + } |
| 64 | + } else { |
| 65 | + $arrayType = $scope->getNativeType($args[0]->value); |
| 66 | + $columnKeyType = $scope->getNativeType($args[1]->value); |
| 67 | + if (count($args) >= 3) { |
| 68 | + $indexKeyType = $scope->getNativeType($args[2]->value); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + $errors = []; |
| 73 | + if ($columnKeyType->isNull()->no()) { |
| 74 | + [$returnValueType] = $this->arrayColumnHelper->getReturnValueType($arrayType, $columnKeyType, $scope); |
| 75 | + if ($returnValueType instanceof NeverType) { |
| 76 | + $errorBuilder = RuleErrorBuilder::message(sprintf( |
| 77 | + 'Cannot access column %s on %s.', |
| 78 | + $columnKeyType->describe(VerbosityLevel::value()), |
| 79 | + $arrayType->getIterableValueType()->describe(VerbosityLevel::value()), |
| 80 | + ))->identifier('arrayColumn.column'); |
| 81 | + |
| 82 | + if ($this->treatPhpDocTypesAsCertainTip) { |
| 83 | + $errorBuilder->treatPhpDocTypesAsCertainTip(); |
| 84 | + } |
| 85 | + |
| 86 | + $errors[] = $errorBuilder->build(); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + if ($indexKeyType !== null && $indexKeyType->isNull()->no()) { |
| 91 | + $returnIndexType = $this->arrayColumnHelper->getReturnIndexType($arrayType, $indexKeyType, $scope); |
| 92 | + if ($returnIndexType instanceof NeverType) { |
| 93 | + $errorBuilder = RuleErrorBuilder::message(sprintf( |
| 94 | + 'Cannot access column %s on %s.', |
| 95 | + $indexKeyType->describe(VerbosityLevel::value()), |
| 96 | + $arrayType->getIterableValueType()->describe(VerbosityLevel::value()), |
| 97 | + ))->identifier('arrayColumn.index'); |
| 98 | + |
| 99 | + if ($this->treatPhpDocTypesAsCertainTip) { |
| 100 | + $errorBuilder->treatPhpDocTypesAsCertainTip(); |
| 101 | + } |
| 102 | + |
| 103 | + $errors[] = $errorBuilder->build(); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + return $errors; |
| 108 | + } |
| 109 | + |
| 110 | +} |
0 commit comments