diff --git a/src/Standards/Generic/Sniffs/PHP/ForbiddenFunctionsSniff.php b/src/Standards/Generic/Sniffs/PHP/ForbiddenFunctionsSniff.php index 1a10b15320..8717189f0e 100644 --- a/src/Standards/Generic/Sniffs/PHP/ForbiddenFunctionsSniff.php +++ b/src/Standards/Generic/Sniffs/PHP/ForbiddenFunctionsSniff.php @@ -163,6 +163,11 @@ public function process(File $phpcsFile, $stackPtr) return; } + if (empty($tokens[$stackPtr]['nested_attributes']) === false) { + // Class instantiation in attribute, not function call. + return; + } + $function = strtolower($tokens[$stackPtr]['content']); $pattern = null; diff --git a/src/Standards/Generic/Tests/PHP/ForbiddenFunctionsUnitTest.inc b/src/Standards/Generic/Tests/PHP/ForbiddenFunctionsUnitTest.inc index b30f0dde94..060da6128c 100644 --- a/src/Standards/Generic/Tests/PHP/ForbiddenFunctionsUnitTest.inc +++ b/src/Standards/Generic/Tests/PHP/ForbiddenFunctionsUnitTest.inc @@ -55,3 +55,6 @@ function mymodule_form_callback(SizeOf $sizeof) { } $size = $class?->sizeof($array); + +#[SizeOf(10)] +function doSomething() {} diff --git a/src/Standards/Squiz/Sniffs/PHP/LowercasePHPFunctionsSniff.php b/src/Standards/Squiz/Sniffs/PHP/LowercasePHPFunctionsSniff.php index 519b7f6ccc..37f1e40e7b 100644 --- a/src/Standards/Squiz/Sniffs/PHP/LowercasePHPFunctionsSniff.php +++ b/src/Standards/Squiz/Sniffs/PHP/LowercasePHPFunctionsSniff.php @@ -75,6 +75,11 @@ public function process(File $phpcsFile, $stackPtr) } // Make sure this is a function call or a use statement. + if (empty($tokens[$stackPtr]['nested_attributes']) === false) { + // Class instantiation in attribute, not function call. + return; + } + $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); if ($next === false) { // Not a function call. diff --git a/src/Standards/Squiz/Tests/PHP/DiscouragedFunctionsUnitTest.inc b/src/Standards/Squiz/Tests/PHP/DiscouragedFunctionsUnitTest.inc index ca457a2e9a..3c875d0983 100644 --- a/src/Standards/Squiz/Tests/PHP/DiscouragedFunctionsUnitTest.inc +++ b/src/Standards/Squiz/Tests/PHP/DiscouragedFunctionsUnitTest.inc @@ -2,4 +2,6 @@ error_log('test'); print_r($array); var_dump($array); -?> + +#[Var_Dump(10)] +function debugMe() {} diff --git a/src/Standards/Squiz/Tests/PHP/LowercasePHPFunctionsUnitTest.inc b/src/Standards/Squiz/Tests/PHP/LowercasePHPFunctionsUnitTest.inc index c67381ad8b..702b13de0a 100644 --- a/src/Standards/Squiz/Tests/PHP/LowercasePHPFunctionsUnitTest.inc +++ b/src/Standards/Squiz/Tests/PHP/LowercasePHPFunctionsUnitTest.inc @@ -41,3 +41,10 @@ $callToNamespacedFunction = namespace\STR_REPEAT($a, 2); // Could potentially be $filePath = new \File($path); $count = $object?->Count(); + +class AttributesShouldBeIgnored +{ + #[Putenv('FOO', 'foo')] + public function foo(): void + {} +} diff --git a/src/Standards/Squiz/Tests/PHP/LowercasePHPFunctionsUnitTest.inc.fixed b/src/Standards/Squiz/Tests/PHP/LowercasePHPFunctionsUnitTest.inc.fixed index 40507c0404..281425c595 100644 --- a/src/Standards/Squiz/Tests/PHP/LowercasePHPFunctionsUnitTest.inc.fixed +++ b/src/Standards/Squiz/Tests/PHP/LowercasePHPFunctionsUnitTest.inc.fixed @@ -41,3 +41,10 @@ $callToNamespacedFunction = namespace\STR_REPEAT($a, 2); // Could potentially be $filePath = new \File($path); $count = $object?->Count(); + +class AttributesShouldBeIgnored +{ + #[Putenv('FOO', 'foo')] + public function foo(): void + {} +}