EDIT 2023-01-10
I have refined the answer below and published it on Github.
The repository have a minimal guide on how to install it locally, no docker needed.
It's the same principle as outlined in the original answer, but this new repository use PSR12 as a base ruleset and enforces rule 1, 3 and 4 while allowing rule 2 in OP. I still believe this gives the most freedom, but it can easily be tinkered with by forking the repository and inspecting the individual rules in VS Code by enabling "phpsab.debug": true
and "phpsab.snifferShowSources": true
in the settings.
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.
- Stroustrup style braces (K&R variant)
<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>