Allowed names/prefixes for unused variables
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.
- [x] I have read the Contribution Guidelines and this is not a support question.
- [ ] I intend to create a pull request to implement this feature.
@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.
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, in your use case, which sniffs are affected by this?