Menu
27/03 2018

Formatting PHP in VS Code

I recently had one of those brick wall moments over a workflow issue all developers have, especially those who try to stick to open source solutions for everything: Code formatting!

It is one of those things, just like keeping the bathroom clean and tidy, allowing us to know where everything is without having to fish through untidy code to figure out why things aren't running as smoothly as we'd like. With HTML it is especially important and can iradicate the rogue div causing an entire site to break (among other issues). Many decent system templates are in fact *.php files too so it makes this doubly important.

I recently started using VS Code which has a lot of really useful, modern and forward thinking workflow tools built in. It doesn't have a decent PHP formatting capability when PHP is mixed with HTML though. This led me on a journey which I thought I'd share with you.

Step one (the only step I believed there would be when I started): This was to search through VSC's extension library and found one that theoretically did format PHP. I installed and had error as it couldn't find a *.dll file it needed as it was in effect a go between extension that assumed my system already had the tool it needed installed.

Step 2: After googling (another useful tool for every developer) it was clear that this tool could be installed with composer. My version of composer was out of date and needed upgrading in order to fully function. Installing composer exposed an issue with my local development environment (I use wamp so I can very quickly switch between projects - ie immediately).

Step 3: I therefore needed to install the latest version of this and migrate all my projects to it. This didn't take too long* as I'm a pretty organised chap and know where everything is. Thankfully knowing wamp inside out helps as there were some large databases to move!
* about a day

Step 4: Anyway, after reinstalling wamp, which was actually a really useful thing to do as it includes an easier way of switching between php versions (I was previously doing it manually, which is what exposed the issue when installing composer in the first place) I could then smoothly install composer and in turn the package that my new extension needed in order to format PHP files (Step 5, kind of).

Step 5: Once this was I done I could override settings in VS Code so that the new plugin could speak to the new package on my system:


{
    "php.validate.executablePath":"C:\\wamp64\\bin\\php\\php7.0.23\\php.exe",
    "php-cs-fixer.executablePathWindows": "php-cs-fixer.bat",
    "php-cs-fixer.formatHtml": true,
}

Once this was done it was time to breath a sigh of relief once the alert had gone, restart and breath in again. My PHP/HTML was this:


<?php
$time = strtotime('2000-04-28 17:25:43');

echo  humanTiming($time) ;

function humanTiming ($time)
{

$time = time() - $time; // to get the time since that moment
 $time = ($time<1)? 1 : $time; $tokens = array ( 31536000 => 'year',
        2592000 => 'month',
604800 => 'week',
86400 => 'day',
	3600 => 'hour',
60 => 'minute',
1 => 'second'
    );

foreach ($tokens as $unit => $text) {
if ($time < $unit) continue; $numberOfUnits = floor($time / $unit); return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'');
}

}
?>

<?php echo humanTiming($time); ?> years of making the Web easier.

© Webegg <?php echo date('Y'); ?>

and now it is this (after doing a Shift+Alt+F)


<?php
$time = strtotime('2000-04-28 17:25:43');
echo  humanTiming($time) ;
function humanTiming($time)
{
    $time = time() - $time; // to get the time since that moment
    $time = ($time<1)? 1 : $time; $tokens = array( 31536000 => 'year',
        2592000 => 'month',
        604800 => 'week',
        86400 => 'day',
        3600 => 'hour',
        60 => 'minute',
        1 => 'second'
    );

    foreach ($tokens as $unit => $text) {
        if ($time < $unit) { continue; } $numberOfUnits = floor($time / $unit); return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'');
    }
}
?>

<?php echo humanTiming($time); ?>years of making the Web easier.

© Webegg <?php echo date('Y'); ?>

It was quite a journey to get there but a typical day in the life. That's progress 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

This article is in the Web Industry category. Here are some other related articles also in this category.