@@ -1276,16 +1276,20 @@ protected function processVariableAsGlobalDeclaration(File $phpcsFile, $stackPtr
12761276 /**
12771277 * Process a variable as a static declaration within a function.
12781278 *
1279- * This will not operate on variables that are written a class definition
1280- * like `static $foo;` or `public static ?int $foo = 'bar';` because class
1281- * properties (static or instance) are currently not tracked by this sniff.
1282- * This is because a class property might be unused inside the class, but
1283- * used outside the class (we cannot easily know if it is unused); this is
1284- * also because it's common and legal to define class properties when they
1285- * are assigned and that assignment can happen outside a class (we cannot
1286- * easily know if the use of a property is undefined). These sorts of checks
1287- * are better performed by static analysis tools that can see a whole project
1288- * rather than a linter which can only easily see a file or some lines.
1279+ * Specifically, this looks for variable definitions of the form `static
1280+ * $foo = 'hello';` or `static int $foo;` inside a function definition.
1281+ *
1282+ * This will not operate on variables that are written in a class definition
1283+ * outside of a function like `static $foo;` or `public static ?int $foo =
1284+ * 'bar';` because class properties (static or instance) are currently not
1285+ * tracked by this sniff. This is because a class property might be unused
1286+ * inside the class, but used outside the class (we cannot easily know if it
1287+ * is unused); this is also because it's common and legal to define class
1288+ * properties when they are assigned and that assignment can happen outside a
1289+ * class (we cannot easily know if the use of a property is undefined). These
1290+ * sorts of checks are better performed by static analysis tools that can see
1291+ * a whole project rather than a linter which can only easily see a file or
1292+ * some lines.
12891293 *
12901294 * If found, such a variable will be marked as declared (and possibly
12911295 * assigned, if it includes an initial value) within the scope of the
@@ -1313,7 +1317,7 @@ protected function processVariableAsStaticDeclaration(File $phpcsFile, $stackPtr
13131317 $ tokens = $ phpcsFile ->getTokens ();
13141318
13151319 // Search backwards for a `static` keyword that occurs before the start of the statement.
1316- $ startOfStatement = $ phpcsFile ->findPrevious ([T_SEMICOLON , T_OPEN_CURLY_BRACKET , T_FN_ARROW ], $ stackPtr - 1 , null , false , null , true );
1320+ $ startOfStatement = $ phpcsFile ->findPrevious ([T_SEMICOLON , T_OPEN_CURLY_BRACKET , T_FN_ARROW , T_OPEN_PARENTHESIS ], $ stackPtr - 1 , null , false , null , true );
13171321 $ staticPtr = $ phpcsFile ->findPrevious ([T_STATIC ], $ stackPtr - 1 , null , false , null , true );
13181322 if (! is_int ($ startOfStatement )) {
13191323 $ startOfStatement = 1 ;
@@ -1326,6 +1330,18 @@ protected function processVariableAsStaticDeclaration(File $phpcsFile, $stackPtr
13261330 return false ;
13271331 }
13281332
1333+ // Is the 'static' keyword an anonymous static function declaration? If so,
1334+ // this is not a static variable declaration.
1335+ $ tokenAfterStatic = $ phpcsFile ->findNext (Tokens::$ emptyTokens , $ staticPtr + 1 , null , true , null , true );
1336+ $ functionTokenTypes = [
1337+ T_FUNCTION ,
1338+ T_CLOSURE ,
1339+ T_FN ,
1340+ ];
1341+ if (is_int ($ tokenAfterStatic ) && in_array ($ tokens [$ tokenAfterStatic ]['code ' ], $ functionTokenTypes , true )) {
1342+ return false ;
1343+ }
1344+
13291345 // Is the token inside function parameters? If so, this is not a static
13301346 // declaration because we must be inside a function body.
13311347 if (Helpers::isTokenFunctionParameter ($ phpcsFile , $ stackPtr )) {
0 commit comments