Skip to content

Squiz/LowercasePHPFunctions + Generic/ForbiddenFunctions: bug fix for class names in attributes #3779

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Standards/Generic/Sniffs/PHP/ForbiddenFunctionsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,6 @@ function mymodule_form_callback(SizeOf $sizeof) {
}

$size = $class?->sizeof($array);

#[SizeOf(10)]
function doSomething() {}
5 changes: 5 additions & 0 deletions src/Standards/Squiz/Sniffs/PHP/LowercasePHPFunctionsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
error_log('test');
print_r($array);
var_dump($array);
?>

#[Var_Dump(10)]
function debugMe() {}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{}
}