Skip to content

AbstractFunctionRestrictionsSniff::is_targetted_token() incorrectly treats first class callable as a function call #2478

@rodrigoprimo

Description

@rodrigoprimo

Bug Description

The method AbstractFunctionRestrictionsSniff::is_targetted_token() is used to determine if a given token is a function call or not. It does not handle first class callables properly and identifies them as function calls, which is incorrect.

I found this while using AbstractFunctionParameterSniff::process_no_parameters() to create the sniff for #2473. AbstractFunctionParameterSniff is affected by this bug and sees first class callables as a function call without parameters.

Loosely related to this PHPCSUtils PR: PHPCSStandards/PHPCSUtils#362

Minimal Code Snippet

The issue happens when running this command:

phpcs --standard=WordPress --sniffs=WordPress.Utils.I18nTextDomainFixer test.php

Over a file containing this code:

<?php
// phpcs:set WordPress.Utils.I18nTextDomainFixer old_text_domain[] text-domain
// phpcs:set WordPress.Utils.I18nTextDomainFixer new_text_domain something-else

$callable = __(...);

Error Code

FILE: test.php
---------------------------------------------------------------------------------------------------------------
FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
---------------------------------------------------------------------------------------------------------------
 5 | WARNING | Missing $domain arg and preceding argument(s) (WordPress.Utils.I18nTextDomainFixer.MissingArgs)
---------------------------------------------------------------------------------------------------------------

Environment

Question Answer
PHP version 8.3
PHP_CodeSniffer version 3.10.1
WordPressCS version develop branch
PHPCSUtils version 1.0.12
PHPCSExtra version 1.2.1
WordPressCS install type git clone

Additional Context (optional)

I can try to create a PR to fix this problem, but it seems there are no tests for the AbstractFunctionRestrictionsSniff::is_targetted_token() and for the rest of the AbstractFunctionRestrictionsSniff. I believe it would be important to start by creating tests first.

Tested Against develop Branch?

  • I have verified the issue still exists in the develop branch of WordPressCS.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions