GrahamL
GrahamL

Reputation: 263

adding fixable errors using phpcs

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");
    }

Upvotes: 1

Views: 682

Answers (1)

Greg Sherwood
Greg Sherwood

Reputation: 7222

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

Upvotes: 1

Related Questions