|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Rules\Arrays; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PhpParser\Node\Expr; |
| 7 | +use PhpParser\Node\Expr\Assign; |
| 8 | +use PhpParser\Node\Scalar\LNumber; |
| 9 | +use PHPStan\Analyser\Scope; |
| 10 | +use PHPStan\Rules\Rule; |
| 11 | +use PHPStan\Rules\RuleError; |
| 12 | +use PHPStan\Rules\RuleErrorBuilder; |
| 13 | +use PHPStan\Rules\RuleLevelHelper; |
| 14 | +use PHPStan\Type\Constant\ConstantIntegerType; |
| 15 | +use PHPStan\Type\Constant\ConstantStringType; |
| 16 | +use PHPStan\Type\ErrorType; |
| 17 | +use PHPStan\Type\Type; |
| 18 | +use PHPStan\Type\VerbosityLevel; |
| 19 | + |
| 20 | +/** |
| 21 | + * @implements Rule<Assign> |
| 22 | + */ |
| 23 | +class ArrayDeconstructionRule implements Rule |
| 24 | +{ |
| 25 | + |
| 26 | + private RuleLevelHelper $ruleLevelHelper; |
| 27 | + |
| 28 | + private NonexistentOffsetInArrayDimFetchCheck $nonexistentOffsetInArrayDimFetchCheck; |
| 29 | + |
| 30 | + public function __construct( |
| 31 | + RuleLevelHelper $ruleLevelHelper, |
| 32 | + NonexistentOffsetInArrayDimFetchCheck $nonexistentOffsetInArrayDimFetchCheck |
| 33 | + ) |
| 34 | + { |
| 35 | + $this->ruleLevelHelper = $ruleLevelHelper; |
| 36 | + $this->nonexistentOffsetInArrayDimFetchCheck = $nonexistentOffsetInArrayDimFetchCheck; |
| 37 | + } |
| 38 | + |
| 39 | + public function getNodeType(): string |
| 40 | + { |
| 41 | + return Assign::class; |
| 42 | + } |
| 43 | + |
| 44 | + public function processNode(Node $node, Scope $scope): array |
| 45 | + { |
| 46 | + if (!$node->var instanceof Node\Expr\List_ && !$node->var instanceof Node\Expr\Array_) { |
| 47 | + return []; |
| 48 | + } |
| 49 | + |
| 50 | + return $this->getErrors( |
| 51 | + $scope, |
| 52 | + $node->var, |
| 53 | + $node->expr |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @param Node\Expr\List_|Node\Expr\Array_ $var |
| 59 | + * @return RuleError[] |
| 60 | + */ |
| 61 | + private function getErrors(Scope $scope, Expr $var, Expr $expr): array |
| 62 | + { |
| 63 | + $exprTypeResult = $this->ruleLevelHelper->findTypeToCheck( |
| 64 | + $scope, |
| 65 | + $expr, |
| 66 | + '', |
| 67 | + static function (Type $varType): bool { |
| 68 | + return $varType->isArray()->yes(); |
| 69 | + } |
| 70 | + ); |
| 71 | + $exprType = $exprTypeResult->getType(); |
| 72 | + if ($exprType instanceof ErrorType) { |
| 73 | + return []; |
| 74 | + } |
| 75 | + if (!$exprType->isArray()->yes()) { |
| 76 | + return [ |
| 77 | + RuleErrorBuilder::message(sprintf('Cannot use array destructuring on %s.', $exprType->describe(VerbosityLevel::typeOnly())))->build(), |
| 78 | + ]; |
| 79 | + } |
| 80 | + |
| 81 | + $errors = []; |
| 82 | + $i = 0; |
| 83 | + foreach ($var->items as $item) { |
| 84 | + if ($item === null) { |
| 85 | + $i++; |
| 86 | + continue; |
| 87 | + } |
| 88 | + |
| 89 | + $keyExpr = null; |
| 90 | + if ($item->key === null) { |
| 91 | + $keyType = new ConstantIntegerType($i); |
| 92 | + $keyExpr = new Node\Scalar\LNumber($i); |
| 93 | + } else { |
| 94 | + $keyType = $scope->getType($item->key); |
| 95 | + if ($keyType instanceof ConstantIntegerType) { |
| 96 | + $keyExpr = new LNumber($keyType->getValue()); |
| 97 | + } elseif ($keyType instanceof ConstantStringType) { |
| 98 | + $keyExpr = new Node\Scalar\String_($keyType->getValue()); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + $itemErrors = $this->nonexistentOffsetInArrayDimFetchCheck->check( |
| 103 | + $scope, |
| 104 | + $expr, |
| 105 | + '', |
| 106 | + $keyType |
| 107 | + ); |
| 108 | + $errors = array_merge($errors, $itemErrors); |
| 109 | + |
| 110 | + if ($keyExpr === null) { |
| 111 | + $i++; |
| 112 | + continue; |
| 113 | + } |
| 114 | + |
| 115 | + if (!$item->value instanceof Node\Expr\List_ && !$item->value instanceof Node\Expr\Array_) { |
| 116 | + $i++; |
| 117 | + continue; |
| 118 | + } |
| 119 | + |
| 120 | + $errors = array_merge($errors, $this->getErrors( |
| 121 | + $scope, |
| 122 | + $item->value, |
| 123 | + new Expr\ArrayDimFetch($expr, $keyExpr) |
| 124 | + )); |
| 125 | + } |
| 126 | + |
| 127 | + return $errors; |
| 128 | + } |
| 129 | + |
| 130 | +} |
0 commit comments