Skip to content

Commit

Permalink
[Php80] Validate ArrayItem on CurlyListNodeAnnotationToAttributeMapper (
Browse files Browse the repository at this point in the history
#2814)

* [Php80] Validate ArrayItemNode and ArrayItem on CurlyListNodeAnnotationToAttributeMapper

* phpstan

* [ci-review] Rector Rectify

* final touch: no need double cast

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Aug 21, 2022
1 parent 837f71e commit ffa1221
Showing 1 changed file with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Rector\PhpAttribute\Contract\AnnotationToAttributeMapperInterface;
use Rector\PhpAttribute\Enum\DocTagNodeState;
use Symfony\Contracts\Service\Attribute\Required;
use Webmozart\Assert\Assert;

/**
* @implements AnnotationToAttributeMapperInterface<CurlyListNode>
Expand Down Expand Up @@ -51,18 +52,23 @@ public function map($value): Array_
continue;
}

++$loop;
Assert::isInstanceOf($valueExpr, ArrayItem::class);

if (! is_numeric($arrayItemNode->key)) {
$arrayItems[] = $valueExpr;
continue;
}

$keyExpr = $loop !== $arrayItemNode->key && is_numeric($arrayItemNode->key)
? new LNumber((int) $arrayItemNode->key)
: null;
++$loop;

if ($valueExpr instanceof ArrayItem && $keyExpr instanceof LNumber) {
$valueExpr->key = $keyExpr;
$arrayItemNodeKey = (int) $arrayItemNode->key;
if ($loop === $arrayItemNodeKey) {
$arrayItems[] = $valueExpr;
} else {
$arrayItems[] = new ArrayItem($valueExpr, $keyExpr);
continue;
}

$valueExpr->key = new LNumber($arrayItemNodeKey);
$arrayItems[] = $valueExpr;
}

return new Array_($arrayItems);
Expand Down

0 comments on commit ffa1221

Please sign in to comment.