13
13
14
14
namespace Utils \Rector ;
15
15
16
- use Nette \Utils \Strings ;
17
16
use PhpParser \Comment \Doc ;
18
17
use PhpParser \Node ;
19
18
use PhpParser \Node \Expr \Variable ;
20
19
use PhpParser \Node \Stmt \ClassMethod ;
21
20
use PhpParser \Node \Stmt \Function_ ;
22
21
use Rector \Core \Php \ReservedKeywordAnalyzer ;
23
22
use Rector \Core \Rector \AbstractRector ;
24
- use Rector \NodeTypeResolver \Node \AttributeKey ;
25
- use Symplify \PackageBuilder \Strings \StringFormatConverter ;
23
+ use Rector \NodeNestingScope \ParentFinder ;
26
24
use Symplify \RuleDocGenerator \ValueObject \CodeSample \CodeSample ;
27
25
use Symplify \RuleDocGenerator \ValueObject \RuleDefinition ;
28
26
@@ -43,16 +41,16 @@ final class UnderscoreToCamelCaseVariableNameRector extends AbstractRector
43
41
private $ reservedKeywordAnalyzer ;
44
42
45
43
/**
46
- * @var StringFormatConverter
44
+ * @var ParentFinder
47
45
*/
48
- private $ stringFormatConverter ;
46
+ private $ parentFinder ;
49
47
50
48
public function __construct (
51
49
ReservedKeywordAnalyzer $ reservedKeywordAnalyzer ,
52
- StringFormatConverter $ stringFormatConverter
50
+ ParentFinder $ parentFinder
53
51
) {
54
52
$ this ->reservedKeywordAnalyzer = $ reservedKeywordAnalyzer ;
55
- $ this ->stringFormatConverter = $ stringFormatConverter ;
53
+ $ this ->parentFinder = $ parentFinder ;
56
54
}
57
55
58
56
public function getRuleDefinition (): RuleDefinition
@@ -100,19 +98,20 @@ public function refactor(Node $node): ?Node
100
98
return null ;
101
99
}
102
100
103
- if (! Strings::contains ($ nodeName , '_ ' )) {
104
- return null ;
105
- }
106
-
107
101
if ($ this ->reservedKeywordAnalyzer ->isNativeVariable ($ nodeName )) {
108
102
return null ;
109
103
}
110
104
111
- if ($ nodeName [0 ] === '_ ' ) {
105
+ $ underscorePosition = strpos ($ nodeName , '_ ' );
106
+ // underscore not found, or in the first char, skip
107
+ if ((int ) $ underscorePosition === 0 ) {
112
108
return null ;
113
109
}
114
110
115
- $ camelCaseName = $ this ->stringFormatConverter ->underscoreAndHyphenToCamelCase ($ nodeName );
111
+ $ replaceUnderscoreToSpace = str_replace ('_ ' , ' ' , $ nodeName );
112
+ $ uppercaseFirstChar = ucwords ($ replaceUnderscoreToSpace );
113
+ $ camelCaseName = lcfirst (str_replace (' ' , '' , $ uppercaseFirstChar ));
114
+
116
115
if ($ camelCaseName === 'this ' ) {
117
116
return null ;
118
117
}
@@ -125,24 +124,13 @@ public function refactor(Node $node): ?Node
125
124
126
125
private function updateDocblock (Variable $ variable , string $ variableName , string $ camelCaseName ): void
127
126
{
128
- $ parentNode = $ variable ->getAttribute (AttributeKey::PARENT_NODE );
129
-
130
- while ($ parentNode ) {
131
- /**
132
- * @var ClassMethod|Function_ $parentNode
133
- */
134
- $ parentNode = $ parentNode ->getAttribute (AttributeKey::PARENT_NODE );
135
-
136
- if ($ parentNode instanceof ClassMethod || $ parentNode instanceof Function_) {
137
- break ;
138
- }
139
- }
127
+ $ parentClassMethodOrFunction = $ this ->parentFinder ->findByTypes ($ variable , [ClassMethod::class, Function_::class]);
140
128
141
- if ($ parentNode === null ) {
129
+ if ($ parentClassMethodOrFunction === null ) {
142
130
return ;
143
131
}
144
132
145
- $ docComment = $ parentNode ->getDocComment ();
133
+ $ docComment = $ parentClassMethodOrFunction ->getDocComment ();
146
134
if ($ docComment === null ) {
147
135
return ;
148
136
}
@@ -152,11 +140,11 @@ private function updateDocblock(Variable $variable, string $variableName, string
152
140
return ;
153
141
}
154
142
155
- if (! Strings:: match ( $ docCommentText , sprintf (self ::PARAM_NAME_REGEX , $ variableName ))) {
143
+ if (! preg_match ( sprintf (self ::PARAM_NAME_REGEX , $ variableName ), $ docCommentText )) {
156
144
return ;
157
145
}
158
146
159
- $ phpDocInfo = $ this ->phpDocInfoFactory ->createFromNodeOrEmpty ($ parentNode );
147
+ $ phpDocInfo = $ this ->phpDocInfoFactory ->createFromNodeOrEmpty ($ parentClassMethodOrFunction );
160
148
$ paramTagValueNodes = $ phpDocInfo ->getParamTagValueNodes ();
161
149
162
150
foreach ($ paramTagValueNodes as $ paramTagValueNode ) {
@@ -166,6 +154,6 @@ private function updateDocblock(Variable $variable, string $variableName, string
166
154
}
167
155
}
168
156
169
- $ parentNode ->setDocComment (new Doc ($ phpDocInfo ->getPhpDocNode ()->__toString ()));
157
+ $ parentClassMethodOrFunction ->setDocComment (new Doc ($ phpDocInfo ->getPhpDocNode ()->__toString ()));
170
158
}
171
159
}
0 commit comments