forked from rectorphp/rector-symfony
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidatorBuilderEnableAnnotationMappingRector.php
102 lines (87 loc) · 2.86 KB
/
ValidatorBuilderEnableAnnotationMappingRector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
declare(strict_types=1);
namespace Rector\Symfony\Symfony52\Rector\MethodCall;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Type\ObjectType;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @changelog https://github.com/symfony/symfony/blob/5.x/UPGRADE-5.2.md#validator
* @see \Rector\Symfony\Tests\Symfony52\Rector\MethodCall\ValidatorBuilderEnableAnnotationMappingRector\ValidatorBuilderEnableAnnotationMappingRectorTest
*/
final class ValidatorBuilderEnableAnnotationMappingRector extends AbstractRector
{
public function __construct(
private readonly ValueResolver $valueResolver
) {
}
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 array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [MethodCall::class];
}
/**
* @param MethodCall $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->isName($node->name, 'enableAnnotationMapping')) {
return null;
}
if (! $this->isObjectType($node->var, new ObjectType('Symfony\Component\Validator\ValidatorBuilder'))) {
return null;
}
$firstArg = $node->args[0];
if (! $firstArg instanceof Arg) {
return null;
}
if ($this->valueResolver->isTrueOrFalse($firstArg->value)) {
return null;
}
if (! $this->isObjectType($firstArg->value, new ObjectType('Doctrine\Common\Annotations\Reader'))) {
return null;
}
$readerType = $firstArg->value;
$firstArg->value = $this->nodeFactory->createTrue();
return $this->nodeFactory->createMethodCall($node, 'setDoctrineAnnotationReader', [$readerType]);
}
}