Skip to content

Commit

Permalink
[Symfony 5.2] Add ValidatorBuilderEnableAnnotationMappingRector (#5357)
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik authored Jan 29, 2021
1 parent 4591ac6 commit e1b1d7d
Show file tree
Hide file tree
Showing 8 changed files with 212 additions and 0 deletions.
4 changes: 4 additions & 0 deletions config/set/symfony52.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Rector\Symfony5\Rector\MethodCall\DefinitionAliasSetPrivateToSetPublicRector;
use Rector\Symfony5\Rector\MethodCall\FormBuilderSetDataMapperRector;
use Rector\Symfony5\Rector\MethodCall\ReflectionExtractorEnableMagicCallExtractorRector;
use Rector\Symfony5\Rector\MethodCall\ValidatorBuilderEnableAnnotationMappingRector;
use Rector\Symfony5\Rector\New_\PropertyAccessorCreationBooleanToFlagsRector;
use Rector\Symfony5\Rector\New_\PropertyPathMapperToDataMapperRector;
use Rector\Symfony5\Rector\StaticCall\BinaryFileResponseCreateToNewInstanceRector;
Expand Down Expand Up @@ -116,4 +117,7 @@

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

# https://github.com/symfony/symfony/blob/5.x/UPGRADE-5.2.md#validator
$services->set(ValidatorBuilderEnableAnnotationMappingRector::class);
};
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]);
}
}
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);
}
}
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);
}
}
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();
}
}
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);
}
}
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');
}
}
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;
}
}

0 comments on commit e1b1d7d

Please sign in to comment.