1313
1414namespace Utils \Rector ;
1515
16- use Nette \Utils \Strings ;
1716use PhpParser \Comment \Doc ;
1817use PhpParser \Node ;
1918use PhpParser \Node \Expr \Variable ;
2019use PhpParser \Node \Stmt \ClassMethod ;
2120use PhpParser \Node \Stmt \Function_ ;
2221use Rector \Core \Php \ReservedKeywordAnalyzer ;
2322use Rector \Core \Rector \AbstractRector ;
24- use Rector \NodeTypeResolver \Node \AttributeKey ;
25- use Symplify \PackageBuilder \Strings \StringFormatConverter ;
23+ use Rector \NodeNestingScope \ParentFinder ;
2624use Symplify \RuleDocGenerator \ValueObject \CodeSample \CodeSample ;
2725use Symplify \RuleDocGenerator \ValueObject \RuleDefinition ;
2826
@@ -43,16 +41,16 @@ final class UnderscoreToCamelCaseVariableNameRector extends AbstractRector
4341 private $ reservedKeywordAnalyzer ;
4442
4543 /**
46- * @var StringFormatConverter
44+ * @var ParentFinder
4745 */
48- private $ stringFormatConverter ;
46+ private $ parentFinder ;
4947
5048 public function __construct (
5149 ReservedKeywordAnalyzer $ reservedKeywordAnalyzer ,
52- StringFormatConverter $ stringFormatConverter
50+ ParentFinder $ parentFinder
5351 ) {
5452 $ this ->reservedKeywordAnalyzer = $ reservedKeywordAnalyzer ;
55- $ this ->stringFormatConverter = $ stringFormatConverter ;
53+ $ this ->parentFinder = $ parentFinder ;
5654 }
5755
5856 public function getRuleDefinition (): RuleDefinition
@@ -100,19 +98,20 @@ public function refactor(Node $node): ?Node
10098 return null ;
10199 }
102100
103- if (! Strings::contains ($ nodeName , '_ ' )) {
104- return null ;
105- }
106-
107101 if ($ this ->reservedKeywordAnalyzer ->isNativeVariable ($ nodeName )) {
108102 return null ;
109103 }
110104
111- if ($ nodeName [0 ] === '_ ' ) {
105+ $ underscorePosition = strpos ($ nodeName , '_ ' );
106+ // underscore not found, or in the first char, skip
107+ if ((int ) $ underscorePosition === 0 ) {
112108 return null ;
113109 }
114110
115- $ camelCaseName = $ this ->stringFormatConverter ->underscoreAndHyphenToCamelCase ($ nodeName );
111+ $ replaceUnderscoreToSpace = str_replace ('_ ' , ' ' , $ nodeName );
112+ $ uppercaseFirstChar = ucwords ($ replaceUnderscoreToSpace );
113+ $ camelCaseName = lcfirst (str_replace (' ' , '' , $ uppercaseFirstChar ));
114+
116115 if ($ camelCaseName === 'this ' ) {
117116 return null ;
118117 }
@@ -125,24 +124,13 @@ public function refactor(Node $node): ?Node
125124
126125 private function updateDocblock (Variable $ variable , string $ variableName , string $ camelCaseName ): void
127126 {
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]);
140128
141- if ($ parentNode === null ) {
129+ if ($ parentClassMethodOrFunction === null ) {
142130 return ;
143131 }
144132
145- $ docComment = $ parentNode ->getDocComment ();
133+ $ docComment = $ parentClassMethodOrFunction ->getDocComment ();
146134 if ($ docComment === null ) {
147135 return ;
148136 }
@@ -152,11 +140,11 @@ private function updateDocblock(Variable $variable, string $variableName, string
152140 return ;
153141 }
154142
155- if (! Strings:: match ( $ docCommentText , sprintf (self ::PARAM_NAME_REGEX , $ variableName ))) {
143+ if (! preg_match ( sprintf (self ::PARAM_NAME_REGEX , $ variableName ), $ docCommentText )) {
156144 return ;
157145 }
158146
159- $ phpDocInfo = $ this ->phpDocInfoFactory ->createFromNodeOrEmpty ($ parentNode );
147+ $ phpDocInfo = $ this ->phpDocInfoFactory ->createFromNodeOrEmpty ($ parentClassMethodOrFunction );
160148 $ paramTagValueNodes = $ phpDocInfo ->getParamTagValueNodes ();
161149
162150 foreach ($ paramTagValueNodes as $ paramTagValueNode ) {
@@ -166,6 +154,6 @@ private function updateDocblock(Variable $variable, string $variableName, string
166154 }
167155 }
168156
169- $ parentNode ->setDocComment (new Doc ($ phpDocInfo ->getPhpDocNode ()->__toString ()));
157+ $ parentClassMethodOrFunction ->setDocComment (new Doc ($ phpDocInfo ->getPhpDocNode ()->__toString ()));
170158 }
171159}
0 commit comments