From d45a39470bb6c87ecbb974801f587c8693308b85 Mon Sep 17 00:00:00 2001 From: Payton Swick Date: Mon, 6 Jan 2025 10:24:18 -0500 Subject: [PATCH 1/5] Add test for arrow function in global scope --- Tests/VariableAnalysisSniff/ArrowFunctionTest.php | 1 + .../fixtures/ArrowFunctionFixture.php | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/Tests/VariableAnalysisSniff/ArrowFunctionTest.php b/Tests/VariableAnalysisSniff/ArrowFunctionTest.php index 94db882..531f98a 100644 --- a/Tests/VariableAnalysisSniff/ArrowFunctionTest.php +++ b/Tests/VariableAnalysisSniff/ArrowFunctionTest.php @@ -58,6 +58,7 @@ public function testArrowFunctionsWithoutUnusedBeforeUsed() 102, 112, 150, + 184, ]; $this->assertSame($expectedWarnings, $lines); } diff --git a/Tests/VariableAnalysisSniff/fixtures/ArrowFunctionFixture.php b/Tests/VariableAnalysisSniff/fixtures/ArrowFunctionFixture.php index f3f48e8..9cfea71 100644 --- a/Tests/VariableAnalysisSniff/fixtures/ArrowFunctionFixture.php +++ b/Tests/VariableAnalysisSniff/fixtures/ArrowFunctionFixture.php @@ -175,3 +175,14 @@ function arrowFunctionWithNestedArrowFunction() { ]; $fn(); } + +// Arrow function in global scope +array_map( + fn( + $dir, + string $bar, + \My\Class|bool $foo, // Unused variable $foo + \My\Class|bool $baz, + ) => PREFIX_DIR . $dir . $bar . $baz, + PHP_ERROR_LOGS['ignore_dirs'] +); From fe417086559b621977b409f1d3097c25837e5830 Mon Sep 17 00:00:00 2001 From: Payton Swick Date: Mon, 6 Jan 2025 10:33:03 -0500 Subject: [PATCH 2/5] Still look for arrow func when var scope is 0 `findVariableScopeExceptArrowFunctions()` can return null if it finds no scope, but it can also return 0 which is the file level scope. The additional code to look for arrow function scope needs to operate even on 0. --- VariableAnalysis/Lib/Helpers.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/VariableAnalysis/Lib/Helpers.php b/VariableAnalysis/Lib/Helpers.php index 9b779aa..8690dac 100644 --- a/VariableAnalysis/Lib/Helpers.php +++ b/VariableAnalysis/Lib/Helpers.php @@ -429,7 +429,8 @@ public static function findVariableScope(File $phpcsFile, $stackPtr, $varName = $varName = isset($varName) ? $varName : self::normalizeVarName($token['content']); $enclosingScopeIndex = self::findVariableScopeExceptArrowFunctions($phpcsFile, $stackPtr); - if ($enclosingScopeIndex) { + + if (!is_null($enclosingScopeIndex)) { $arrowFunctionIndex = self::getContainingArrowFunctionIndex($phpcsFile, $stackPtr, $enclosingScopeIndex); $isTokenInsideArrowFunctionBody = is_int($arrowFunctionIndex); if ($isTokenInsideArrowFunctionBody) { From 490606d997e0fc74814f4968c68d82c2eb47909e Mon Sep 17 00:00:00 2001 From: Payton Swick Date: Mon, 6 Jan 2025 10:34:48 -0500 Subject: [PATCH 3/5] Remove duplicate call to findVariableScopeExceptArrowFunctions --- VariableAnalysis/Lib/Helpers.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VariableAnalysis/Lib/Helpers.php b/VariableAnalysis/Lib/Helpers.php index 8690dac..ae06129 100644 --- a/VariableAnalysis/Lib/Helpers.php +++ b/VariableAnalysis/Lib/Helpers.php @@ -447,7 +447,7 @@ public static function findVariableScope(File $phpcsFile, $stackPtr, $varName = } } - return self::findVariableScopeExceptArrowFunctions($phpcsFile, $stackPtr); + return $enclosingScopeIndex; } /** From f107315d33029e851bf2be6af75f4a65734ab54f Mon Sep 17 00:00:00 2001 From: Payton Swick Date: Mon, 6 Jan 2025 10:53:23 -0500 Subject: [PATCH 4/5] Remove phpcs-import-detection depdendency since it is unused --- composer.json | 123 +++++++++++++++++++++++++------------------------- 1 file changed, 61 insertions(+), 62 deletions(-) diff --git a/composer.json b/composer.json index 544bc3c..b11fae4 100644 --- a/composer.json +++ b/composer.json @@ -1,64 +1,63 @@ { - "name": "sirbrillig/phpcs-variable-analysis", - "description": "A PHPCS sniff to detect problems with variables.", - "type": "phpcodesniffer-standard", - "keywords" : [ "phpcs", "static analysis" ], - "license": "BSD-2-Clause", - "authors": [ - { - "name": "Sam Graham", - "email": "php-codesniffer-variableanalysis@illusori.co.uk" - }, - { - "name": "Payton Swick", - "email": "payton@foolord.com" - } - ], - "support" : { - "issues": "https://github.com/sirbrillig/phpcs-variable-analysis/issues", - "wiki" : "https://github.com/sirbrillig/phpcs-variable-analysis/wiki", - "source": "https://github.com/sirbrillig/phpcs-variable-analysis" - }, - "config": { - "sort-order": true, - "allow-plugins": { - "dealerdirect/phpcodesniffer-composer-installer": true - }, - "lock": false - }, - "autoload": { - "psr-4": { - "VariableAnalysis\\": "VariableAnalysis/" - } - }, - "autoload-dev": { - "psr-4": { - "VariableAnalysis\\Tests\\": "Tests/" - } - }, - "minimum-stability": "dev", - "prefer-stable": true, - "scripts": { - "test": "./vendor/bin/phpunit --no-coverage", - "coverage": "./vendor/bin/phpunit", - "test-lte9": "./vendor/bin/phpunit -c phpunitlte9.xml.dist --no-coverage", - "coverage-lte9": "./vendor/bin/phpunit -c phpunitlte9.xml.dist", - "lint": "./vendor/bin/phpcs", - "fix": "./vendor/bin/phpcbf", - "phpstan": "./vendor/bin/phpstan analyse", - "psalm": "./vendor/bin/psalm --no-cache", - "static-analysis": "composer phpstan && composer psalm" - }, - "require" : { - "php" : ">=5.4.0", - "squizlabs/php_codesniffer": "^3.5.6" - }, - "require-dev": { - "phpunit/phpunit": "^4.8.36 || ^5.7.21 || ^6.5 || ^7.0 || ^8.0 || ^9.0 || ^10.5.32 || ^11.3.3", - "sirbrillig/phpcs-import-detection": "^1.1", - "phpcsstandards/phpcsdevcs": "^1.1", - "phpstan/phpstan": "^1.7", - "dealerdirect/phpcodesniffer-composer-installer": "^0.7 || ^1.0", - "vimeo/psalm": "^0.2 || ^0.3 || ^1.1 || ^4.24 || ^5.0" - } + "name": "sirbrillig/phpcs-variable-analysis", + "description": "A PHPCS sniff to detect problems with variables.", + "type": "phpcodesniffer-standard", + "keywords": ["phpcs", "static analysis"], + "license": "BSD-2-Clause", + "authors": [ + { + "name": "Sam Graham", + "email": "php-codesniffer-variableanalysis@illusori.co.uk" + }, + { + "name": "Payton Swick", + "email": "payton@foolord.com" + } + ], + "support": { + "issues": "https://github.com/sirbrillig/phpcs-variable-analysis/issues", + "wiki": "https://github.com/sirbrillig/phpcs-variable-analysis/wiki", + "source": "https://github.com/sirbrillig/phpcs-variable-analysis" + }, + "config": { + "sort-order": true, + "allow-plugins": { + "dealerdirect/phpcodesniffer-composer-installer": true + }, + "lock": false + }, + "autoload": { + "psr-4": { + "VariableAnalysis\\": "VariableAnalysis/" + } + }, + "autoload-dev": { + "psr-4": { + "VariableAnalysis\\Tests\\": "Tests/" + } + }, + "minimum-stability": "dev", + "prefer-stable": true, + "scripts": { + "test": "./vendor/bin/phpunit --no-coverage", + "coverage": "./vendor/bin/phpunit", + "test-lte9": "./vendor/bin/phpunit -c phpunitlte9.xml.dist --no-coverage", + "coverage-lte9": "./vendor/bin/phpunit -c phpunitlte9.xml.dist", + "lint": "./vendor/bin/phpcs", + "fix": "./vendor/bin/phpcbf", + "phpstan": "./vendor/bin/phpstan analyse", + "psalm": "./vendor/bin/psalm --no-cache", + "static-analysis": "composer phpstan && composer psalm" + }, + "require": { + "php": ">=5.4.0", + "squizlabs/php_codesniffer": "^3.5.6" + }, + "require-dev": { + "phpunit/phpunit": "^4.8.36 || ^5.7.21 || ^6.5 || ^7.0 || ^8.0 || ^9.0 || ^10.5.32 || ^11.3.3", + "phpcsstandards/phpcsdevcs": "^1.1", + "phpstan/phpstan": "^1.7", + "dealerdirect/phpcodesniffer-composer-installer": "^0.7 || ^1.0", + "vimeo/psalm": "^0.2 || ^0.3 || ^1.1 || ^4.24 || ^5.0" + } } From f4748f1e56be4540c635c6c90cfa1944301218c3 Mon Sep 17 00:00:00 2001 From: Payton Swick Date: Mon, 6 Jan 2025 10:55:09 -0500 Subject: [PATCH 5/5] Remove ImportDetection on phpcs config --- phpcs.xml.dist | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 808ae0e..ff63b21 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -30,7 +30,7 @@ @@ -59,7 +59,6 @@ -