-
Notifications
You must be signed in to change notification settings - Fork 578
Expand file tree
/
Copy pathArrayColumnRule.php
More file actions
134 lines (111 loc) · 3.78 KB
/
Copy pathArrayColumnRule.php
File metadata and controls
134 lines (111 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Functions;
use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\ArgumentsNormalizer;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\Php\ArrayColumnHelper;
use PHPStan\Type\Type;
use PHPStan\Type\VerbosityLevel;
use function count;
use function sprintf;
/**
* Reports `array_column()` calls reading a property that does not exist on the
* objects contained in the source array.
*
* @implements Rule<FuncCall>
*/
final class ArrayColumnRule implements Rule
{
public function __construct(
private readonly ReflectionProvider $reflectionProvider,
private readonly ArrayColumnHelper $arrayColumnHelper,
private readonly bool $treatPhpDocTypesAsCertain,
private readonly bool $treatPhpDocTypesAsCertainTip,
)
{
}
public function getNodeType(): string
{
return FuncCall::class;
}
public function processNode(Node $node, Scope $scope): array
{
if (!($node->name instanceof Node\Name)) {
return [];
}
if (!$this->reflectionProvider->hasFunction($node->name, $scope)) {
return [];
}
$functionReflection = $this->reflectionProvider->getFunction($node->name, $scope);
if ($functionReflection->getName() !== 'array_column') {
return [];
}
$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs(
$scope,
$node->getArgs(),
$functionReflection->getVariants(),
$functionReflection->getNamedArgumentsVariants(),
);
$normalizedFuncCall = ArgumentsNormalizer::reorderFuncArguments($parametersAcceptor, $node);
if ($normalizedFuncCall === null) {
return [];
}
$args = $normalizedFuncCall->getArgs();
if (count($args) < 2) {
return [];
}
$arrayArg = $args[0]->value;
$valueType = $scope->getType($arrayArg)->getIterableValueType();
$nativeValueType = $scope->getNativeType($arrayArg)->getIterableValueType();
$errors = [];
foreach ($this->checkColumn($args[1]->value, $valueType, $nativeValueType, '#2 $column_key', $scope) as $error) {
$errors[] = $error;
}
if (count($args) >= 3) {
foreach ($this->checkColumn($args[2]->value, $valueType, $nativeValueType, '#3 $index_key', $scope) as $error) {
$errors[] = $error;
}
}
return $errors;
}
/**
* @return list<IdentifierRuleError>
*/
private function checkColumn(Node\Expr $columnExpr, Type $valueType, Type $nativeValueType, string $parameter, Scope $scope): array
{
$checkedValueType = $this->treatPhpDocTypesAsCertain ? $valueType : $nativeValueType;
$columnType = $scope->getType($columnExpr);
$missingProperties = $this->arrayColumnHelper->findMissingObjectProperties($checkedValueType, $columnType);
if ($missingProperties === []) {
return [];
}
$nativeMissingPropertyNames = [];
foreach ($this->arrayColumnHelper->findMissingObjectProperties($nativeValueType, $columnType) as $nativeMissingProperty) {
$nativeMissingPropertyNames[$nativeMissingProperty->getValue()] = true;
}
$errors = [];
foreach ($missingProperties as $propertyNameType) {
$errorBuilder = RuleErrorBuilder::message(sprintf(
'Parameter %s of function array_column expects a valid property name, %s given, but %s does not have such property.',
$parameter,
$propertyNameType->describe(VerbosityLevel::value()),
$checkedValueType->describe(VerbosityLevel::typeOnly()),
))->identifier('arrayColumn.property');
if (
$this->treatPhpDocTypesAsCertain
&& $this->treatPhpDocTypesAsCertainTip
&& !isset($nativeMissingPropertyNames[$propertyNameType->getValue()])
) {
$errorBuilder->treatPhpDocTypesAsCertainTip();
}
$errors[] = $errorBuilder->build();
}
return $errors;
}
}