Skip to content

Commit

Permalink
[Php80] Rollback preserve int key defined not start from 0 Fixture on…
Browse files Browse the repository at this point in the history
… AnnotationToAttributeRector (#2811)

* [Php80] Rollback preserver int key defined Fixture on AnnotationToAttributeRector

* mark as single quoted 1

* Fixed 🎉

* [ci-review] Rector Rectify

* final touch: variable clean up

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Aug 20, 2022
1 parent 8e75310 commit 790ca8c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Rector\PhpAttribute\AnnotationToAttributeMapper;

use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Scalar\LNumber;
Expand All @@ -13,7 +12,6 @@
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 @@ -42,29 +40,29 @@ public function isCandidate(mixed $value): bool
public function map($value): Array_
{
$arrayItems = [];
$valuesWithExplicitSilent = $value->getValues();
$arrayItemNodes = $value->getValues();
$loop = -1;

foreach ($valuesWithExplicitSilent as $key => $singleValue) {
$valueExpr = $this->annotationToAttributeMapper->map($singleValue);
foreach ($arrayItemNodes as $arrayItemNode) {
$valueExpr = $this->annotationToAttributeMapper->map($arrayItemNode);

// remove node
if ($valueExpr === DocTagNodeState::REMOVE_ARRAY) {
continue;
}

if (! is_int($key)) {
$keyExpr = $this->annotationToAttributeMapper->map($key);
Assert::isInstanceOf($keyExpr, Expr::class);
} else {
++$loop;
++$loop;

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

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

return new Array_($arrayItems);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Fixture\D
use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\DiscriminatorMap({
* "cost" = "CostEntity",
* "product" = "ProductEntity",
* })
* @ORM\DiscriminatorMap({ "1" = "CostDetailEntity" })
*/
class PreserveIntKeyDefined
{
Expand All @@ -22,7 +19,7 @@ namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Fixture\D

use Doctrine\ORM\Mapping as ORM;

#[ORM\DiscriminatorMap(['cost' => 'CostEntity', 'product' => 'ProductEntity'])]
#[ORM\DiscriminatorMap([1 => 'CostDetailEntity'])]
class PreserveIntKeyDefined
{
}
Expand Down

0 comments on commit 790ca8c

Please sign in to comment.