@@ -41,7 +41,7 @@ public function register()
41
41
public function process (File $ phpcsFile , $ stackPtr )
42
42
{
43
43
if ($ phpcsFile ->getTokens ()[$ stackPtr ]['type ' ] === 'T_USE ' ) {
44
- $ this ->registerUses ($ phpcsFile , $ stackPtr );
44
+ $ this ->registerUse ($ phpcsFile , $ stackPtr );
45
45
}
46
46
if ($ phpcsFile ->getTokens ()[$ stackPtr ]['type ' ] === 'T_FUNCTION ' ) {
47
47
$ this ->registerConstructorParameters ($ phpcsFile , $ stackPtr );
@@ -59,29 +59,9 @@ public function process(File $phpcsFile, $stackPtr)
59
59
return ;
60
60
}
61
61
62
- $ variableInParameters = false ;
63
- if ($ variable = $ phpcsFile ->findNext (T_VARIABLE , $ equalsPtr , $ statementEnd )) {
64
- $ variableName = $ phpcsFile ->getTokens ()[$ variable ]['content ' ];
65
- if ($ variableName === '$this ' ) {
66
- $ variableName = $ this ->getNext ($ phpcsFile , $ variable , $ statementEnd , T_STRING )['content ' ];
67
- }
68
- foreach ($ this ->constructorParameters as $ parameter ) {
69
- $ parameterName = $ parameter ['name ' ];
70
- if ($ this ->variableName ($ parameterName ) === $ this ->variableName ($ variableName )) {
71
- $ variableInParameters = true ;
72
- }
73
- }
74
- }
75
-
76
- if (!$ variableInParameters ) {
62
+ if (!$ this ->isVariableInConstructorParameters ($ phpcsFile , $ equalsPtr , $ statementEnd )) {
77
63
$ next = $ stackPtr ;
78
- while ($ next = $ phpcsFile ->findNext (T_DOUBLE_COLON , $ next + 1 , $ statementEnd )) {
79
- if ($ this ->getNext ($ phpcsFile , $ next , $ statementEnd , T_STRING )['content ' ] === 'class ' ) {
80
- $ className = $ this ->getPrevious ($ phpcsFile , $ next , T_STRING )['content ' ];
81
- }
82
- }
83
-
84
- $ className = $ this ->getClassNamespace ($ className );
64
+ $ className = $ this ->obtainClassToGetOrCreate ($ phpcsFile , $ next , $ statementEnd );
85
65
86
66
$ phpcsFile ->addWarning (
87
67
sprintf ("Class %s needs to be requested in constructor, " .
@@ -118,9 +98,9 @@ private function isObjectManagerGetInstance(File $phpcsFile, int $stackPtr): boo
118
98
*/
119
99
private function getClassNamespace (string $ className ): string
120
100
{
121
- foreach ($ this ->uses as $ use ) {
122
- if (end ( $ use ) === $ className ) {
123
- $ className = implode ( ' / ' , $ use) ;
101
+ foreach ($ this ->uses as $ key => $ use ) {
102
+ if ($ key === $ className ) {
103
+ return $ use ;
124
104
}
125
105
}
126
106
return $ className ;
@@ -132,15 +112,21 @@ private function getClassNamespace(string $className): string
132
112
* @param File $phpcsFile
133
113
* @param int $stackPtr
134
114
*/
135
- private function registerUses (File $ phpcsFile , int $ stackPtr ): void
115
+ private function registerUse (File $ phpcsFile , int $ stackPtr ): void
136
116
{
137
117
$ useEnd = $ phpcsFile ->findEndOfStatement ($ stackPtr );
138
118
$ use = [];
139
119
$ usePosition = $ stackPtr ;
140
120
while ($ usePosition = $ phpcsFile ->findNext (T_STRING , $ usePosition + 1 , $ useEnd )) {
141
121
$ use [] = $ phpcsFile ->getTokens ()[$ usePosition ]['content ' ];
142
122
}
143
- $ this ->uses [] = $ use ;
123
+
124
+ $ key = end ($ use );
125
+ if ($ phpcsFile ->findNext (T_AS , $ stackPtr , $ useEnd )) {
126
+ $ this ->uses [$ key ] = implode ("\\" , array_slice ($ use , 0 , count ($ use ) - 1 ));
127
+ } else {
128
+ $ this ->uses [$ key ] = implode ("\\" , $ use );
129
+ }
144
130
}
145
131
146
132
/**
@@ -194,4 +180,44 @@ protected function variableName(string $parameterName): string
194
180
{
195
181
return str_replace ('$ ' , '' , $ parameterName );
196
182
}
183
+
184
+ /**
185
+ * @param File $phpcsFile
186
+ * @param int $equalsPtr
187
+ * @param int $statementEnd
188
+ * @return bool
189
+ */
190
+ private function isVariableInConstructorParameters (File $ phpcsFile , int $ equalsPtr , int $ statementEnd ): bool
191
+ {
192
+ if ($ variable = $ phpcsFile ->findNext (T_VARIABLE , $ equalsPtr , $ statementEnd )) {
193
+ $ variableName = $ phpcsFile ->getTokens ()[$ variable ]['content ' ];
194
+ if ($ variableName === '$this ' ) {
195
+ $ variableName = $ this ->getNext ($ phpcsFile , $ variable , $ statementEnd , T_STRING )['content ' ];
196
+ }
197
+ foreach ($ this ->constructorParameters as $ parameter ) {
198
+ $ parameterName = $ parameter ['name ' ];
199
+ if ($ this ->variableName ($ parameterName ) === $ this ->variableName ($ variableName )) {
200
+ return true ;
201
+ }
202
+ }
203
+ }
204
+ return false ;
205
+ }
206
+
207
+ /**
208
+ * @param File $phpcsFile
209
+ * @param $next
210
+ * @param int $statementEnd
211
+ * @return string
212
+ */
213
+ private function obtainClassToGetOrCreate (File $ phpcsFile , $ next , int $ statementEnd ): string
214
+ {
215
+ while ($ next = $ phpcsFile ->findNext (T_DOUBLE_COLON , $ next + 1 , $ statementEnd )) {
216
+ if ($ this ->getNext ($ phpcsFile , $ next , $ statementEnd , T_STRING )['content ' ] === 'class ' ) {
217
+ $ className = $ this ->getPrevious ($ phpcsFile , $ next , T_STRING )['content ' ];
218
+ }
219
+ }
220
+
221
+ return $ this ->getClassNamespace ($ className );
222
+ }
197
223
}
0 commit comments