Skip to content

Commit ffb6f16

Browse files
authored
Allow finding arrow function variables when arrow function is in file scope (#347)
* Add test for arrow function in global scope * 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. * Remove duplicate call to findVariableScopeExceptArrowFunctions * Remove phpcs-import-detection depdendency since it is unused * Remove ImportDetection on phpcs config
1 parent 6a0023c commit ffb6f16

File tree

5 files changed

+77
-66
lines changed

5 files changed

+77
-66
lines changed

Tests/VariableAnalysisSniff/ArrowFunctionTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public function testArrowFunctionsWithoutUnusedBeforeUsed()
5858
102,
5959
112,
6060
150,
61+
184,
6162
];
6263
$this->assertSame($expectedWarnings, $lines);
6364
}

Tests/VariableAnalysisSniff/fixtures/ArrowFunctionFixture.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,14 @@ function arrowFunctionWithNestedArrowFunction() {
175175
];
176176
$fn();
177177
}
178+
179+
// Arrow function in global scope
180+
array_map(
181+
fn(
182+
$dir,
183+
string $bar,
184+
\My\Class|bool $foo, // Unused variable $foo
185+
\My\Class|bool $baz,
186+
) => PREFIX_DIR . $dir . $bar . $baz,
187+
PHP_ERROR_LOGS['ignore_dirs']
188+
);

