Skip to content

Commit

Permalink
[Php80] Preserve int key defined not start from 0 on AnnotationToAttr…
Browse files Browse the repository at this point in the history
…ibuteRector (#2735)
  • Loading branch information
samsonasik committed Aug 5, 2022
1 parent 502d0ad commit c88bd7a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Scalar\LNumber;
use Rector\BetterPhpDocParser\ValueObject\PhpDoc\DoctrineAnnotation\CurlyListNode;
use Rector\PhpAttribute\AnnotationToAttributeMapper;
use Rector\PhpAttribute\Contract\AnnotationToAttributeMapperInterface;
Expand Down Expand Up @@ -41,7 +42,10 @@ public function isCandidate(mixed $value): bool
public function map($value): Array_
{
$arrayItems = [];
foreach ($value->getValuesWithExplicitSilentAndWithoutQuotes() as $key => $singleValue) {
$valuesWithExplicitSilent = $value->getValuesWithExplicitSilentAndWithoutQuotes();
$loop = -1;

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

// remove node
Expand All @@ -51,10 +55,15 @@ public function map($value): Array_

Assert::isInstanceOf($valueExpr, Expr::class);

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

$keyExpr = $loop !== $key
? new LNumber($key)
: null;
}

$arrayItems[] = new ArrayItem($valueExpr, $keyExpr);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Fixture\Doctrine;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\DiscriminatorMap({ "1" = "CostDetailEntity" })
*/
class PreserveIntKeyDefined
{
}

?>
-----
<?php

namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Fixture\Doctrine;

use Doctrine\ORM\Mapping as ORM;

#[ORM\DiscriminatorMap([1 => 'CostDetailEntity'])]
class PreserveIntKeyDefined
{
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
new AnnotationToAttribute('Doctrine\ORM\Mapping\UniqueConstraint'),
new AnnotationToAttribute('Doctrine\ORM\Mapping\JoinColumns'),
new AnnotationToAttribute('Doctrine\ORM\Mapping\JoinColumn'),
new AnnotationToAttribute('Doctrine\ORM\Mapping\DiscriminatorMap'),

// validation
new AnnotationToAttribute('Symfony\Component\Validator\Constraints\All'),
Expand Down

0 comments on commit c88bd7a

Please sign in to comment.