Skip to content

Getting incorrect Exit Code with PHPCBF #1372

@JohnnyWalkerDigital

Description

@JohnnyWalkerDigital

Unexpected Exit Code from PHPCBF

Let's say I have a rule to enforce every file starting with declare(strict_types=1); like:

<rule ref="SlevomatCodingStandard.TypeHints.DeclareStrictTypes">
    <exclude-pattern>tests/*</exclude-pattern>
    <properties>
        <property name="linesCountBeforeDeclare" value="1" />
        <property name="linesCountAfterDeclare" value="1" />
        <property name="spacesCountAroundEqualsSign" value="0" />
    </properties>
</rule>

I then deliberately remove declare(strict_types=1); from one file and run ./vendor/bin/phpcbf.

This is the output I see:

.........................................F..................  60 / 125 (48%)
............................................................ 120 / 125 (96%)
.....                                                        125 / 125 (100%)



PHPCBF RESULT SUMMARY
----------------------------------------------------------------------------------------------------------------------------------
FILE                                                                                                              FIXED  REMAINING
----------------------------------------------------------------------------------------------------------------------------------
.../home/Library/Project/app/View/Components/PersonLayout.php  1      0
----------------------------------------------------------------------------------------------------------------------------------
A TOTAL OF 1 ERROR WERE FIXED IN 1 FILE
----------------------------------------------------------------------------------------------------------------------------------


Time: 4.83 secs; Memory: 32MB

My understand is that, according to the documentation, the Exit Code should be 0: "clean code base / auto-fixed with no issues remaining".

However in this instance, I'm getting the Exit Code 2: "issues found/remaining, non-auto-fixable"

This is very confusing to me because the number of remaining issues is zero.

UPDATE:
I was suppressing any warnings with -n. This seems to be related to the issue. If there are non-fixable warnings and they are suppressed with -n then the exit code reflects the existence of those unfixable warnings, rather than the suppressed output.

Code sample (updated)

#!/bin/bash

# Run PHP_CODESNIFFER fixer
./vendor/bin/phpcbf -n
result=$?

printf "Exit Code: %s\n" "$result"

Custom ruleset (updated)

    <rule ref="SlevomatCodingStandard.TypeHints.DeclareStrictTypes">
        <exclude-pattern>tests/*</exclude-pattern>
        <properties>
            <property name="linesCountBeforeDeclare" value="1" />
            <property name="linesCountAfterDeclare" value="1" />
            <property name="spacesCountAroundEqualsSign" value="0" />
        </properties>
    </rule>
    <rule ref="Generic.Files.LineLength">
        <properties>
            <property name="lineLimit" value="120"/>
            <property name="absoluteLineLimit" value="0"/>
        </properties>
    </rule>

To reproduce (updated)

Steps to reproduce the behavior:

  1. Create a file called test.php opening <?php without declare(strict_types=1); and with a line exceeding 120 chars
  2. Add custom ruleset above
  3. Create a file called check.sh that will display the Exit Code for you (use code above)
  4. Run bash check.sh
  5. See Exit Code is returned as 2

Expected behavior

Exit Code should be 0.

Versions (please complete the following information)

Operating System MacOS 26.2
PHP version 8.4
PHP_CodeSniffer version 4.0.1 (stable)
Standard PSR12
Install type Composer (global)

Please confirm

  • I have searched the issue list and am not opening a duplicate issue.
  • I have read the Contribution Guidelines and this is not a support question.
  • I confirm that this bug is a bug in PHP_CodeSniffer and not in one of the external standards.
  • I have verified the issue still exists in the 4.x branch of PHP_CodeSniffer.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions