PHP_CodeSniffer icon indicating copy to clipboard operation
PHP_CodeSniffer copied to clipboard

Allowed names/prefixes for unused variables

Open kekmitkep opened this issue 8 months ago • 3 comments

Is your feature request related to a problem?

When using callbacks for example for filter calls on arrays and you only use the $value, the $key parameter might be unused.

There are also multiple other use cases (like workarounds for interface implementation or deep code analysis #490 ) where this could be an issue.

This is prevalent in UnusedFunctionParameterSniff.

Describe the solution you'd like

There could be reserved names or prefixes that are ignored by the unused variable/parameter check.

Should be flagged:

        $selectedCompanies = $companyOptions->values()->filter(
            fn($item, $index): bool => in_array($index + 1, $selectedIndices, true)
        );

Should not be flagged:

        $selectedCompanies = $companyOptions->values()->filter(
            fn($_, $index): bool => in_array($index + 1, $selectedIndices, true)
        );

Additional context (optional)

A similar thing has already been added for PHPMD: https://github.com/phpmd/phpmd/issues/278 https://github.com/phpmd/phpmd/pull/319

For consistency's sake I would suggest a similar or the same approach.

kekmitkep avatar May 07 '25 12:05 kekmitkep

@kekmitkep Please include information about which sniff(s) this request is for.

There are also multiple other usescases where this could be an issue.

Again, please include information about which sniffs would be involved in those usecases.

jrfnl avatar May 07 '25 16:05 jrfnl

Need the same thing. I created a PR for it.

Started out with only $_. It would be possible to add the other ignore patterns as well. I am just not sure how to formulate those variations most efficiently in the tests.

func0der avatar May 12 '25 11:05 func0der

@func0der, in your use case, which sniffs are affected by this?

rodrigoprimo avatar May 19 '25 12:05 rodrigoprimo