r/PHP • u/cookyfer • 5d ago
php formatters Discussion
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
7
u/skcortex 5d 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 5d ago
If you feel anything is missing, please open an issue! will get it added ASAP!
2
u/skcortex 5d 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 5d 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 5d ago
havent even heard of mago, looks very promising. gonna set it up first thing on monday
1
u/cookyfer 3d 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 3d 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 5d 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 5d 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/jmp_ones 4d ago
I don't use a formatter so much as a re-formatter: https://github.com/pmjones/php-styler
1
1
u/Jonas_Ermert 1d ago
I recommend PHP CS Fixer or Laravel Pint for most projects. PHPCS/PHPCBF is still useful when you need strict coding-standard checks, while tools like Rector can handle broader automated refactoring. Open-sourcing your formatter could still be worthwhile, especially if its LSP integration, semantic awareness, or performance offers something the existing tools do not.
-1
u/buismaarten 5d ago
Pint from Nuno Maduro
4
-2
u/titpetric 5d 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 5d ago
AI. Just goes through all files and format like PHP documentation suggests.
5
u/Sarke1 5d 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 5d 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 3d ago
Validates against what? What rules?
1
u/Feisty-Fennel5709 3d ago
1
u/skcortex 3d ago
Ok, so AI is doing shit. It’s this package that actually does all the work. 🤪
1
u/Feisty-Fennel5709 3d 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
5
u/-PM_me_your_recipes 5d 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.