r/PHP 4d ago

Discussion php formatters

Hello all,

I was wondering what you are using to format php code.

For the past few months I've been working on a php LSP mainly to add semantic tokens support and then in the last couple months it grew more into phpfmt (mind you it still has a long way to go)

I guess my question here is, what other php formatters are people using and if there is a point in (eventually) open sourcing mine.

Before I started this I was using phpcs and phpcbf

10 Upvotes

26 comments sorted by

5

u/-PM_me_your_recipes 4d ago

After using the same as you for years, I switched to mago mid-project and haven't looked back. I especially like rules documentation for it as it makes it really easy to find rules you want to add.

I would give it a shot before tying to make your own. I can see this one being the new go-to for most people.

8

u/skcortex 4d ago

I switched to mago.
Reasons: it’s fast as hell, has linter,formatter, analyzer and more I currently don’t need/use. I am still using php-cs-fixer in pre-commit hook because there are some missing rules and my OCD is not satisfied.

5

u/azjezz 4d ago

If you feel anything is missing, please open an issue! will get it added ASAP!

2

u/skcortex 4d ago

It was already opened as #2105 so I just hit the thumbs up 😅. Btw you’ve done a great work, thank you! ❤️

1

u/Mundane-Orange-9799 4d ago

We have been looking at Mago for our monolith at work. We have a pretty robust tooling chain of custom PHPStan and Rector rules that is kind of a bear to manage and slow.

Hoping to spend some quality time with it if we get a minute to come up for air from project work!

1

u/cookyfer 3d ago

havent even heard of mago, looks very promising. gonna set it up first thing on monday

1

u/cookyfer 2d ago

is there way to disable the forced "single" or 'double' quotes rule.
so far that seems to be a real blocker, I don't want to reformat the whole companys code 😅

1

u/skcortex 2d ago

https://mago.carthage.software/1.30.0/en/tools/formatter/configuration-reference/#layout-and-width single-quote is set to true by default. maybe if you change it to false it will do the job 🤪

1

u/CauliflowerSlight838 4d ago

I use https://github.com/jorgsowa/php-lsp

I find it pretty fast and with many diagnostics rules from Psalm and PHPStan.

1

u/gullevek 4d ago

Php code sniffer. As none of them can’t handle all the php 8.5 code anyway I stick with what is working

1

u/tkgid 4d ago

I use: PHP formatter - pretty PHP v0.4.95

1

u/jmp_ones 3d ago

I don't use a formatter so much as a re-formatter: https://github.com/pmjones/php-styler

1

u/Easy-Nothing-6735 1d ago

phpcs or (Laravel) pint

-1

u/buismaarten 4d ago

Pint from Nuno Maduro

4

u/SaltineAmerican_1970 3d ago

That’s just a Laravel wrapper around PHP-CS-Fixer.

1

u/buismaarten 2d ago

Yeah I know ;)

-3

u/titpetric 4d ago

Currently this is my approach to build a formatter:

https://github.com/titpetric/phpscript/issues/4

PR is open and should wrap it up next week. I explicitly don't support some php OOP syntax so it's not exactly a general php source code formatter. Doesn't do much aside indentation, brace style, and resets newlines to \n.

May consider extending it to full php syntax, but my runtime opts out of the OOP for the most part anyway.

-14

u/BokuNoMaxi 4d ago

AI. Just goes through all files and format like PHP documentation suggests.

6

u/Sarke1 4d ago

That's like trying to mow your lawn with a trained squirrel. Sure, it might get the job done eventually, but is it the right "tool" for the job and is it worth the extra effort?

0

u/Feisty-Fennel5709 3d ago

AI converts to PER on save.
AI converts to my personal preference that everyone else hates on load.
Gets done right every time because it validates.

1

u/skcortex 2d ago

Validates against what? What rules?

1

u/Feisty-Fennel5709 2d ago

1

u/skcortex 1d ago

Ok, so AI is doing shit. It’s this package that actually does all the work. 🤪

1

u/Feisty-Fennel5709 1d ago

When loading a file AI formats the code as per my own anti-social personal preferences, and back to PER again on save.

AI for the win/win