Is there a way to disable reporting for phpcbf?
Something like: phpcbf -q --report=none .?
No, but which output do you want to suppress? The output showing you which files are being fixed and or the final report showing you what was done (or both)?
You could always redirect STDOUT to /dev/null (or NUL for Windows, I think it is) if you want no output at all.
Sorry, should have mentioned -q works, but only suppresses that progress output. The final report still shows, so you are probably wanting to suppress that, which there is no way to do.
My use case: generating files with zend-code (via a composer script), which doesn't completely comply with PSR-2. Running phpcbf automatically after generation, I have no need for any report to show i.e. silent fixing, since it's just removing redundant blank lines that break PSR-2 compatibility.
> /dev/null seems to work in terms of hiding the output from phpcbf, though Composer is giving me a notice that the script returned with error code 1.
"generate-types": "vendor/bin/soap-client generate:types --config ./config/generate.php && phpcbf -q src/SoapTypes > /dev/null && echo \"Types have been generated to src/SoapTypes. (Ignore the error code message.)\"",

Even with the report still showing, the error code message still persists. Is there a way to override that?
The exit codes tell you what PHPCBF did, and can not be changed using config settings:
I'm not sure what config options you have, but if you can have the script error only on exit codes > 1 then it would make more sense. Otherwise, you'll need to see if you can ignore the exit code for PHPCBF if you don't actually care what it did.
The exit code here is 1: phpcbf fixed everything that could be fixed (running phpcbf again doesn't give me any exit code message).
Composer custom scripts don't appear to have a way to override the exit code, so even though PHPCBF was 100% successful, it's still reported with a big scary red message.
I have found one workaround though. The relevant part of my script:
phpcbf -q src/SoapTypes > /dev/null || true

It still might be useful for some to have phpcbf output no report and no error code, but for now at least, my issue is solved. I'm leaving the issue open in case you wish to pursue, but otherwise, it can be closed.
Glad you found a solution for your case. I think an option to disable reporting (or at least change the exit code) would be helpful.
It may be an idea to add an option to use industry standard exit codes ?
Something like --exit-codes=zero-based
I realize changing the codes now would break BC too much, but adding an option to use the more common standard of 0 = success, non-0 = failure could be helpful.
I would expect the current exit codes of 0 and 1 to both report as 0 (success) in that case.
Refs:
Related: #1359
Another solution is to check the exit code directly after running phpcbf (it worked for me for running it via Composer script):
vendor/bin/phpcbf; if [ $? -eq 1 ]; then exit 0; fi
@gsherwood Is the changing of the exit code for phpcbf as proposed in https://github.com/squizlabs/PHP_CodeSniffer/issues/1818#issuecomment-354611665 something which could be earmarked for PHPCS 4.0 ?
Is the changing of the exit code for
phpcbfas proposed in #1818 (comment) something which could be earmarked for PHPCS 4.0 ?
Yep. Perfect time to redo them for both PHPCS and PHPCBF. I'll throw an issue in.
Is there any way to get a code for "There are errors that can't be fixed" instead of just saying its all good because they are not fixable?
eg, no namespace in psr12, snake case instead of camel case, no visibility declared etc. These are all errors and should be fixed. I would like my script to be able to tell devs "Hey there are still errors but I can't fix them"
Is there any way to get a code for "There are errors that can't be fixed" instead of just saying its all good because they are not fixable?
If you just want to know if there are errors, PHPCS is best for this. As of version 3, the PHPCS exist codes are:
So if you run PHPCS and get an exit code of 2, you can run PHPCBF and expect that some errors will be fixed. If you then run PHPCS again and get an exit code of 0, you know that no errors remain.
Yeah, I Was hoping to avoid checking files twice with two tools. But starting with cs and then only running fixes if necessary is a decent compromise. TY :)
Param --runtime-set ignore_warnings_on_exit 1 works on phpcs, but it does not for phpcbf.
There is a kind of workaround for a Composer script: _Run 2 times with an OR (||), so the second time will exit 0_.
{
"cs:fix": [
"phpcbf src/ tests/ config/ -p || phpcbf src/ tests/ config/ -q"
],
}
- Exit code 0 is used to indicate that no fixable errors were found, so nothing was fixed
- Exit code 1 is used to indicate that all fixable errors were fixed correctly
- Exit code 2 is used to indicate that PHPCBF failed to fix some of the fixable errors it found
- Exit code 3 is used for general script execution errors
— @ gsherwood
Looks ugly, non-DRY 😔, but it works.
@nelson6e65 Alternatively you could check the exit code of the first run in the || part of the commend. If it's 1, you should be fine.
@nelson6e65 Alternatively you could check the exit code of the first run in the
||part of the commend. If it's1, you should be fine.
Yes, thanks. I tried, but only on linux and was uglier. 😅 How can I check the exit code cross-os? I use Windows, Fedora and Ubuntu.
I use Windows, Fedora and Ubuntu.
Cross-platform is ugly. Had to roll a custom vendor/bin executable so composer exec everything would work on Windows https://github.com/conversionxl/phpcs-ruleset/commit/b0069f9d8f22c7613754e33a8824328e075801ef
Most helpful comment
It may be an idea to add an option to use industry standard exit codes ?
Something like
--exit-codes=zero-basedI realize changing the codes now would break BC too much, but adding an option to use the more common standard of
0 = success, non-0 = failurecould be helpful.I would expect the current exit codes of
0and1to both report as0(success) in that case.Refs: