-
Notifications
You must be signed in to change notification settings - Fork 579
Expand file tree
/
Copy pathArrayColumnRule.php
More file actions
162 lines (135 loc) · 4.46 KB
/
Copy pathArrayColumnRule.php
File metadata and controls
162 lines (135 loc) · 4.46 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?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\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 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;
// array_column() reads object properties (never ArrayAccess offsets), so
// only check when the elements are definitely objects. Array elements use
// offset access, scalars never have the member - leave those to other rules.
if (!$checkedValueType->isObject()->yes()) {
return [];
}
$columnType = $scope->getType($columnExpr);
$propertyNames = $columnType->getConstantStrings();
if ($propertyNames === []) {
return [];
}
$errors = [];
foreach ($propertyNames as $propertyNameType) {
$propertyName = $propertyNameType->getValue();
if (!$this->isPropertyMissing($checkedValueType, $propertyName)) {
continue;
}
$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) {
if (!$nativeValueType->isObject()->yes() || !$this->isPropertyMissing($nativeValueType, $propertyName)) {
$errorBuilder->treatPhpDocTypesAsCertainTip();
}
}
$errors[] = $errorBuilder->build();
}
return $errors;
}
private function isPropertyMissing(Type $valueType, string $propertyName): bool
{
$classReflections = $valueType->getObjectClassReflections();
if ($classReflections === []) {
return false;
}
foreach ($classReflections as $classReflection) {
if ($classReflection->isEnum()) {
return false;
}
if ($classReflection->hasInstanceProperty($propertyName)) {
return false;
}
if ($classReflection->allowsDynamicProperties()) {
return false;
}
if ($classReflection->hasNativeMethod('__isset') && $classReflection->hasNativeMethod('__get')) {
return false;
}
}
return true;
}
}