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:
- Create a file called
test.php opening <?php without declare(strict_types=1); and with a line exceeding 120 chars
- Add custom ruleset above
- Create a file called
check.sh that will display the Exit Code for you (use code above)
- Run
bash check.sh
- 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
Unexpected Exit Code from PHPCBF
Let's say I have a rule to enforce every file starting with
declare(strict_types=1);like:I then deliberately remove
declare(strict_types=1);from one file and run./vendor/bin/phpcbf.This is the output I see:
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-nthen the exit code reflects the existence of those unfixable warnings, rather than the suppressed output.Code sample (updated)
Custom ruleset (updated)
To reproduce (updated)
Steps to reproduce the behavior:
test.phpopening <?php withoutdeclare(strict_types=1);and with a line exceeding 120 charscheck.shthat will display the Exit Code for you (use code above)bash check.sh2Expected behavior
Exit Code should be
0.Versions (please complete the following information)
Please confirm
4.xbranch of PHP_CodeSniffer.