2021-10-14 15:34:01 +00:00
< ? php
declare ( strict_types = 1 );
namespace Rector\Transform\Rector\Attribute ;
use PhpParser\Node ;
use PhpParser\Node\Attribute ;
use PhpParser\Node\Identifier ;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface ;
use Rector\Core\Rector\AbstractRector ;
use Rector\Transform\ValueObject\AttributeKeyToClassConstFetch ;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample ;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition ;
2021-12-10 00:25:09 +00:00
use RectorPrefix20211210\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
*
* @ see \Rector\Tests\Transform\Rector\Attribute\AttributeKeyToClassConstFetchRector\AttributeKeyToClassConstFetchRectorTest
*/
final class AttributeKeyToClassConstFetchRector extends \Rector\Core\Rector\AbstractRector implements \Rector\Core\Contract\Rector\ConfigurableRectorInterface
{
/**
2021-12-10 00:25:09 +00:00
* @ deprecated
2021-10-14 15:34:01 +00:00
* @ var string
*/
public const ATTRIBUTE_KEYS_TO_CLASS_CONST_FETCHES = 'attribute_keys_to_class_const_fetches' ;
/**
* @ var AttributeKeyToClassConstFetch []
*/
private $attributeKeysToClassConstFetches = [];
public function getRuleDefinition () : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
{
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition ( 'Replace key value on specific attribute to class constant' , [ new \Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample ( <<< 'CODE_SAMPLE'
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
2021-12-06 21:12:03 +00:00
, [ new \Rector\Transform\ValueObject\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
{
return [ \PhpParser\Node\Attribute :: class ];
}
/**
2021-12-10 09:57:54 +00:00
* @ param \PhpParser\Node $node
2021-10-14 15:34:01 +00:00
*/
2021-12-10 09:57:54 +00:00
public function refactor ( $node ) : ? \PhpParser\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 ;
if ( ! $argName instanceof \PhpParser\Node\Identifier ) {
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 09:57:54 +00:00
public function configure ( $configuration ) : void
2021-10-14 15:34:01 +00:00
{
2021-11-28 17:01:20 +00:00
$attributeKeysToClassConstFetches = $configuration [ self :: ATTRIBUTE_KEYS_TO_CLASS_CONST_FETCHES ] ? ? $configuration ;
2021-12-10 00:25:09 +00:00
\RectorPrefix20211210\Webmozart\Assert\Assert :: allIsAOf ( $attributeKeysToClassConstFetches , \Rector\Transform\ValueObject\AttributeKeyToClassConstFetch :: class );
2021-10-14 15:34:01 +00:00
$this -> attributeKeysToClassConstFetches = $attributeKeysToClassConstFetches ;
}
}