|
3 | 3 | namespace PHPStan\Rules\Arrays; |
4 | 4 |
|
5 | 5 | use PhpParser\Node; |
| 6 | +use PhpParser\Node\Expr\ArrayDimFetch; |
| 7 | +use PhpParser\Node\Expr\Variable; |
6 | 8 | use PHPStan\Analyser\NullsafeOperatorHelper; |
7 | 9 | use PHPStan\Analyser\Scope; |
8 | 10 | use PHPStan\DependencyInjection\AutowiredParameter; |
|
11 | 13 | use PHPStan\Rules\Rule; |
12 | 14 | use PHPStan\Rules\RuleErrorBuilder; |
13 | 15 | use PHPStan\Rules\RuleLevelHelper; |
| 16 | +use PHPStan\TrinaryLogic; |
14 | 17 | use PHPStan\Type\ErrorType; |
15 | 18 | use PHPStan\Type\Type; |
16 | 19 | use PHPStan\Type\VerbosityLevel; |
@@ -42,6 +45,10 @@ public function getNodeType(): string |
42 | 45 |
|
43 | 46 | public function processNode(Node $node, Scope $scope): array |
44 | 47 | { |
| 48 | + if ($this->isImplicitArrayCreation($node, $scope)->yes()) { |
| 49 | + return []; |
| 50 | + } |
| 51 | + |
45 | 52 | if ($node->dim !== null) { |
46 | 53 | $dimType = $scope->getType($node->dim); |
47 | 54 | $unknownClassPattern = sprintf('Access to offset %s on an unknown class %%s.', SprintfHelper::escapeFormatString($dimType->describe(VerbosityLevel::value()))); |
@@ -153,4 +160,22 @@ public function processNode(Node $node, Scope $scope): array |
153 | 160 | ); |
154 | 161 | } |
155 | 162 |
|
| 163 | + private function isImplicitArrayCreation(Node\Expr\ArrayDimFetch $node, Scope $scope): TrinaryLogic |
| 164 | + { |
| 165 | + $varNode = $node->var; |
| 166 | + while ($varNode instanceof ArrayDimFetch) { |
| 167 | + $varNode = $varNode->var; |
| 168 | + } |
| 169 | + |
| 170 | + if (!$varNode instanceof Variable) { |
| 171 | + return TrinaryLogic::createNo(); |
| 172 | + } |
| 173 | + |
| 174 | + if (!is_string($varNode->name)) { |
| 175 | + return TrinaryLogic::createNo(); |
| 176 | + } |
| 177 | + |
| 178 | + return $scope->hasVariableType($varNode->name)->negate(); |
| 179 | + } |
| 180 | + |
156 | 181 | } |
0 commit comments