Skip to content

Commit 03ee71f

Browse files
authored
Fix error in closures with namespace separators (#197)
* Add test for fully qualified type in closure arguments * Improve error messages for processVariableAsUseImportDefinition * Allow namespace separators in getFunctionIndexForFunctionArgument
1 parent 3b19ddf commit 03ee71f

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

Tests/VariableAnalysisSniff/fixtures/FunctionWithClosureFixture.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,15 @@ function method_with_self_inside_closure() {
8282
echo static::$static_member;
8383
}
8484
}
85+
86+
function function_with_type_argument_in_closure($items, $item_id) {
87+
return array_filter($items, function (Taxed_Line_Item $line_item) use ($item_id) {
88+
return $line_item->item_id === $item_id;
89+
});
90+
}
91+
92+
function function_with_fully_qualified_type_argument_in_closure($items, $item_id) {
93+
return array_filter($items, function (\Taxed_Line_Item $line_item) use ($item_id) {
94+
return $line_item->item_id === $item_id;
95+
});
96+
}

VariableAnalysis/Lib/Helpers.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ public static function getFunctionIndexForFunctionArgument(File $phpcsFile, $sta
120120
$nonFunctionTokenTypes[] = T_COMMA;
121121
$nonFunctionTokenTypes[] = T_STRING;
122122
$nonFunctionTokenTypes[] = T_BITWISE_AND;
123+
$nonFunctionTokenTypes[] = T_NS_SEPARATOR;
123124
$functionPtr = self::getIntOrNull($phpcsFile->findPrevious($nonFunctionTokenTypes, $stackPtr - 1, null, true, null, true));
124125
if (! is_int($functionPtr)) {
125126
return null;

VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -587,11 +587,11 @@ protected function processVariableAsUseImportDefinition(File $phpcsFile, $stackP
587587

588588
$endOfArgsPtr = $phpcsFile->findPrevious([T_CLOSE_PARENTHESIS], $stackPtr - 1, null);
589589
if (! is_int($endOfArgsPtr)) {
590-
throw new \Exception("Arguments index not found for function use index {$stackPtr}");
590+
throw new \Exception("Arguments index not found for function use index {$stackPtr} when processing variable {$varName}");
591591
}
592592
$functionPtr = Helpers::getFunctionIndexForFunctionArgument($phpcsFile, $endOfArgsPtr);
593593
if (! is_int($functionPtr)) {
594-
throw new \Exception("Function index not found for function use index {$stackPtr}");
594+
throw new \Exception("Function index not found for function use index {$stackPtr} when processing variable {$varName}");
595595
}
596596

597597
// Use is both a read (in the enclosing scope) and a define (in the function scope)

0 commit comments

Comments
 (0)