forked from rectorphp/rector-symfony
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemoveDefaultGetBlockPrefixRector.php
144 lines (120 loc) · 3.77 KB
/
RemoveDefaultGetBlockPrefixRector.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
declare(strict_types=1);
namespace Rector\Symfony\Symfony30\Rector\ClassMethod;
use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Return_;
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/3.4/UPGRADE-3.0.md#form
*
* @see \Rector\Symfony\Tests\Symfony30\Rector\ClassMethod\RemoveDefaultGetBlockPrefixRector\RemoveDefaultGetBlockPrefixRectorTest
*/
final class RemoveDefaultGetBlockPrefixRector extends AbstractRector
{
public function __construct(
private readonly ValueResolver $valueResolver
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Rename `getBlockPrefix()` if it returns the default value - class to underscore, e.g. UserFormType = user_form',
[
new CodeSample(
<<<'CODE_SAMPLE'
use Symfony\Component\Form\AbstractType;
class TaskType extends AbstractType
{
public function getBlockPrefix()
{
return 'task';
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use Symfony\Component\Form\AbstractType;
class TaskType extends AbstractType
{
}
CODE_SAMPLE
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Class_::class];
}
/**
* @param Class_ $node
*/
public function refactor(Node $node)
{
if (! $node->extends instanceof Name) {
return null;
}
// work only with direct parent, as other can provide aliases on purpose
if (! $this->isName($node->extends, 'Symfony\Component\Form\AbstractType')) {
return null;
}
foreach ($node->stmts as $key => $classStmt) {
if (! $classStmt instanceof ClassMethod) {
continue;
}
if (! $this->isName($classStmt, 'getBlockPrefix')) {
continue;
}
$returnedExpr = $this->resolveOnlyStmtReturnExpr($classStmt);
if (! $returnedExpr instanceof Expr) {
return null;
}
$returnedValue = $this->valueResolver->getValue($returnedExpr);
$className = $this->nodeNameResolver->getName($node);
if (! is_string($className)) {
continue;
}
$shortClassName = $this->nodeNameResolver->getShortName($className);
if (\str_ends_with($shortClassName, 'Type')) {
$shortClassName = (string) Strings::before($shortClassName, 'Type');
}
$underscoredClassShortName = $this->camelToSnake($shortClassName);
if ($underscoredClassShortName !== $returnedValue) {
continue;
}
// remove method as unused
unset($node->stmts[$key]);
return $node;
}
return null;
}
private function camelToSnake(string $content): string
{
return mb_strtolower(Strings::replace($content, '#([a-z])([A-Z])#', '$1_$2'));
}
/**
* return <$thisValue>;
*/
private function resolveOnlyStmtReturnExpr(ClassMethod $classMethod): ?Expr
{
if (count((array) $classMethod->stmts) !== 1) {
return null;
}
$onlyStmt = $classMethod->stmts[0] ?? null;
if (! $onlyStmt instanceof Return_) {
return null;
}
return $onlyStmt->expr;
}
}