2021-10-14 15:34:01 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\Transform\Rector\Attribute;
|
2021-10-14 15:34:01 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Attribute;
|
|
|
|
use PhpParser\Node\Identifier;
|
|
|
|
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
|
2023-09-20 12:53:23 +00:00
|
|
|
use Rector\Core\PhpParser\Node\Value\ValueResolver;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\Core\Rector\AbstractRector;
|
|
|
|
use Rector\Transform\ValueObject\AttributeKeyToClassConstFetch;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2023-09-01 11:10:35 +00:00
|
|
|
use RectorPrefix202309\Webmozart\Assert\Assert;
|
2021-10-14 15:34:01 +00:00
|
|
|
/**
|
|
|
|
* @changelog https://github.com/doctrine/dbal/blob/3.1.x/src/Types/Types.php
|
|
|
|
*
|
2023-06-08 18:28:01 +00:00
|
|
|
* @api used in rector-doctrine
|
2021-10-14 15:34:01 +00:00
|
|
|
* @see \Rector\Tests\Transform\Rector\Attribute\AttributeKeyToClassConstFetchRector\AttributeKeyToClassConstFetchRectorTest
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class AttributeKeyToClassConstFetchRector extends AbstractRector implements ConfigurableRectorInterface
|
2021-10-14 15:34:01 +00:00
|
|
|
{
|
2023-09-20 12:53:23 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
|
|
|
* @var \Rector\Core\PhpParser\Node\Value\ValueResolver
|
|
|
|
*/
|
|
|
|
private $valueResolver;
|
2021-10-14 15:34:01 +00:00
|
|
|
/**
|
|
|
|
* @var AttributeKeyToClassConstFetch[]
|
|
|
|
*/
|
|
|
|
private $attributeKeysToClassConstFetches = [];
|
2023-09-20 12:53:23 +00:00
|
|
|
public function __construct(ValueResolver $valueResolver)
|
|
|
|
{
|
|
|
|
$this->valueResolver = $valueResolver;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2021-10-14 15:34:01 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Replace key value on specific attribute to class constant', [new ConfiguredCodeSample(<<<'CODE_SAMPLE'
|
2021-10-14 15:34:01 +00:00
|
|
|
use Doctrine\ORM\Mapping\Column;
|
|
|
|
|
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
#[Column(type: "string")]
|
|
|
|
public $name;
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
|
|
|
, <<<'CODE_SAMPLE'
|
|
|
|
use Doctrine\ORM\Mapping\Column;
|
|
|
|
use Doctrine\DBAL\Types\Types;
|
|
|
|
|
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
#[Column(type: Types::STRING)]
|
|
|
|
public $name;
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
2022-06-07 09:46:15 +00:00
|
|
|
, [new AttributeKeyToClassConstFetch('Doctrine\\ORM\\Mapping\\Column', 'type', 'Doctrine\\DBAL\\Types\\Types', ['string' => 'STRING'])])]);
|
2021-10-14 15:34:01 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @return array<class-string<Node>>
|
|
|
|
*/
|
|
|
|
public function getNodeTypes() : array
|
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [Attribute::class];
|
2021-10-14 15:34:01 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param Attribute $node
|
2021-10-14 15:34:01 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2021-10-14 15:34:01 +00:00
|
|
|
{
|
|
|
|
foreach ($this->attributeKeysToClassConstFetches as $attributeKeyToClassConstFetch) {
|
|
|
|
if (!$this->isName($node->name, $attributeKeyToClassConstFetch->getAttributeClass())) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
foreach ($node->args as $arg) {
|
|
|
|
$argName = $arg->name;
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$argName instanceof Identifier) {
|
2021-10-14 15:34:01 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!$this->isName($argName, $attributeKeyToClassConstFetch->getAttributeKey())) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$value = $this->valueResolver->getValue($arg->value);
|
|
|
|
$constName = $attributeKeyToClassConstFetch->getValuesToConstantsMap()[$value] ?? null;
|
|
|
|
if ($constName === null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$arg->value = $this->nodeFactory->createClassConstFetch($attributeKeyToClassConstFetch->getConstantClass(), $constName);
|
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
/**
|
2021-11-27 13:43:28 +00:00
|
|
|
* @param mixed[] $configuration
|
2021-10-14 15:34:01 +00:00
|
|
|
*/
|
2021-12-10 10:22:23 +00:00
|
|
|
public function configure(array $configuration) : void
|
2021-10-14 15:34:01 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
Assert::allIsAOf($configuration, AttributeKeyToClassConstFetch::class);
|
2022-02-18 01:46:17 +00:00
|
|
|
$this->attributeKeysToClassConstFetches = $configuration;
|
2021-10-14 15:34:01 +00:00
|
|
|
}
|
|
|
|
}
|