1

When I call addFixableError in phpcs the output shows a fixable error as expected, however in the sniff code I check the return and this is false and I would therefore expect the output from phpcs to show that the error is not fixable.

I am correct in this assumptino or am I missing something?

It seems that phpcs_file->fixer->enabled is false

how do i correctly set this value?

sniff code -

        $phpcs_file->fixer->beginChangeset();
    $fix = $phpcs_file->addFixableError ($error, $stack_ptr, 'FunctionNameInvalid', $data);
    if ($fix === true) {
        $token = $tokens[$stack_ptr +2];
        $token['content'] = $snake_case_string;
        if ($phpcs_file->fixer->replaceToken ($stack_ptr, "asasd") === false) {
            print("could not replace\n");
        }
    } else {
        print("could not fix\n");
    }
GrahamL
  • 243
  • 2
  • 15

1 Answers1

1

The only time PHPCS will ask a sniff to fix errors is when it is printing a diff report. At all other times, PHPCS only shows errors and doesn't fix them.

The companion script, PHPCBF, is the one that does the fixing by overwriting files with a fixed copy.

If you want to auto-fix a file, use PHPCBF. If you just want to see what the fixes would look like, use PHPCS with the diff report. In both cases, the return value of addFixableError will be true. In all other cases, the return value will be false.

So those two commands to get a true return value would be:

phpcbf /path/to/file.php

or

phpcs /path/to/file.php --report=diff

Greg Sherwood
  • 6,992
  • 2
  • 29
  • 24
  • Thanks for the clarification - it really helped
    The root of my confusion came from the fact that when phpcs creates a diff it uses the ob_start command so I was not seeing any of my outpu
    – GrahamL Jun 12 '17 at 08:07