2021-11-20 15:31:12 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\PhpAttribute\AnnotationToAttributeMapper;
|
2021-11-20 15:31:12 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Expr\Array_;
|
|
|
|
use PhpParser\Node\Expr\ArrayItem;
|
2022-08-05 14:06:15 +00:00
|
|
|
use PhpParser\Node\Scalar\LNumber;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\BetterPhpDocParser\ValueObject\PhpDoc\DoctrineAnnotation\CurlyListNode;
|
|
|
|
use Rector\PhpAttribute\AnnotationToAttributeMapper;
|
|
|
|
use Rector\PhpAttribute\Contract\AnnotationToAttributeMapperInterface;
|
|
|
|
use Rector\PhpAttribute\Enum\DocTagNodeState;
|
2023-06-01 08:56:46 +00:00
|
|
|
use RectorPrefix202306\Symfony\Contracts\Service\Attribute\Required;
|
|
|
|
use RectorPrefix202306\Webmozart\Assert\Assert;
|
2021-11-20 15:31:12 +00:00
|
|
|
/**
|
|
|
|
* @implements AnnotationToAttributeMapperInterface<CurlyListNode>
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class CurlyListNodeAnnotationToAttributeMapper implements AnnotationToAttributeMapperInterface
|
2021-11-20 15:31:12 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \Rector\PhpAttribute\AnnotationToAttributeMapper
|
|
|
|
*/
|
|
|
|
private $annotationToAttributeMapper;
|
|
|
|
/**
|
|
|
|
* Avoid circular reference
|
|
|
|
* @required
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function autowire(AnnotationToAttributeMapper $annotationToAttributeMapper) : void
|
2021-11-20 15:31:12 +00:00
|
|
|
{
|
|
|
|
$this->annotationToAttributeMapper = $annotationToAttributeMapper;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param mixed $value
|
|
|
|
*/
|
|
|
|
public function isCandidate($value) : bool
|
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return $value instanceof CurlyListNode;
|
2021-11-20 15:31:12 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param CurlyListNode $value
|
|
|
|
*/
|
2022-06-06 17:12:56 +00:00
|
|
|
public function map($value) : \PhpParser\Node\Expr
|
2021-11-20 15:31:12 +00:00
|
|
|
{
|
2022-02-05 10:39:52 +00:00
|
|
|
$arrayItems = [];
|
2022-08-20 21:58:41 +00:00
|
|
|
$arrayItemNodes = $value->getValues();
|
2022-08-05 14:06:15 +00:00
|
|
|
$loop = -1;
|
2022-08-20 21:58:41 +00:00
|
|
|
foreach ($arrayItemNodes as $arrayItemNode) {
|
|
|
|
$valueExpr = $this->annotationToAttributeMapper->map($arrayItemNode);
|
2022-02-05 10:39:52 +00:00
|
|
|
// remove node
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($valueExpr === DocTagNodeState::REMOVE_ARRAY) {
|
2022-02-05 10:39:52 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-08-21 14:29:36 +00:00
|
|
|
Assert::isInstanceOf($valueExpr, ArrayItem::class);
|
|
|
|
if (!\is_numeric($arrayItemNode->key)) {
|
|
|
|
$arrayItems[] = $valueExpr;
|
|
|
|
continue;
|
|
|
|
}
|
2022-08-20 21:58:41 +00:00
|
|
|
++$loop;
|
2022-08-21 14:29:36 +00:00
|
|
|
$arrayItemNodeKey = (int) $arrayItemNode->key;
|
|
|
|
if ($loop === $arrayItemNodeKey) {
|
2022-08-20 21:58:41 +00:00
|
|
|
$arrayItems[] = $valueExpr;
|
2022-08-21 14:29:36 +00:00
|
|
|
continue;
|
2022-02-05 10:39:52 +00:00
|
|
|
}
|
2022-08-21 14:29:36 +00:00
|
|
|
$valueExpr->key = new LNumber($arrayItemNodeKey);
|
|
|
|
$arrayItems[] = $valueExpr;
|
2022-02-05 10:39:52 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
return new Array_($arrayItems);
|
2021-11-20 15:31:12 +00:00
|
|
|
}
|
|
|
|
}
|