rector/packages/PhpAttribute/NodeFactory/DoctrineAnnotationFactory.php
Tomas Votruba 727b9f46f0 Updated Rector to commit bfa1891c50677b01136a9308fd3c3ecc12e267d9
bfa1891c50 [cleanup] Remove 73 unused public methods (#3245)
2022-12-23 17:10:25 +00:00

56 lines
1.7 KiB
PHP

<?php
declare (strict_types=1);
namespace Rector\PhpAttribute\NodeFactory;
use PhpParser\Node\Arg;
use PhpParser\Node\Attribute;
use PhpParser\Node\Scalar\String_;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode;
use Rector\Core\Contract\PhpParser\NodePrinterInterface;
use Rector\NodeTypeResolver\Node\AttributeKey;
final class DoctrineAnnotationFactory
{
/**
* @readonly
* @var \Rector\Core\Contract\PhpParser\NodePrinterInterface
*/
private $nodePrinter;
public function __construct(NodePrinterInterface $nodePrinter)
{
$this->nodePrinter = $nodePrinter;
}
/**
* @api downgrade
*/
public function createFromAttribute(Attribute $attribute, string $className) : DoctrineAnnotationTagValueNode
{
$items = $this->createItemsFromArgs($attribute->args);
$identifierTypeNode = new IdentifierTypeNode($className);
return new DoctrineAnnotationTagValueNode($identifierTypeNode, null, $items);
}
/**
* @param Arg[] $args
* @return mixed[]
*/
private function createItemsFromArgs(array $args) : array
{
$items = [];
foreach ($args as $arg) {
if ($arg->value instanceof String_) {
// standardize double quotes for annotations
$arg->value->setAttribute(AttributeKey::KIND, String_::KIND_DOUBLE_QUOTED);
}
$itemValue = $this->nodePrinter->print($arg->value);
if ($arg->name !== null) {
$name = $this->nodePrinter->print($arg->name);
$items[$name] = $itemValue;
} else {
$items[] = $itemValue;
}
}
return $items;
}
}