-
-
Notifications
You must be signed in to change notification settings - Fork 687
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 ValidatorBuilderEnableAnnotationMappingRector (#5357)
- Loading branch information
1 parent
4591ac6
commit e1b1d7d
Showing
8 changed files
with
212 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
97 changes: 97 additions & 0 deletions
97
rules/symfony5/src/Rector/MethodCall/ValidatorBuilderEnableAnnotationMappingRector.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,97 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Symfony5\Rector\MethodCall; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\MethodCall; | ||
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#validator | ||
* @see \Rector\Symfony5\Tests\Rector\MethodCall\ValidatorBuilderEnableAnnotationMappingRector\ValidatorBuilderEnableAnnotationMappingRectorTest | ||
*/ | ||
final class ValidatorBuilderEnableAnnotationMappingRector extends AbstractRector | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private const REQUIRED_TYPE = 'Symfony\Component\Validator\ValidatorBuilder'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private const ARG_OLD_TYPE = 'Doctrine\Common\Annotations\Reader'; | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition( | ||
'Migrates from deprecated ValidatorBuilder->enableAnnotationMapping($reader) to ValidatorBuilder->enableAnnotationMapping(true)->setDoctrineAnnotationReader($reader)', | ||
[ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
use Doctrine\Common\Annotations\Reader; | ||
use Symfony\Component\Validator\ValidatorBuilder; | ||
class SomeClass | ||
{ | ||
public function run(ValidatorBuilder $builder, Reader $reader) | ||
{ | ||
$builder->enableAnnotationMapping($reader); | ||
} | ||
} | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
use Doctrine\Common\Annotations\Reader; | ||
use Symfony\Component\Validator\ValidatorBuilder; | ||
class SomeClass | ||
{ | ||
public function run(ValidatorBuilder $builder, Reader $reader) | ||
{ | ||
$builder->enableAnnotationMapping(true)->setDoctrineAnnotationReader($reader); | ||
} | ||
} | ||
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, 'enableAnnotationMapping')) { | ||
return null; | ||
} | ||
|
||
if ($this->isBool($node->args[0]->value)) { | ||
return null; | ||
} | ||
|
||
if (! $this->isObjectType($node->args[0]->value, self::ARG_OLD_TYPE)) { | ||
return null; | ||
} | ||
|
||
$readerType = $node->args[0]->value; | ||
$node->args[0]->value = $this->createTrue(); | ||
return $this->createMethodCall($node, 'setDoctrineAnnotationReader', [$readerType]); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...s/Rector/MethodCall/ValidatorBuilderEnableAnnotationMappingRector/Fixture/fixture.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,29 @@ | ||
<?php | ||
|
||
namespace Rector\Symfony5\Tests\Rector\MethodCall\ValidatorBuilderEnableAnnotationMappingRector\Fixture; | ||
|
||
use Doctrine\Common\Annotations\Reader; | ||
use Symfony\Component\Validator\ValidatorBuilder; | ||
|
||
class Fixture | ||
{ | ||
public function run(ValidatorBuilder $builder, Reader $reader) | ||
{ | ||
$builder->enableAnnotationMapping($reader); | ||
} | ||
} | ||
----- | ||
<?php | ||
|
||
namespace Rector\Symfony5\Tests\Rector\MethodCall\ValidatorBuilderEnableAnnotationMappingRector\Fixture; | ||
|
||
use Doctrine\Common\Annotations\Reader; | ||
use Symfony\Component\Validator\ValidatorBuilder; | ||
|
||
class Fixture | ||
{ | ||
public function run(ValidatorBuilder $builder, Reader $reader) | ||
{ | ||
$builder->enableAnnotationMapping(true)->setDoctrineAnnotationReader($reader); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...or/MethodCall/ValidatorBuilderEnableAnnotationMappingRector/Fixture/skip_bool_arg.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,13 @@ | ||
<?php | ||
|
||
namespace Rector\Symfony5\Tests\Rector\MethodCall\ValidatorBuilderEnableAnnotationMappingRector\Fixture; | ||
|
||
use Symfony\Component\Validator\ValidatorBuilder; | ||
|
||
class SkipBoolArg | ||
{ | ||
public function run(ValidatorBuilder $builder) | ||
{ | ||
$builder->enableAnnotationMapping(true); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...orBuilderEnableAnnotationMappingRector/Fixture/skip_not_enable_annotation_mapping.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,13 @@ | ||
<?php | ||
|
||
namespace Rector\Symfony5\Tests\Rector\MethodCall\ValidatorBuilderEnableAnnotationMappingRector\Fixture; | ||
|
||
use Symfony\Component\Validator\ValidatorBuilder; | ||
|
||
class SkipNotEnableAnnotatinoMapping | ||
{ | ||
public function run(ValidatorBuilder $builder) | ||
{ | ||
$builder->foo(); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...hodCall/ValidatorBuilderEnableAnnotationMappingRector/Fixture/skip_not_reader_arg.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,13 @@ | ||
<?php | ||
|
||
namespace Rector\Symfony5\Tests\Rector\MethodCall\ValidatorBuilderEnableAnnotationMappingRector\Fixture; | ||
|
||
use Symfony\Component\Validator\ValidatorBuilder; | ||
|
||
class SkipNotReaderArg | ||
{ | ||
public function run(ValidatorBuilder $builder, $variable) | ||
{ | ||
$builder->enableAnnotationMapping($variable); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
.../ValidatorBuilderEnableAnnotationMappingRector/Fixture/skip_not_validator_builder.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,12 @@ | ||
<?php | ||
|
||
namespace Rector\Symfony5\Tests\Rector\MethodCall\ValidatorBuilderEnableAnnotationMappingRector\Fixture; | ||
|
||
class SkipNotValidatorBuilder | ||
{ | ||
public function run() | ||
{ | ||
$d = new \DateTime('now'); | ||
$d->format('Y-m-d'); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...uilderEnableAnnotationMappingRector/ValidatorBuilderEnableAnnotationMappingRectorTest.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\MethodCall\ValidatorBuilderEnableAnnotationMappingRector; | ||
|
||
use Iterator; | ||
use Rector\Symfony5\Rector\MethodCall\ValidatorBuilderEnableAnnotationMappingRector; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
final class ValidatorBuilderEnableAnnotationMappingRectorTest 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 ValidatorBuilderEnableAnnotationMappingRector::class; | ||
} | ||
} |