Skip to content

Commit

Permalink
[Symfony 5.2] Add PropertyPathMapperToDataMapperRector (#4887)
Browse files Browse the repository at this point in the history
  • Loading branch information
simivar authored Dec 14, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent efb80cf commit 6f1eb0b
Showing 5 changed files with 198 additions and 0 deletions.
4 changes: 4 additions & 0 deletions config/set/symfony52.php
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@
use Rector\Renaming\ValueObject\RenameClassConstant;
use Rector\Symfony5\Rector\MethodCall\ReflectionExtractorEnableMagicCallExtractorRector;
use Rector\Symfony5\Rector\New_\PropertyAccessorCreationBooleanToFlagsRector;
use Rector\Symfony5\Rector\New_\PropertyPathMapperToDataMapperRector;
use Rector\Symfony5\Rector\StaticCall\BinaryFileResponseCreateToNewInstanceRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symplify\SymfonyPhpConfig\ValueObjectInliner;
@@ -19,6 +20,9 @@

$services = $containerConfigurator->services();

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

declare(strict_types=1);

namespace Rector\Symfony5\Rector\New_;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Name;
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\New_\PropertyPathMapperToDataMapperRector\PropertyPathMapperToDataMapperRectorTest
*/
final class PropertyPathMapperToDataMapperRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Migrate from PropertyPathMapper to DataMapper and PropertyPathAccessor', [
new CodeSample(
<<<'CODE_SAMPLE'
use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper;
class SomeClass
{
public function run()
{
return new PropertyPathMapper();
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper;
class SomeClass
{
public function run()
{
return 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 [New_::class];
}

/**
* @param New_ $node
*/
public function refactor(Node $node): ?Node
{
if ($this->shouldSkip($node)) {
return null;
}

return $this->generateNewInstances($node);
}

private function shouldSkip(New_ $new): bool
{
if (! $new->class instanceof Name) {
return true;
}

return ! $this->isName($new->class, 'Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper');
}

private function generateNewInstances(New_ $new): New_
{
$arguments = [];
if (isset($new->args[0])) {
$arguments = [$new->args[0]];
}

$new = new New_(
new FullyQualified('Symfony\Component\Form\Extension\Core\DataAccessor\PropertyPathAccessor'),
$arguments
);

return new New_(
new FullyQualified('Symfony\Component\Form\Extension\Core\DataMapper\DataMapper'),
[$this->createArg($new)]
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Rector\Symfony5\Tests\Rector\New_\PropertyPathMapperToDataMapperRector\Fixture;

use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper;
use Symfony\Component\PropertyAccess\PropertyAccessor;

class WithArgument
{
public function run()
{
return new PropertyPathMapper(new PropertyAccessor());
}
}

?>
-----
<?php

namespace Rector\Symfony5\Tests\Rector\New_\PropertyPathMapperToDataMapperRector\Fixture;

use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper;
use Symfony\Component\PropertyAccess\PropertyAccessor;

class WithArgument
{
public function run()
{
return new \Symfony\Component\Form\Extension\Core\DataMapper\DataMapper(new \Symfony\Component\Form\Extension\Core\DataAccessor\PropertyPathAccessor(new PropertyAccessor()));
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Rector\Symfony5\Tests\Rector\New_\PropertyPathMapperToDataMapperRector\Fixture;

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

class WithoutArgument
{
public function run()
{
return new PropertyPathMapper();
}
}

?>
-----
<?php

namespace Rector\Symfony5\Tests\Rector\New_\PropertyPathMapperToDataMapperRector\Fixture;

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

class WithoutArgument
{
public function run()
{
return 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,31 @@
<?php

declare(strict_types=1);

namespace Rector\Symfony5\Tests\Rector\New_\PropertyPathMapperToDataMapperRector;

use Iterator;
use Rector\Symfony5\Rector\New_\PropertyPathMapperToDataMapperRector;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

final class PropertyPathMapperToDataMapperRectorTest 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 PropertyPathMapperToDataMapperRector::class;
}
}

0 comments on commit 6f1eb0b

Please sign in to comment.