Skip to content

phpcbf returns exit-code 1 even if all errors were fixed #3057

Closed
department-of-veterans-affairs/va.gov-cms
#16199
@caugner

Description

@caugner

Describe the bug
phpcbf returns exit-code 1 even if it fixed all errors (i.e. there are no remaining errors):

if ($this->reporter->totalFixable === 0) {
// PHPCBF fixed all fixable errors.
return 1;
}

This prevents phpcbf to be run using lint-staged as part of a husky git pre-commit hook.

Code sample
package.json:

{
  "devDependencies": {
    "husky": "^4.2.5",
    "lint-staged": "^10.2.13",
  },
  "husky": {
    "hooks": {
      "pre-commit": "lint-staged"
    }
  },
  "lint-staged": {
    "*.php": "vendor/bin/phpcbf"
  }
}

Custom ruleset
(Not relevant)

To reproduce
Steps to reproduce the behavior:

  1. yarn install (or npm install) with above package.json
  2. Make change to a PHP file so that it diverges from the coding standard and git add it.
  3. Run git commit -m "test"
  4. See that lint-staged rejects the commit because phpcbf returns a non-zero exit-code:
$ git commit -m "test"
husky > pre-commit (node v12.18.3)
[STARTED] Preparing...
[SUCCESS] Preparing...
[STARTED] Hiding unstaged changes to partially staged files...
[SUCCESS] Hiding unstaged changes to partially staged files...
[STARTED] Running tasks...
[STARTED] Running tasks for *.php
[STARTED] vendor/bin/phpcbf
[FAILED] vendor/bin/phpcbf [FAILED]
[FAILED] vendor/bin/phpcbf [FAILED]
[SUCCESS] Running tasks...
[STARTED] Applying modifications...
[SKIPPED] Skipped because of errors from tasks.
[STARTED] Restoring unstaged changes to partially staged files...
[SKIPPED] Skipped because of errors from tasks.
[STARTED] Reverting to original state because of errors...
[SUCCESS] Reverting to original state because of errors...
[STARTED] Cleaning up...
[SUCCESS] Cleaning up...

✖ vendor/bin/phpcbf:
F 1 / 1 (100%)



PHPCBF RESULT SUMMARY
----------------------------------------------------------------------
FILE                                                  FIXED  REMAINING
----------------------------------------------------------------------
C:\path\to\file.php                               5      0
----------------------------------------------------------------------
A TOTAL OF 5 ERRORS WERE FIXED IN 1 FILE
----------------------------------------------------------------------

Time: 160ms; Memory: 8MB


husky > pre-commit hook failed (add --no-verify to bypass)

Expected behavior
Like eslint --fix and prettier --write, phpcbf should return exit-code 0 if it was able to fix all issues.

Alternatively, it should be possible to trigger this behaviour with a new option --ignore-fixed-on-exit.

Versions (please complete the following information):

  • OS: Windows 10
  • PHP: 7.3.21
  • PHPCS: 3.5.6
  • Standard: PSR2

Additional context
Workaround:

bin/phpcbf

#!/usr/bin/env php
<?php

use PHP_CodeSniffer\Runner;

include_once __DIR__.'/../vendor/squizlabs/php_codesniffer/autoload.php';

$runner   = new Runner();
$exitCode = $runner->runPHPCBF();
if ($exitCode === 1) {
    $exitCode = 0;
}
exit($exitCode);

package.json

{
  "lint-staged": {
    "*.php": "bin/phpcbf"
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions