1515 */
1616class AutogeneratedClassNotInConstructorSniff implements Sniff
1717{
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 ' ;
2719
2820 /**
2921 * @var array
@@ -55,9 +47,6 @@ public function process(File $phpcsFile, $stackPtr)
5547 $ this ->registerConstructorParameters ($ phpcsFile , $ stackPtr );
5648 }
5749 if ($ phpcsFile ->getTokens ()[$ stackPtr ]['type ' ] === 'T_DOUBLE_COLON ' ) {
58- if (!$ this ->isInsideConstruct ($ stackPtr )) {
59- return ;
60- }
6150 if (!$ this ->isObjectManagerGetInstance ($ phpcsFile , $ stackPtr )) {
6251 return ;
6352 }
@@ -73,9 +62,12 @@ public function process(File $phpcsFile, $stackPtr)
7362 $ variableInParameters = false ;
7463 if ($ variable = $ phpcsFile ->findNext (T_VARIABLE , $ equalsPtr , $ statementEnd )) {
7564 $ variableName = $ phpcsFile ->getTokens ()[$ variable ]['content ' ];
65+ if ($ variableName === '$this ' ) {
66+ $ variableName = $ this ->getNext ($ phpcsFile , $ variable , $ statementEnd , T_STRING )['content ' ];
67+ }
7668 foreach ($ this ->constructorParameters as $ parameter ) {
7769 $ parameterName = $ parameter ['name ' ];
78- if ($ parameterName === $ variableName ) {
70+ if ($ this -> variableName ( $ parameterName) === $ this -> variableName ( $ variableName ) ) {
7971 $ variableInParameters = true ;
8072 }
8173 }
@@ -91,7 +83,7 @@ public function process(File $phpcsFile, $stackPtr)
9183
9284 $ className = $ this ->getClassNamespace ($ className );
9385
94- $ phpcsFile ->addError (
86+ $ phpcsFile ->addWarning (
9587 sprintf ("Class %s needs to be requested in constructor, " .
9688 "otherwise compiler will not be able to find and generate these classes " , $ className ),
9789 $ stackPtr ,
@@ -121,17 +113,6 @@ private function isObjectManagerGetInstance(File $phpcsFile, int $stackPtr): boo
121113 return false ;
122114 }
123115
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-
135116 /**
136117 * Get the complete class namespace from the use's
137118 *
@@ -176,36 +157,42 @@ private function registerConstructorParameters(File $phpcsFile, int $stackPtr):
176157 $ functionName = $ phpcsFile ->getDeclarationName ($ stackPtr );
177158 if ($ functionName == '__construct ' ) {
178159 $ this ->constructorParameters = $ phpcsFile ->getMethodParameters ($ stackPtr );
179- $ this ->constructorScopeOpener = $ phpcsFile ->getTokens ()[$ stackPtr ]['scope_opener ' ];
180- $ this ->constructorScopeCloser = $ phpcsFile ->getTokens ()[$ stackPtr ]['scope_closer ' ];
181-
182160 }
183161 }
184162
185163 /**
186164 * Get next token
187165 *
188166 * @param File $phpcsFile
189- * @param int $nextDoubleColumn
190- * @param int $statementEnd
167+ * @param int $from
168+ * @param int $to
191169 * @param int|string|array $types
192170 * @return mixed
193171 */
194- private function getNext (File $ phpcsFile , int $ nextDoubleColumn , int $ statementEnd , $ types )
172+ private function getNext (File $ phpcsFile , int $ from , int $ to , $ types )
195173 {
196- return $ phpcsFile ->getTokens ()[$ phpcsFile ->findNext ($ types , $ nextDoubleColumn + 1 , $ statementEnd )];
174+ return $ phpcsFile ->getTokens ()[$ phpcsFile ->findNext ($ types , $ from + 1 , $ to )];
197175 }
198176
199177 /**
200178 * Get previous token
201179 *
202180 * @param File $phpcsFile
203- * @param int $nextDoubleColumn
181+ * @param int $from
204182 * @param int|string|array $types
205183 * @return mixed
206184 */
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 )
208195 {
209- return $ phpcsFile -> getTokens ()[ $ phpcsFile -> findPrevious ( $ types , $ nextDoubleColumn - 1 )] ;
196+ return str_replace ( ' $ ' , '' , $ parameterName ) ;
210197 }
211198}
0 commit comments