Help me Stack Overflow, you are my only hope.
I’d like to apply auto-formatting in VS Code to PHP files with the following rules:
Example in the screenshot.
I don't want to apply PSR-1 or PSR-2 or PSR-12 or CakePHP or WordPress standards.
I've struggled with:
phpfmt is the closest, but super-buggy.
VS Code settings wise, I roll with:
So I'd like to instantly beautify my PHP files, please.
Thank you for any ideas!
I finally sat down and figured out how to achieve this. This is from an Ubuntu perspective, but I should think that it could be made to work on the other platforms as well.
I tested a bunch of linter/fixer extensions with no real progress until I ended up at this.
It was a bit involved to install a version of phpcs/phpcbf together with a ruleset that the extension could use, so I bundled it all up in a docker container (but everything can be done locally as well with composer of course).
I haven't been able to find rules that can enforce the two whitespace rules in OP (space before conditional and space after function), but there are a lot of customization possible, including writing your own sniffs (and I didn't look super hard to be honest). But the default ruleset/standard in the project linked above allows extra whitespace, so that is half the battle I feel.
For the other rules the following are the explicit parts needed, but please do check out there resources: 1 and 2 and enable "phpsab.snifferShowSources": true
and "phpsab.debug": true
when creating a custom ruleset.
<rule ref="Generic.Functions.OpeningFunctionBraceKernighanRitchie">
</rule>
<rule ref="Drupal.ControlStructures.ControlSignature.SpaceAfterCloseParenthesis">
</rule>
<rule ref="PSR2.ControlStructures.ControlStructureSpacing">
<properties>
<property name="requiredSpacesAfterOpen" value="1" />
<property name="requiredSpacesBeforeClose" value="1" />
</properties>
</rule>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With