Skip to content

Commit 693476f

Browse files
committed
Squiz/LowercasePHPFunctions: 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. Fixes 3778
1 parent ed8e00d commit 693476f

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

src/Standards/Squiz/Sniffs/PHP/LowercasePHPFunctionsSniff.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ public function process(File $phpcsFile, $stackPtr)
7575
}
7676

7777
// Make sure this is a function call or a use statement.
78+
if (empty($tokens[$stackPtr]['nested_attributes']) === false) {
79+
// Class instantiation in attribute, not function call.
80+
return;
81+
}
82+
7883
$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
7984
if ($next === false) {
8085
// Not a function call.

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,10 @@ $callToNamespacedFunction = namespace\STR_REPEAT($a, 2); // Could potentially be
4141
$filePath = new \File($path);
4242

4343
$count = $object?->Count();
44+
45+
class AttributesShouldBeIgnored
46+
{
47+
#[Putenv('FOO', 'foo')]
48+
public function foo(): void
49+
{}
50+
}

src/Standards/Squiz/Tests/PHP/LowercasePHPFunctionsUnitTest.inc.fixed

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,10 @@ $callToNamespacedFunction = namespace\STR_REPEAT($a, 2); // Could potentially be
4141
$filePath = new \File($path);
4242

4343
$count = $object?->Count();
44+
45+
class AttributesShouldBeIgnored
46+
{
47+
#[Putenv('FOO', 'foo')]
48+
public function foo(): void
49+
{}
50+
}

0 commit comments

Comments
 (0)