15
15
*/
16
16
class AutogeneratedClassNotInConstructorSniff implements Sniff
17
17
{
18
- const ERROR_CODE = 'AUTOGENERATED_CLASS_NOT_IN_CONSTRUCTOR ' ;
19
- /**
20
- * @var mixed
21
- */
22
- private $ constructorScopeOpener ;
23
- /**
24
- * @var mixed
25
- */
26
- private $ constructorScopeCloser ;
18
+ private const ERROR_CODE = 'AUTOGENERATED_CLASS_NOT_IN_CONSTRUCTOR ' ;
27
19
28
20
/**
29
21
* @var array
@@ -55,9 +47,6 @@ public function process(File $phpcsFile, $stackPtr)
55
47
$ this ->registerConstructorParameters ($ phpcsFile , $ stackPtr );
56
48
}
57
49
if ($ phpcsFile ->getTokens ()[$ stackPtr ]['type ' ] === 'T_DOUBLE_COLON ' ) {
58
- if (!$ this ->isInsideConstruct ($ stackPtr )) {
59
- return ;
60
- }
61
50
if (!$ this ->isObjectManagerGetInstance ($ phpcsFile , $ stackPtr )) {
62
51
return ;
63
52
}
@@ -73,9 +62,12 @@ public function process(File $phpcsFile, $stackPtr)
73
62
$ variableInParameters = false ;
74
63
if ($ variable = $ phpcsFile ->findNext (T_VARIABLE , $ equalsPtr , $ statementEnd )) {
75
64
$ variableName = $ phpcsFile ->getTokens ()[$ variable ]['content ' ];
65
+ if ($ variableName === '$this ' ) {
66
+ $ variableName = $ this ->getNext ($ phpcsFile , $ variable , $ statementEnd , T_STRING )['content ' ];
67
+ }
76
68
foreach ($ this ->constructorParameters as $ parameter ) {
77
69
$ parameterName = $ parameter ['name ' ];
78
- if ($ parameterName === $ variableName ) {
70
+ if ($ this -> variableName ( $ parameterName) === $ this -> variableName ( $ variableName ) ) {
79
71
$ variableInParameters = true ;
80
72
}
81
73
}
@@ -91,7 +83,7 @@ public function process(File $phpcsFile, $stackPtr)
91
83
92
84
$ className = $ this ->getClassNamespace ($ className );
93
85
94
- $ phpcsFile ->addError (
86
+ $ phpcsFile ->addWarning (
95
87
sprintf ("Class %s needs to be requested in constructor, " .
96
88
"otherwise compiler will not be able to find and generate these classes " , $ className ),
97
89
$ stackPtr ,
@@ -121,17 +113,6 @@ private function isObjectManagerGetInstance(File $phpcsFile, int $stackPtr): boo
121
113
return false ;
122
114
}
123
115
124
- /**
125
- * Checks if the code is inside __construct
126
- *
127
- * @param int $stackPtr
128
- * @return bool
129
- */
130
- private function isInsideConstruct (int $ stackPtr ): bool
131
- {
132
- return $ stackPtr > $ this ->constructorScopeOpener && $ stackPtr < $ this ->constructorScopeCloser ;
133
- }
134
-
135
116
/**
136
117
* Get the complete class namespace from the use's
137
118
*
@@ -176,36 +157,42 @@ private function registerConstructorParameters(File $phpcsFile, int $stackPtr):
176
157
$ functionName = $ phpcsFile ->getDeclarationName ($ stackPtr );
177
158
if ($ functionName == '__construct ' ) {
178
159
$ this ->constructorParameters = $ phpcsFile ->getMethodParameters ($ stackPtr );
179
- $ this ->constructorScopeOpener = $ phpcsFile ->getTokens ()[$ stackPtr ]['scope_opener ' ];
180
- $ this ->constructorScopeCloser = $ phpcsFile ->getTokens ()[$ stackPtr ]['scope_closer ' ];
181
-
182
160
}
183
161
}
184
162
185
163
/**
186
164
* Get next token
187
165
*
188
166
* @param File $phpcsFile
189
- * @param int $nextDoubleColumn
190
- * @param int $statementEnd
167
+ * @param int $from
168
+ * @param int $to
191
169
* @param int|string|array $types
192
170
* @return mixed
193
171
*/
194
- private function getNext (File $ phpcsFile , int $ nextDoubleColumn , int $ statementEnd , $ types )
172
+ private function getNext (File $ phpcsFile , int $ from , int $ to , $ types )
195
173
{
196
- return $ phpcsFile ->getTokens ()[$ phpcsFile ->findNext ($ types , $ nextDoubleColumn + 1 , $ statementEnd )];
174
+ return $ phpcsFile ->getTokens ()[$ phpcsFile ->findNext ($ types , $ from + 1 , $ to )];
197
175
}
198
176
199
177
/**
200
178
* Get previous token
201
179
*
202
180
* @param File $phpcsFile
203
- * @param int $nextDoubleColumn
181
+ * @param int $from
204
182
* @param int|string|array $types
205
183
* @return mixed
206
184
*/
207
- private function getPrevious (File $ phpcsFile , int $ nextDoubleColumn , $ types )
185
+ private function getPrevious (File $ phpcsFile , int $ from , $ types )
186
+ {
187
+ return $ phpcsFile ->getTokens ()[$ phpcsFile ->findPrevious ($ types , $ from - 1 )];
188
+ }
189
+
190
+ /**
191
+ * @param $parameterName
192
+ * @return array|string|string[]
193
+ */
194
+ protected function variableName ($ parameterName )
208
195
{
209
- return $ phpcsFile -> getTokens ()[ $ phpcsFile -> findPrevious ( $ types , $ nextDoubleColumn - 1 )] ;
196
+ return str_replace ( ' $ ' , '' , $ parameterName ) ;
210
197
}
211
198
}
0 commit comments