4

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:

  • Stroustrup style braces (K&R variant)
  • space before conditional
  • space after function
  • spaces in parentheses

Example in the screenshot.Stroustrup braces and spaces

I don't want to apply PSR-1 or PSR-2 or PSR-12 or CakePHP or WordPress standards.

I've struggled with:

  • VS code internal PHP formatter
  • phpfmt (which has been last updated in 2018 and archived on GitHub)
  • PHP Intelephense (which has little to do with formatting, as far as I can tell)

phpfmt is the closest, but super-buggy.

VS Code settings wise, I roll with:

  • "files.autoSave": "onFocusChange",
  • "editor.formatOnSave": true,

So I'd like to instantly beautify my PHP files, please.

Thank you for any ideas!

  • 5
    I work with `intelephense` which can format your code. It does `space before conditional` and `space after function` by default. For `Stroustrup style braces (K&R variant)`, change the default settings _add `"intelephense.format.braces": "k&r",` to your `settings.json` file._ And you can't do `spaces in parentheses` with any php extension found in vscode (as far as I know) I hope it helped a little. – Raphaël Balet Aug 17 '20 at 07:22
  • 1
    @rbalet — Hey, thanks for that `"intelephense.format.braces": "k&r",` tip! At least function declarations now look a little better… however, the conditionals' formatting as `} else {` is just killing me, no joy with these. :/ – Anatoly IVANOV Aug 18 '20 at 20:46
  • I so wish this was possible. It is so close with the k&r-setting, however I do strongly agree that 'else' should have its own line! I would be happy if there was a setting to just let the formatter ignore inline spaces left for readability. That would allow vertical alignment of multiple lines of assignment, like this: https://www.reddit.com/r/vscode/comments/ct8yc2/how_can_i_stop_code_from_removing_inline_spaces/. See also these newer (and unresolved) SO-questions: https://stackoverflow.com/q/63034135 and https://stackoverflow.com/q/69091624 – glaux Dec 17 '21 at 10:47
  • @glaux — Exactly! So far, I just have a "write a custom PHP formatter extension" on my never-ending to-do list. Not a particularly fun project, to be honest. JavaScript is much better served in VS Code. Seeing how JS is moving away from Client Side and back to Server Side Rendering, my guess is that maybe at some point PHP will become redundant and the aforementioned PHP formatting problem will self-destruct. At least, for new, “from scratch” projects. ‍♂️ – Anatoly IVANOV Dec 18 '21 at 12:34
  • I have improved the answer, please take a look if it still has interest. – glaux Jan 10 '23 at 19:01

1 Answers1

1
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>
  • spaces in parentheses
<rule ref="PSR2.ControlStructures.ControlStructureSpacing">
  <properties>
    <property name="requiredSpacesAfterOpen" value="1" />
    <property name="requiredSpacesBeforeClose" value="1" />
  </properties>
</rule>
glaux
  • 701
  • 7
  • 20
  • Woha! That’s quite a detour, thanks so much for sharing this. I’ve read through the docs quickly, but will dive deeper and report back — I get your overall idea, thank you! – Anatoly IVANOV Jun 18 '22 at 05:16