Skip to main content

Format poorly formatted PHP code

ยท 2 min read
Christophe
Markdown, WSL and Docker lover ~ PHP developer ~ Insatiable curious.

Format poorly formatted PHP code

You're recovering some old PHP code; you want to answer a question asked on a forum and the person who posted the PHP question didn't take care to ensure that it was properly formatted; ... there are too many occasions when the syntactic quality of a code can be rotten.

You'd like to have an online tool that you can use to quickly retrieve code with a much more polished layout, just by copying and pasting. Without having to install anything and without any headaches.

For example, how do you make the code below look cleaner in five seconds?

<?php
abstract class mycollection implements Iterator
{
protected function populate($array, $dataobject){
foreach($array as $item){
$object = new $dataobject();
foreach($item as $key => $val){$object->$key = $val;}
$this->storage[] = $object;
}}
}

There are a huge number of linters on the internet, but this is perhaps one of the best: https://benjamincrozat.com/pint-express. It's based on the Laravel Pint tool (but not limited to Laravel code for sure).

So, here was how the code looks&like before:

Correctly formatted PHP code

Jump to Pint-express, copy the code in the Code textarea of the script and let's the reformatting occurs:

Correctly formatted PHP code

Much better.

Note: there are other tools like for instance https://codebeautify.org/php-beautifier.

If you're a PHP developer ...โ€‹

... then make sure to use tools like PHP-CS-Fixer or PHP_CodeSniffer or many others. See also my Docker image that provides static analysis tools for PHP.

Think to add these tools in your editor (there are many VSCode extensions), in your workflow (f.i. by using local make actions) or adding these steps in a remote pipeline