Skip to content

Commit 4d2d3d4

Browse files
committed
Generic/ForbiddenFunctions: bug fix for class names in attributes
Attributes cannot contain function calls and in most cases, `T_STRING`s in an attribute will be a class name, not a function name. As things were, `T_STRING`'s in attributes which _looked_ like function calls (but in actual fact are class instantiations) would lead to false positives. Fixed now by ignoring all code within attributes. Includes unit test.
1 parent 693476f commit 4d2d3d4

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

src/Standards/Generic/Sniffs/PHP/ForbiddenFunctionsSniff.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ public function process(File $phpcsFile, $stackPtr)
163163
return;
164164
}
165165

166+
if (empty($tokens[$stackPtr]['nested_attributes']) === false) {
167+
// Class instantiation in attribute, not function call.
168+
return;
169+
}
170+
166171
$function = strtolower($tokens[$stackPtr]['content']);
167172
$pattern = null;
168173

src/Standards/Generic/Tests/PHP/ForbiddenFunctionsUnitTest.inc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,6 @@ function mymodule_form_callback(SizeOf $sizeof) {
5555
}
5656

5757
$size = $class?->sizeof($array);
58+
59+
#[SizeOf(10)]
60+
function doSomething() {}

src/Standards/Squiz/Tests/PHP/DiscouragedFunctionsUnitTest.inc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22
error_log('test');
33
print_r($array);
44
var_dump($array);
5-
?>
5+
6+
#[Var_Dump(10)]
7+
function debugMe() {}

0 commit comments

Comments
 (0)