2021-05-10 00:23:30 +00:00
< ? php
declare ( strict_types = 1 );
2022-06-06 17:12:56 +00:00
namespace Rector\Symfony\Rector\MethodCall ;
2021-05-10 00:23:30 +00:00
2022-06-06 17:12:56 +00:00
use PhpParser\Node ;
use PhpParser\Node\Arg ;
use PhpParser\Node\Expr\MethodCall ;
use PhpParser\Node\Expr\New_ ;
use PhpParser\Node\Name\FullyQualified ;
use PHPStan\Type\ObjectType ;
use Rector\Core\Rector\AbstractRector ;
2022-06-07 09:18:30 +00:00
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample ;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition ;
2021-05-10 00:23:30 +00:00
/**
* @ see https :// github . com / symfony / symfony / blob / 5. x / UPGRADE - 5.2 . md #form
* @ see \Rector\Symfony\Tests\Rector\MethodCall\FormBuilderSetDataMapperRector\FormBuilderSetDataMapperRectorTest
*/
2022-06-07 08:22:29 +00:00
final class FormBuilderSetDataMapperRector extends AbstractRector
2021-05-10 00:23:30 +00:00
{
2022-06-14 06:49:40 +00:00
/**
* @ var string
*/
private const DATAMAPPER_INTERFACE = 'Symfony\\Component\\Form\\DataMapperInterface' ;
/**
* @ var string
*/
private const DATAMAPPER_CLASS = 'Symfony\\Component\\Form\\Extension\\Core\\DataMapper\\DataMapper' ;
/**
* @ readonly
* @ var \PHPStan\Type\ObjectType
*/
private $dataMapperInterface ;
2021-05-10 00:23:30 +00:00
/**
2022-01-07 00:57:38 +00:00
* @ readonly
2021-08-23 00:20:32 +00:00
* @ var \PHPStan\Type\ObjectType
2021-05-10 00:23:30 +00:00
*/
private $dataMapperObjectType ;
public function __construct ()
{
2022-06-14 06:49:40 +00:00
$this -> dataMapperInterface = new ObjectType ( self :: DATAMAPPER_INTERFACE );
$this -> dataMapperObjectType = new ObjectType ( self :: DATAMAPPER_CLASS );
2021-05-10 00:23:30 +00:00
}
2022-06-07 08:22:29 +00:00
public function getRuleDefinition () : RuleDefinition
2021-05-10 00:23:30 +00:00
{
2022-06-07 08:22:29 +00:00
return new RuleDefinition ( 'Migrates from deprecated Form Builder->setDataMapper(new PropertyPathMapper()) to Builder->setDataMapper(new DataMapper(new PropertyPathAccessor()))' , [ new CodeSample ( <<< 'CODE_SAMPLE'
2021-05-10 00:23:30 +00:00
use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper ;
use Symfony\Component\Form\FormConfigBuilderInterface ;
class SomeClass
{
public function run ( FormConfigBuilderInterface $builder )
{
$builder -> setDataMapper ( new PropertyPathMapper ());
}
}
CODE_SAMPLE
, <<< 'CODE_SAMPLE'
use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper ;
use Symfony\Component\Form\FormConfigBuilderInterface ;
use Symfony\Component\Form\Extension\Core\DataMapper\DataMapper ;
use Symfony\Component\Form\Extension\Core\DataAccessor\PropertyPathAccessor ;
class SomeClass
{
public function run ( FormConfigBuilderInterface $builder )
{
$builder -> setDataMapper ( new DataMapper ( new PropertyPathAccessor ()));
}
}
CODE_SAMPLE
)]);
}
/**
* @ return array < class - string < Node >>
*/
public function getNodeTypes () : array
{
2022-06-07 08:22:29 +00:00
return [ MethodCall :: class ];
2021-05-10 00:23:30 +00:00
}
/**
2021-12-10 10:22:23 +00:00
* @ param MethodCall $node
2021-05-10 00:23:30 +00:00
*/
2022-06-07 08:22:29 +00:00
public function refactor ( Node $node ) : ? Node
2021-05-10 00:23:30 +00:00
{
2022-06-07 09:46:15 +00:00
if ( ! $this -> isObjectType ( $node -> var , new ObjectType ( 'Symfony\\Component\\Form\\FormConfigBuilderInterface' ))) {
2021-05-10 00:23:30 +00:00
return null ;
}
if ( ! $this -> isName ( $node -> name , 'setDataMapper' )) {
return null ;
}
2022-01-07 00:57:38 +00:00
$argumentValue = $node -> getArgs ()[ 0 ] -> value ;
2022-06-14 06:49:40 +00:00
if ( $this -> isObjectType ( $argumentValue , $this -> dataMapperInterface ) || $this -> isObjectType ( $argumentValue , $this -> dataMapperObjectType )) {
2021-05-10 00:23:30 +00:00
return null ;
}
2022-06-07 09:46:15 +00:00
$propertyPathAccessor = new New_ ( new FullyQualified ( 'Symfony\\Component\\Form\\Extension\\Core\\DataAccessor\\PropertyPathAccessor' ));
2022-06-14 06:49:40 +00:00
$newArgumentValue = new New_ ( new FullyQualified ( self :: DATAMAPPER_CLASS ), [ new Arg ( $propertyPathAccessor )]);
2022-01-07 00:57:38 +00:00
$node -> getArgs ()[ 0 ] -> value = $newArgumentValue ;
2021-05-10 00:23:30 +00:00
return $node ;
}
}