Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Symfony 5.2] Add FormBuilderSetDataMapperRector #5339

Merged
merged 15 commits into from
Jan 28, 2021
Merged
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"nette/utils": "^3.2",
"nikic/php-parser": "^4.10.4",
"phpstan/phpdoc-parser": "^0.4.9",
"phpstan/phpstan": "^0.12.64",
"phpstan/phpstan": "^0.12.64, <0.12.70",
"phpstan/phpstan-phpunit": "^0.12.17",
"psr/simple-cache": "^1.0",
"sebastian/diff": "^4.0",
Expand Down
4 changes: 4 additions & 0 deletions config/set/symfony52.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Rector\Renaming\ValueObject\MethodCallRename;
use Rector\Renaming\ValueObject\RenameClassAndConstFetch;
use Rector\Symfony5\Rector\MethodCall\DefinitionAliasSetPrivateToSetPublicRector;
use Rector\Symfony5\Rector\MethodCall\FormBuilderSetDataMapperRector;
use Rector\Symfony5\Rector\MethodCall\ReflectionExtractorEnableMagicCallExtractorRector;
use Rector\Symfony5\Rector\New_\PropertyAccessorCreationBooleanToFlagsRector;
use Rector\Symfony5\Rector\New_\PropertyPathMapperToDataMapperRector;
Expand Down Expand Up @@ -112,4 +113,7 @@

# https://github.com/symfony/symfony/blob/5.x/UPGRADE-5.2.md#dependencyinjection
$services->set(DefinitionAliasSetPrivateToSetPublicRector::class);

# https://github.com/symfony/symfony/blob/5.x/UPGRADE-5.2.md#form
$services->set(FormBuilderSetDataMapperRector::class);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

declare(strict_types=1);

namespace Rector\Symfony5\Rector\MethodCall;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Name\FullyQualified;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see https://github.com/symfony/symfony/blob/5.x/UPGRADE-5.2.md#form
* @see \Rector\Symfony5\Tests\Rector\MethodCall\FormBuilderSetDataMapperRector\FormBuilderSetDataMapperRectorTest
*/
final class FormBuilderSetDataMapperRector extends AbstractRector
{
/**
* @var string
*/
private const REQUIRED_TYPE = 'Symfony\Component\Form\FormConfigBuilderInterface';

/**
* @var string
*/
private const ARG_CORRECT_TYPE = 'Symfony\Component\Form\Extension\Core\DataMapper\DataMapper';

/**
* @var string
*/
private const ARG_MAPPER_TYPE = 'Symfony\Component\Form\Extension\Core\DataAccessor\PropertyPathAccessor';

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Migrates from deprecated Form Builder->setDataMapper(new PropertyPathMapper()) to Builder->setDataMapper(new DataMapper(new PropertyPathAccessor()))',
[
new CodeSample(
<<<'CODE_SAMPLE'
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;

class SomeClass
{
public function run(FormConfigBuilderInterface $builder)
{
$builder->setDataMapper(new \Symfony\Component\Form\Extension\Core\DataMapper\DataMapper(new \Symfony\Component\Form\Extension\Core\DataAccessor\PropertyPathAccessor()));
}
}
CODE_SAMPLE
),
]);
}

/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [MethodCall::class];
}

/**
* @param MethodCall $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->isObjectType($node->var, self::REQUIRED_TYPE)) {
return null;
}

if (! $this->isName($node->name, 'setDataMapper')) {
return null;
}

$argumentValue = $node->args[0]->value;
if ($this->isObjectType($argumentValue, self::ARG_CORRECT_TYPE)) {
return null;
}

$propertyPathAccessor = new New_(new FullyQualified(self::ARG_MAPPER_TYPE));
$newArgumentValue = new New_(new FullyQualified(self::ARG_CORRECT_TYPE), [new Arg($propertyPathAccessor)]);
$node->args[0]->value = $newArgumentValue;

return $node;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Rector\Symfony5\Tests\Rector\MethodCall\FormBuilderSetDataMapperRector\Fixture;

use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper;
use Symfony\Component\Form\FormConfigBuilderInterface;

class Fixture
{
public function run(FormConfigBuilderInterface $builder)
{
$builder->setDataMapper(new PropertyPathMapper());
}
}
-----
<?php

namespace Rector\Symfony5\Tests\Rector\MethodCall\FormBuilderSetDataMapperRector\Fixture;

use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper;
use Symfony\Component\Form\FormConfigBuilderInterface;

class Fixture
{
public function run(FormConfigBuilderInterface $builder)
{
$builder->setDataMapper(new \Symfony\Component\Form\Extension\Core\DataMapper\DataMapper(new \Symfony\Component\Form\Extension\Core\DataAccessor\PropertyPathAccessor()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Rector\Symfony5\Tests\Rector\MethodCall\FormBuilderSetDataMapperRector\Fixture;

use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper;
use Symfony\Component\Form\FormConfigBuilderInterface;

class SkipCorrectArg
{
public function run(FormConfigBuilderInterface $builder)
{
$propertyPathAccessor = new \Symfony\Component\Form\Extension\Core\DataAccessor\PropertyPathAccessor();
$builder->setDataMapper(new \Symfony\Component\Form\Extension\Core\DataMapper\DataMapper($propertyPathAccessor));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Rector\Symfony5\Tests\Rector\MethodCall\FormBuilderSetDataMapperRector\Fixture;

class SkipNotFormBuilder
{
public function run()
{
$d = new \DateTime('now');
$d->format('Y-m-d');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Symfony5\Tests\Rector\MethodCall\FormBuilderSetDataMapperRector\Fixture;

use Symfony\Component\Form\FormConfigBuilderInterface;

class SkipNotSetDataMapper
{
public function run(FormConfigBuilderInterface $builder)
{
$builder->foo();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Rector\Symfony5\Tests\Rector\MethodCall\FormBuilderSetDataMapperRector;

use Iterator;
use Rector\Symfony5\Rector\MethodCall\FormBuilderSetDataMapperRector;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

final class FormBuilderSetDataMapperRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}

public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

protected function getRectorClass(): string
{
return FormBuilderSetDataMapperRector::class;
}
}