VariableAnalysis/Lib/Helpers.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,8 @@ public static function findVariableScope(File $phpcsFile, $stackPtr, $varName =
429429
$varName = isset($varName) ? $varName : self::normalizeVarName($token['content']);
430430

431431
$enclosingScopeIndex = self::findVariableScopeExceptArrowFunctions($phpcsFile, $stackPtr);
432-
if ($enclosingScopeIndex) {
432+
433+
if (!is_null($enclosingScopeIndex)) {
433434
$arrowFunctionIndex = self::getContainingArrowFunctionIndex($phpcsFile, $stackPtr, $enclosingScopeIndex);
434435
$isTokenInsideArrowFunctionBody = is_int($arrowFunctionIndex);
435436
if ($isTokenInsideArrowFunctionBody) {
@@ -446,7 +447,7 @@ public static function findVariableScope(File $phpcsFile, $stackPtr, $varName =
446447
}
447448
}
448449

449-
return self::findVariableScopeExceptArrowFunctions($phpcsFile, $stackPtr);
450+
return $enclosingScopeIndex;
450451
}
451452

452453
/**

composer.json

Lines changed: 61 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,63 @@
11
{
2-
"name": "sirbrillig/phpcs-variable-analysis",
3-
"description": "A PHPCS sniff to detect problems with variables.",
4-
"type": "phpcodesniffer-standard",
5-
"keywords" : [ "phpcs", "static analysis" ],
6-
"license": "BSD-2-Clause",
7-
"authors": [
8-
{
9-
"name": "Sam Graham",
10-
"email": "[email protected]"
11-
},
12-
{
13-
"name": "Payton Swick",
14-
"email": "[email protected]"
15-
}
16-
],
17-
"support" : {
18-
"issues": "https://github.com/sirbrillig/phpcs-variable-analysis/issues",
19-
"wiki" : "https://github.com/sirbrillig/phpcs-variable-analysis/wiki",
20-
"source": "https://github.com/sirbrillig/phpcs-variable-analysis"
21-
},
22-
"config": {
23-
"sort-order": true,
24-
"allow-plugins": {
25-
"dealerdirect/phpcodesniffer-composer-installer": true
26-
},
27-
"lock": false
28-
},
29-
"autoload": {
30-
"psr-4": {
31-
"VariableAnalysis\\": "VariableAnalysis/"
32-
}
33-
},
34-
"autoload-dev": {
35-
"psr-4": {
36-
"VariableAnalysis\\Tests\\": "Tests/"
37-
}
38-
},
39-
"minimum-stability": "dev",
40-
"prefer-stable": true,
41-
"scripts": {
42-
"test": "./vendor/bin/phpunit --no-coverage",
43-
"coverage": "./vendor/bin/phpunit",
44-
"test-lte9": "./vendor/bin/phpunit -c phpunitlte9.xml.dist --no-coverage",
45-
"coverage-lte9": "./vendor/bin/phpunit -c phpunitlte9.xml.dist",
46-
"lint": "./vendor/bin/phpcs",
47-
"fix": "./vendor/bin/phpcbf",
48-
"phpstan": "./vendor/bin/phpstan analyse",
49-
"psalm": "./vendor/bin/psalm --no-cache",
50-
"static-analysis": "composer phpstan && composer psalm"
51-
},
52-
"require" : {
53-
"php" : ">=5.4.0",
54-
"squizlabs/php_codesniffer": "^3.5.6"
55-
},
56-
"require-dev": {
57-
"phpunit/phpunit": "^4.8.36 || ^5.7.21 || ^6.5 || ^7.0 || ^8.0 || ^9.0 || ^10.5.32 || ^11.3.3",
58-
"sirbrillig/phpcs-import-detection": "^1.1",
59-
"phpcsstandards/phpcsdevcs": "^1.1",
60-
"phpstan/phpstan": "^1.7",
61-
"dealerdirect/phpcodesniffer-composer-installer": "^0.7 || ^1.0",
62-
"vimeo/psalm": "^0.2 || ^0.3 || ^1.1 || ^4.24 || ^5.0"
63-
}
2+
"name": "sirbrillig/phpcs-variable-analysis",
3+
"description": "A PHPCS sniff to detect problems with variables.",
4+
"type": "phpcodesniffer-standard",
5+
"keywords": ["phpcs", "static analysis"],
6+
"license": "BSD-2-Clause",
7+
"authors": [
8+
{
9+
"name": "Sam Graham",
10+
"email": "[email protected]"
11+
},
12+
{
13+
"name": "Payton Swick",
14+
"email": "[email protected]"
15+
}
16+
],
17+
"support": {
18+
"issues": "https://github.com/sirbrillig/phpcs-variable-analysis/issues",
19+
"wiki": "https://github.com/sirbrillig/phpcs-variable-analysis/wiki",
20+
"source": "https://github.com/sirbrillig/phpcs-variable-analysis"
21+
},
22+
"config": {
23+
"sort-order": true,
24+
"allow-plugins": {
25+
"dealerdirect/phpcodesniffer-composer-installer": true
26+
},
27+
"lock": false
28+
},
29+
"autoload": {
30+
"psr-4": {
31+
"VariableAnalysis\\": "VariableAnalysis/"
32+
}
33+
},
34+
"autoload-dev": {
35+
"psr-4": {
36+
"VariableAnalysis\\Tests\\": "Tests/"
37+
}
38+
},
39+
"minimum-stability": "dev",
40+
"prefer-stable": true,
41+
"scripts": {
42+
"test": "./vendor/bin/phpunit --no-coverage",
43+
"coverage": "./vendor/bin/phpunit",
44+
"test-lte9": "./vendor/bin/phpunit -c phpunitlte9.xml.dist --no-coverage",
45+
"coverage-lte9": "./vendor/bin/phpunit -c phpunitlte9.xml.dist",
46+
"lint": "./vendor/bin/phpcs",
47+
"fix": "./vendor/bin/phpcbf",
48+
"phpstan": "./vendor/bin/phpstan analyse",
49+
"psalm": "./vendor/bin/psalm --no-cache",
50+
"static-analysis": "composer phpstan && composer psalm"
51+
},
52+
"require": {
53+
"php": ">=5.4.0",
54+
"squizlabs/php_codesniffer": "^3.5.6"
55+
},
56+
"require-dev": {
57+
"phpunit/phpunit": "^4.8.36 || ^5.7.21 || ^6.5 || ^7.0 || ^8.0 || ^9.0 || ^10.5.32 || ^11.3.3",
58+
"phpcsstandards/phpcsdevcs": "^1.1",
59+
"phpstan/phpstan": "^1.7",
60+
"dealerdirect/phpcodesniffer-composer-installer": "^0.7 || ^1.0",
61+
"vimeo/psalm": "^0.2 || ^0.3 || ^1.1 || ^4.24 || ^5.0"
62+
}
6463
}

phpcs.xml.dist

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
<!--
3232
#############################################################################
33-
USE THE PHPCSDev, ImportDetection and VariableAnalysis RULESETS
33+
USE THE PHPCSDev, VariableAnalysis RULESETS
3434
#############################################################################
3535
-->
3636

@@ -59,7 +59,6 @@
5959
<exclude name="Generic.Files.LineLength.TooLong" />
6060
</rule>
6161

62-
<rule ref="ImportDetection" />
6362
<rule ref="VariableAnalysis"/>
6463

6564

0 commit comments

Comments
 (0)