- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 689
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Symfony 5.2] Add PropertyPathMapperToDataMapperRector (#4887)
- Loading branch information
Showing
5 changed files
with
198 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
rules/symfony5/src/Rector/New_/PropertyPathMapperToDataMapperRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)] | ||
); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...ony5/tests/Rector/New_/PropertyPathMapperToDataMapperRector/Fixture/with_argument.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} | ||
} | ||
|
||
?> |
31 changes: 31 additions & 0 deletions
31
...5/tests/Rector/New_/PropertyPathMapperToDataMapperRector/Fixture/without_argument.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
|
||
?> |
31 changes: 31 additions & 0 deletions
31
...or/New_/PropertyPathMapperToDataMapperRector/PropertyPathMapperToDataMapperRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |