r/PHPhelp • u/FPGA_Superstar • Aug 15 '24
Does PHP have a standardised style?
I'm new to coding in PHP, and the code for the project I'm working on is an ugly mess. There is nothing in the way of standardised formatting or style. I suspect this is uncommon in PHP projects, and I'd like to correct it. But I'm not sure what's idiomatic or standard practice today.
Ideally, I want something like the opinionated Black formatter from Python. Does something like this exist in PHP?
8
u/jmp_ones Aug 16 '24 edited Aug 16 '24
2
u/FPGA_Superstar Aug 16 '24
Excellent! Is it considered industry standard? Or is there another bigger project? 33 stars is higher than I've got any project, but a lot lower than Black's 38k.
2
u/jmp_ones Aug 16 '24 edited Aug 16 '24
Is it considered industry standard?
The project itself? Not by a long shot; though, do note the styling it applies is of the same lineage as Horde/PEAR/PSR/etc.
is there another bigger project?
As far as Black-like, no, not that I'm aware of, and I looked around for one. If you find one please let me know.
2
u/martinbean Aug 16 '24
PSRs. Just adhere to them and you then don’t have to debate tabs versus spaces, 2 spaces or 4, where braces should go, etc.
1
u/FPGA_Superstar Aug 16 '24
This is exactly what I want to do! But I don't want to do it myself, do you know of any automatic formatter?
1
19
Aug 15 '24
There is PSR-1 and PSR-12 documents (https://www.php-fig.org/psr/psr-12/), which basically gice the de factor standard for modern PHP applications.
There are tools like PHP cs fixer, which help you to follow and enforce these rules (and some more specific rules if you like)
1
u/AmiAmigo Aug 17 '24
You don’t like to code like a monkey!?
1
1
u/MatadorSalas11 Aug 16 '24
I use Laravel Pint to format my code but I don't know if it works outside Laravel
1
1
-3
9
u/eurosat7 Aug 15 '24 edited Aug 15 '24
PSR-12 or the followup PER CS 2.0 for code layout.
PHP-FIG offers in general some PSR to suggest standards.
Strong frameworks like symfony offer a lot of great components and tutorials. If symfony is too heavy laravel changes some things to be faster and easier in project development.
An IDE like PhpStorm brings some very strong quality checkers.
There are tools like php-cs, phpstan, phpmd, psalm, pdepend and phpcpd which help to write error resistant and more future proof code. (There are some more)
But none of it is enforced.
If you have old/legacy code it might help to use rector/rector.
Also moving a team to modern php might be easier if pointed to phptherightway.com - it is not perfect but quite good and very helpful to raise awareness.