Supercharge PHP development with VS Code

Extension guidelines to fast-track development in PHP on Visual Studio Code and setting up the ultimate PHP development editor 🤩

In the last post, I introduced VS Code, a free editor from Microsoft that is set to rival the likes of Atom IDE and Sublime. Though VS Code is not projected as a complete IDE, its features and extensions contribute to a great overall experience and scope for better things to come out in the future. Now, let me suggest a few extensions that I found to be most useful and are essential for setting up a PHP development environment.

Let’s get started 🖥

PHP IntelliSense

This is an extensions that suggests auto-completion and refactoring for PHP. On its downside, it requires minimum PHP 7 to work, so if your still using PHP 5.6 or earlier, you will need to upgrade. Also, you need to disable the VS Code built-in PHP auto-completion.

VS Code PHP IntelliSense plugin
VS Code PHP IntelliSense plugin
VS Code PHP IntelliSense plugin

Configuring this is easy. Open File -> Preferences -> Settings, alternatively use shortcut Control+Comma to open up the User Settings. Next, find for the settings php.executablePath and modify them as follows :

  • php.validate.executablePath : Place the path to your PHP 7 executable here, if this is left blank/null then VS Code looks for PHP from your System Path
  • php.suggest.basic : Turn this to false
VS Code default PHP settings
VS Code default PHP settings
VS Code default PHP settings
VS Code custom user PHP settings
VS Code custom user PHP settings
VS Code custom user PHP settings

That’s it ! Now your ready to test out the plugin in action and get a nifty productivity bump 😎

PHP CodeSniffer

This plugin simplifies your life by auto executing php-cs-fixer each time you save a PHP file. Pre-requisites include having php-cs-fixer installed and added to system path. Test your installation by executing, php-cs-fixer — version in terminal/CMD.

If however you don’t have phpcs installed, I recommend using composer to install it,

composer global require FriendsOfPHP/PHP-CS-Fixer

Link to repository :: https://github.com/FriendsOfPHP/PHP-CS-Fixer

Once installed, you can configure the plugin as per your requirements, for example,

PHP Constructor

Any object oriented programming language requires constructors to initialize class variables during object creation. PHP Constructor can initialize constructor properties in your class and helps save some time while creating new class skeleton. This extension is dependency free.

PHP Constructor for VS Code
PHP Constructor for VS Code
PHP Constructor for VS Code

PHP Debug

This extension is a debug adapter between VS Code and XDebug for PHP.
To use this plugin, configure PHP to use XDebug by adding zend_extension=path/to/xdebug to your php.ini, also XDebug should be enabled in your php.ini

[XDebug]
zend_extension="C:\XAMPP\php\ext\php_xdebug.dll"
xdebug.remote_enable = 1
xdebug.remote_autostart = 1

My recommended php.ini configuration,

[XDebug]
zend_extension="C:\XAMPP\php\ext\php_xdebug.dll"

xdebug.remote_enable=1
xdebug.remote_handler=dbgp
xdebug.remote_host=localhost
xdebug.remote_autostart = 1
xdebug.remote_port=9000
xdebug.show_local_vars=1
PHP Debug for VS Code
PHP Debug for VS Code
PHP Debug for VS Code

For further understanding I recommend reading the extension use documentation at VS Code extension page.

PHP DocBlocker

A Basic PHP DocBlocking extension that helps you generate doc-blocks for classes and functions. It auto-completes tags such as @param, @return, @throws also inferring of param and return types from signatures. This extension has no dependencies.

PHP DocBlocker for VS Code
PHP DocBlocker for VS Code
PHP DocBlocker for VS Code

PHP Getters & Setters

This extension helps you generate getters and setters based on property description. Its highly configurable and uses configuration options to show doc blocks as you like them. Also, its dependency free !

PHP getters and setters for Visual Studio Code
PHP getters and setters for Visual Studio Code
PHP Getters and Setters for Visual Studio Code

PHP Mess Detector

This is a must have extension and helps analyse your PHP source code on save. It alerts you of unused variables and other nifty issues and overall helps you produce quality code. If you have PHP configured in your path, then no furthur configurations are needed. You can customize which PHPMD rules are used and supports custom PHPMD ruleset files.

PHP Mess Detector for VS Code
PHP Mess Detector for VS Code
PHP Mess Detector for VS Code

Better PHPUnit

A simple phpunit test case execution suite that helps you either execute individual method or entire suite from within the editor. It’s powerfull features include running tests over ssh for VMs like Laravel Homestead. There are no stringent dependencies for this extension, apart from phpunit to be globally installed.
I will update detailed configurations for using this extension later.

Better PHPUnit for VS Code
Better PHPUnit for VS Code
Better PHPUnit for VS Code

Here are a few shortcuts you can use : Run a test method:

Place your cursor in/on the method you want to run
Open the command menu: cmd+shift+p
Select: Better PHPUnit: run

Run a test file:

Place your cursor on/above the class declaration line
Open the command menu: cmd+shift+p
Select: Better PHPUnit: run

Run the entire suite:

Open the command menu: cmd+shift+p
Select: Better PHPUnit: run suite

Run the previous test:

Open the command menu: cmd+shift+p
Select: Better PHPUnit: run previous

PHP Project

Hassle free php server, sets current workspace or selected directory as a root directory for PHP server. Requires PHP server installed. XAMMP, Wamp or whatever suits you needs and has PHP executable. Path to executable must be set in settings, example “php.validate.executablePath”: “C:/XAMPP7/php/php.exe”

PHP Project for VS Code
PHP Project for VS Code
PHP Project for VS Code

Finally 🧘‍♂️

The thing that’s best about VS Code is that it’s free and open source. With a large community backing it up, frequent updates to extensions and new ones, pouring in every month, this editor is definitely destined to capture the top spot !
Sure, there are other worthy editors available and I often believe the choice to settle with depends on personal expectations from an editor. What VS Code delivers, is a robust platform on which you can configure almost every aspect of your dev needs and supercharge the experience.
In a recent post, I read VS Code is probably the best editor right now, while the traditional PHP goto IDE PHPStorm has slipped to 16th place. While this is not unexpected, given the hefty licencing cost of PHPStorm, we might see more push towards open source editors in near future, especially as the feature set gap tends to shrink.
Original article, here.

I hope this post was helpful. For queries/concerns fire up the comment section below !
Thanks 🤠

Author. Developer. Minimalist.