forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnderscoreToCamelCaseVariableNameRector.php
More file actions
202 lines (169 loc) · 5.98 KB
/
UnderscoreToCamelCaseVariableNameRector.php
File metadata and controls
202 lines (169 loc) · 5.98 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Utils\Rector;
use PhpParser\Comment\Doc;
use PhpParser\Node;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Namespace_;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\Php\ReservedKeywordAnalyzer;
use Rector\PhpParser\Node\FileNode;
use Rector\Rector\AbstractRector;
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
{
/**
* @see https://regex101.com/r/OtFn8I/1
*/
private const PARAM_NAME_REGEX = '#(?<paramPrefix>@param\s.*\s+\$)(?<paramName>%s)#ms';
private bool $hasChanged = false;
public function __construct(private readonly ReservedKeywordAnalyzer $reservedKeywordAnalyzer, private readonly PhpDocInfoFactory $phpDocInfoFactory)
{
}
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 list<string>
*/
public function getNodeTypes(): array
{
return [FileNode::class, Namespace_::class];
}
/**
* @param FileNode|Namespace_ $node
*/
public function refactor(Node $node): ?Node
{
if ($node instanceof FileNode && $node->isNamespaced()) {
// handled in Namespace_ node
return null;
}
if ($node->stmts === null) {
return null;
}
$this->hasChanged = false;
$this->traverseNodesWithCallable(
$node->stmts,
function (Node $subNode) {
if ($subNode instanceof Variable || $subNode instanceof ClassMethod || $subNode instanceof Function_ || $subNode instanceof Closure) {
$this->processRenameVariable($subNode);
}
return null;
},
);
if ($this->hasChanged) {
return $node;
}
return null;
}
/**
* @param FunctionLike|Variable $node
*/
private function processRenameVariable(Node $node): ?Variable
{
if ($node instanceof FunctionLike) {
if ($node instanceof Closure) {
foreach ($node->uses as $closureUse) {
$this->processRenameVariable($closureUse->var);
}
}
foreach ($node->params as $key => $param) {
$originalVariableName = $param->var->name;
$variable = $this->processRenameVariable($param->var);
if ($variable instanceof Variable) {
$node->params[$key]->var = $variable;
$this->updateDocblock($originalVariableName, $variable->name, $node);
}
}
return null;
}
$nodeName = $this->getName($node);
if ($nodeName === null) {
return null;
}
if ($this->reservedKeywordAnalyzer->isNativeVariable($nodeName)) {
return null;
}
$underscorePosition = strpos($nodeName, '_');
// underscore not found, or in the first char, skip
if ((int) $underscorePosition === 0) {
return null;
}
$replaceUnderscoreToSpace = str_replace('_', ' ', $nodeName);
$uppercaseFirstChar = ucwords($replaceUnderscoreToSpace);
$camelCaseName = lcfirst(str_replace(' ', '', $uppercaseFirstChar));
if ($camelCaseName === 'this') {
return null;
}
$node->name = $camelCaseName;
$this->hasChanged = true;
return $node;
}
private function updateDocblock(string $variableName, string $camelCaseName, ?FunctionLike $functionLike): void
{
if (! $functionLike instanceof FunctionLike) {
return;
}
$docComment = $functionLike->getDocComment();
if (! $docComment instanceof Doc) {
return;
}
$docCommentText = $docComment->getText();
if ($docCommentText === null) {
return;
}
if (preg_match(sprintf(self::PARAM_NAME_REGEX, $variableName), $docCommentText) !== 1) {
return;
}
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($functionLike);
$paramTagValueNodes = $phpDocInfo->getParamTagValueNodes();
foreach ($paramTagValueNodes as $paramTagValueNode) {
if ($paramTagValueNode->parameterName === '$' . $variableName) {
$paramTagValueNode->parameterName = '$' . $camelCaseName;
break;
}
}
$functionLike->setDocComment(new Doc($phpDocInfo->getPhpDocNode()->__toString()));
}
}