Skip to content

Commit 657dafa

Browse files
jrfnlgsherwood
authored andcommitted
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 794a238 commit 657dafa

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
@@ -162,6 +162,11 @@ public function process(File $phpcsFile, $stackPtr)
162162
return;
163163
}
164164

165+
if (empty($tokens[$stackPtr]['nested_attributes']) === false) {
166+
// Class instantiation in attribute, not function call.
167+
return;
168+
}
169+
165170
$function = strtolower($tokens[$stackPtr]['content']);
166171
if ($tokens[$stackPtr]['code'] === T_NAME_FULLY_QUALIFIED) {
167172
$function = ltrim($function, '\\');

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)