1515use function preg_quote ;
1616use function sprintf ;
1717use const T_AND_EQUAL ;
18+ use const T_BITWISE_AND ;
1819use const T_CONCAT_EQUAL ;
1920use const T_DIV_EQUAL ;
2021use const T_DO ;
@@ -78,7 +79,13 @@ public function process(File $phpcsFile, $returnPointer): void
7879
7980 $ variableName = $ tokens [$ variablePointer ]['content ' ];
8081
81- if ($ this ->isStaticVariable ($ phpcsFile , $ variablePointer , $ variableName )) {
82+ $ functionPointer = $ this ->findFunctionPointer ($ phpcsFile , $ variablePointer );
83+
84+ if ($ this ->isReturnedByReference ($ phpcsFile , $ functionPointer )) {
85+ return ;
86+ }
87+
88+ if ($ this ->isStaticVariable ($ phpcsFile , $ functionPointer , $ variablePointer , $ variableName )) {
8289 return ;
8390 }
8491
@@ -118,7 +125,10 @@ public function process(File $phpcsFile, $returnPointer): void
118125 return ;
119126 }
120127
121- if (!in_array ($ tokens [$ pointerBeforePreviousVariable ]['code ' ], [T_SEMICOLON , T_OPEN_CURLY_BRACKET ], true )) {
128+ if (
129+ !in_array ($ tokens [$ pointerBeforePreviousVariable ]['code ' ], [T_SEMICOLON , T_OPEN_CURLY_BRACKET ], true )
130+ && TokenHelper::findNextEffective ($ phpcsFile , $ returnSemicolonPointer + 1 ) !== null
131+ ) {
122132 $ phpcsFile ->addError (...$ errorParameters );
123133 return ;
124134 }
@@ -223,22 +233,27 @@ private function isAssigmentToVariable(File $phpcsFile, int $pointer): bool
223233 ], true );
224234 }
225235
226- private function isStaticVariable (File $ phpcsFile , int $ variablePointer , string $ variableName ): bool
236+ private function findFunctionPointer (File $ phpcsFile , int $ pointer ): ? int
227237 {
228238 $ tokens = $ phpcsFile ->getTokens ();
229239
230- $ functionPointer = null ;
231- foreach (array_reverse ($ tokens [$ variablePointer ]['conditions ' ], true ) as $ conditionPointer => $ conditionTokenCode ) {
240+ foreach (array_reverse ($ tokens [$ pointer ]['conditions ' ], true ) as $ conditionPointer => $ conditionTokenCode ) {
232241 if (in_array ($ conditionTokenCode , TokenHelper::$ functionTokenCodes , true )) {
233- $ functionPointer = $ conditionPointer ;
234- break ;
242+ return $ conditionPointer ;
235243 }
236244 }
237245
246+ return null ;
247+ }
248+
249+ private function isStaticVariable (File $ phpcsFile , ?int $ functionPointer , int $ variablePointer , string $ variableName ): bool
250+ {
238251 if ($ functionPointer === null ) {
239252 return false ;
240253 }
241254
255+ $ tokens = $ phpcsFile ->getTokens ();
256+
242257 for ($ i = $ tokens [$ functionPointer ]['scope_opener ' ] + 1 ; $ i < $ variablePointer ; $ i ++) {
243258 if ($ tokens [$ i ]['code ' ] !== T_VARIABLE ) {
244259 continue ;
@@ -256,6 +271,18 @@ private function isStaticVariable(File $phpcsFile, int $variablePointer, string
256271 return false ;
257272 }
258273
274+ private function isReturnedByReference (File $ phpcsFile , ?int $ functionPointer ): bool
275+ {
276+ if ($ functionPointer === null ) {
277+ return false ;
278+ }
279+
280+ $ tokens = $ phpcsFile ->getTokens ();
281+
282+ $ referencePointer = TokenHelper::findNextEffective ($ phpcsFile , $ functionPointer + 1 );
283+ return $ tokens [$ referencePointer ]['code ' ] === T_BITWISE_AND ;
284+ }
285+
259286 private function hasVariableVarAnnotation (File $ phpcsFile , int $ variablePointer ): bool
260287 {
261288 $ tokens = $ phpcsFile ->getTokens ();
0 commit comments