-
Notifications
You must be signed in to change notification settings - Fork 570
Expand file tree
/
Copy pathArrayDestructuringRule.php
More file actions
118 lines (103 loc) · 2.64 KB
/
ArrayDestructuringRule.php
File metadata and controls
118 lines (103 loc) · 2.64 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
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Arrays;
use ArrayAccess;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\RegisteredRule;
use PHPStan\Node\Expr\TypeExpr;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\VerbosityLevel;
use function array_merge;
use function sprintf;
/**
* @implements Rule<Assign>
*/
#[RegisteredRule(level: 3)]
final class ArrayDestructuringRule implements Rule
{
public function __construct(
private RuleLevelHelper $ruleLevelHelper,
private NonexistentOffsetInArrayDimFetchCheck $nonexistentOffsetInArrayDimFetchCheck,
)
{
}
public function getNodeType(): string
{
return Assign::class;
}
public function processNode(Node $node, Scope $scope): array
{
if (!$node->var instanceof Node\Expr\List_) {
return [];
}
return $this->getErrors(
$scope,
$node->var,
$node->expr,
);
}
/**
* @return list<IdentifierRuleError>
*/
private function getErrors(Scope $scope, Node\Expr\List_ $var, Expr $expr): array
{
$exprTypeResult = $this->ruleLevelHelper->findTypeToCheck(
$scope,
$expr,
'',
static fn (Type $varType): bool => $varType->isArray()->yes() || (new ObjectType(ArrayAccess::class))->isSuperTypeOf($varType)->yes(),
);
$exprType = $exprTypeResult->getType();
if ($exprType->isError()->yes()) {
return [];
}
if (!$exprType->isArray()->yes() && !(new ObjectType(ArrayAccess::class))->isSuperTypeOf($exprType)->yes()) {
return [
RuleErrorBuilder::message(sprintf('Cannot use array destructuring on %s.', $exprType->describe(VerbosityLevel::typeOnly())))
->identifier('offsetAccess.nonArray')
->build(),
];
}
$errors = [];
$i = 0;
foreach ($var->items as $item) {
if ($item === null) {
$i++;
continue;
}
$keyExpr = null;
if ($item->key === null) {
$keyType = new ConstantIntegerType($i);
$keyExpr = new Node\Scalar\Int_($i);
} else {
$keyType = $scope->getType($item->key);
$keyExpr = new TypeExpr($keyType);
}
$itemErrors = $this->nonexistentOffsetInArrayDimFetchCheck->check(
$scope,
$expr,
'',
$keyType,
);
$errors = array_merge($errors, $itemErrors);
if (!$item->value instanceof Node\Expr\List_) {
$i++;
continue;
}
$errors = array_merge($errors, $this->getErrors(
$scope,
$item->value,
new Expr\ArrayDimFetch($expr, $keyExpr),
));
}
return $errors;
}
}