-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
UnderscoreToCamelCaseVariableNameRector.php
175 lines (151 loc) · 3.73 KB
/
UnderscoreToCamelCaseVariableNameRector.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
declare(strict_types=1);
namespace Utils\Rector;
use Nette\Utils\Strings;
use PhpParser\Comment\Doc;
use PhpParser\Node;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use Rector\Core\Php\ReservedKeywordAnalyzer;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\PackageBuilder\Strings\StringFormatConverter;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* Adapted from https://github.com/rectorphp/rector/blob/4578e6d8490c1acfbf59bb17c4537a672fa77193/rules/naming/src/Rector/Variable/UnderscoreToCamelCaseVariableNameRector.php
* with skip _ in first character\
*/
final class UnderscoreToCamelCaseVariableNameRector extends AbstractRector
{
/**
* @var string
* @see https://regex101.com/r/OtFn8I/1
*/
private const PARAM_NAME_REGEX = '#(?<paramPrefix>@param\s.*\s+\$)(?<paramName>%s)#ms';
/**
* @var ReservedKeywordAnalyzer
*/
private $reservedKeywordAnalyzer;
/**
* @var StringFormatConverter
*/
private $stringFormatConverter;
public function __construct(
ReservedKeywordAnalyzer $reservedKeywordAnalyzer,
StringFormatConverter $stringFormatConverter
)
{
$this->reservedKeywordAnalyzer = $reservedKeywordAnalyzer;
$this->stringFormatConverter = $stringFormatConverter;
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Change under_score names to camelCase', [
new CodeSample(
<<<'CODE_SAMPLE'
final class SomeClass
{
public function run($a_b)
{
$some_value = $a_b;
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
final class SomeClass
{
public function run($aB)
{
$someValue = $aB;
}
}
CODE_SAMPLE
),
]);
}
/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [Variable::class];
}
/**
* @param Variable $node
*/
public function refactor(Node $node): ?Node
{
$nodeName = $this->getName($node);
if ($nodeName === null)
{
return null;
}
if (! Strings::contains($nodeName, '_'))
{
return null;
}
if ($this->reservedKeywordAnalyzer->isNativeVariable($nodeName))
{
return null;
}
if ($nodeName[0] === '_')
{
return null;
}
$camelCaseName = $this->stringFormatConverter->underscoreAndHyphenToCamelCase($nodeName);
if ($camelCaseName === 'this')
{
return null;
}
$node->name = $camelCaseName;
$this->updateDocblock($node, $nodeName, $camelCaseName);
return $node;
}
private function updateDocblock(Variable $variable, string $variableName, string $camelCaseName): void
{
$parentNode = $variable->getAttribute(AttributeKey::PARENT_NODE);
while ($parentNode)
{
/**
* @var ClassMethod|Function_ $parentNode
*/
$parentNode = $parentNode->getAttribute(AttributeKey::PARENT_NODE);
if ($parentNode instanceof ClassMethod || $parentNode instanceof Function_)
{
break;
}
}
if ($parentNode === null)
{
return;
}
$docComment = $parentNode->getDocComment();
if ($docComment === null)
{
return;
}
$docCommentText = $docComment->getText();
if ($docCommentText === null)
{
return;
}
if (! Strings::match($docCommentText, sprintf(self::PARAM_NAME_REGEX, $variableName)))
{
return;
}
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($parentNode);
$paramTagValueNodes = $phpDocInfo->getParamTagValueNodes();
foreach ($paramTagValueNodes as $paramTagValueNode)
{
if ($paramTagValueNode->parameterName === '$' . $variableName)
{
$paramTagValueNode->parameterName = '$' . $camelCaseName;
break;
}
}
$parentNode->setDocComment(new Doc($phpDocInfo->getPhpDocNode()->__toString()));
}
}