Search code examples
phpsuppress-warningsphpcs

PHP Easy Coding Standard (ECS) - suppress phpcs warnings


ECS = symplify/easy-coding-standard
I am getting an message when running ECS:

Unused variable $validator.
(SlevomatCodingStandard\Sniffs\Variables\UnusedVariableSniff.UnusedVariable)

For this code:

// phpcs:ignore
// @codingStandardsIgnoreLine
foreach ($this->rules as $attribute => $validator) {

I did try using the suppress comments in different setup:

  • only one of them
  • on the same line as the message suggests
  • in different order
  • by targeting specific warning:
    • SlevomatCodingStandard\Sniffs\Variables\UnusedVariableSniff.UnusedVariable
    • SlevomatCodingStandard.Variables.UnusedVariable

Does any one know if ECS supports inline suppress comments?
In the ECS documentation I see it's only possible to suppress whole file.

PS. I know I can use array_keys(), but the goal in this case is to suppress the warnings, as in the future it can be a different one.


Solution

  • Or... as variant add rule to config file easy-coding-standard.yaml

    services:
      SlevomatCodingStandard\Sniffs\Variables\UnusedVariableSniff:
        ignoreUnusedValuesWhenOnlyKeysAreUsedInForeach: true