Setup VS Code for Efficient PHP Development - 10 minutes read


Setup VS Code for Efficient PHP development 🚀

I recently started programming in PHP using the well-known Symfony framework. I wanted to keep my VS Code habits and proficiency I had from my previous projects in Node.js and Vue.js, so I tried to configure VS Code for PHP development as well. Moreover, I didn’t want to invest €199 in the famous PHPStorm IDE… 😕

In this article, I will explain how I managed to make my development environment comparable to PhpStorm’s. The commands will be detailed for Ubuntu 18.04, but they can be adapted for Mac OS and possibly for Windows.

I will take an empty Symfony application as an example for this article. Let’s start!

To be able to run PHP code you obviously need to install PHP. You will also probably need Composer, a commonly used dependency manager for PHP. You can install them by running the following command:

You can install instead of if you want to install some useful libraries as well.

Let’s now create an empty Symfony app. Then open VS Code and launch the development server:

VS Code needs to watch for file changes to work correctly. That’s why the following warning may appear because our Symfony project contains a lot of files:

To fix it, you can run the following commands in your terminal, and then restart VS Code:

Go to http://127.0.0.1:8000 from your favorite web browser and check that your Symfony website works.

Let’s create a new route in our application. Just add an empty file  in the  folder.

By default, VS Code provides some basic code suggestions but they are generic and quite useless. To get useful code suggestions as you type, I recommend installing the PHP Intelephense extension for VS Code. I also tried the PHP Intellisense extension but it’s slower on big projects and provides fewer features (it doesn’t gray out unused imports for example).

Now that the PHP Intelephense extension is installed, we can start writing our HelloWorldController!

As you can see, code suggestions are very relevant and VS Code auto imports the corresponding namespace when you validate a suggestion!

PHP Intelephense can also auto-suggest relevant methods when typing . The problem is that these suggestions are polluted by the default PHP suggestions.

To fix that, you can disable these basic suggestions in VS Code settings. Simply open VS Code’s  file (press  then click on the top right  icon) and add the following line:

When it’s done, you can see useful suggestions!

When you write PHP code, a good practice is to add PHPDoc annotations to make your code more understandable and to help your IDE provide you with relevant code suggestions. By default, VS Code doesn’t suggest anything while writing annotations. To activate code suggestions in comments, open the  file and add the following line:

By default (I don’t know why to be honest 😕), PHP Intelephense excludes test folders. This behavior is specified in the default settings:

To enable suggestions for tests, all you need to do is edit your  file and add:

When you want to get more information about a function, you can Ctrl+Click on it and VS Code will open the line where the function is declared!

If you want VS Code to generate getters and setters for you, you can install this extension. Then, right-click on a class property and select Insert PHP Getter & Setter.

Debugging your PHP code can be painful without a debugger. You can use  (😕) but it’s not very convenient…

The best way to debug your PHP code is to use Xdebug, a profiler and debugger tool for PHP.

Xdebug is not shipped with PHP, you need to install it on your development environment:

Then run  and add the following lines:

Tip!If your PHP project runs in a Docker container, you need also to add the following line to the file, where is the IP address of the interface on your computer (on Mac OS you have to put instead):

Check that Xdebug is successfully installed by running . You should get something like:

To be able to use Xdebug in VS Code, you need to install the PHP debug extension.

Then go to the debug tab in VS Code (accessible in the side menu) and click on Add configuration. Select PHP, and close the newly created  file:

Your debugging environment is now ready! 🕷🚀

Let’s debug our  to see which are the query parameters given by the user. You can use the following code for example:

Press F5 in VS Code to start the debugging session.

Uncheck the Everything option in the breakpoints list and put a breakpoint at line 18 by clicking left of the line number. Then go to http://127.0.0.1:8000/hello?name=Louis. You can now see the value of local variables by hovering the mouse pointer over them:

You can do a lot of amazing things with the debugger 😃, like:

For more information about the power of VS Code debugger, you can visit the official website.

When you encounter an error, or when you use the Symfony debug toolbar or the profiler, you may want to open the desired file directly in your IDE with the cursor at the corresponding line.

To be able to do that, you need to add the following line to your :

Tip! If you run PHP in a Docker container, you can specify a path mapping between your host and your docker like this:

Now, when an error occurs, you can click on it and go directly to the corresponding line in VS Code:

When you visit a route, you can also jump to the corresponding controller by clicking on the debugging toolbar:

Formatting and linting your code is a very good practice to keep it clean automatically, and to inform you about some errors.

The best and commonly used linter for PHP is PHP CS Fixer. It provides a large number of rules to keep your code clean. You can visit this website for more information about the rules and the categories they belong to.

To be able to auto-format your PHP files in VS Code, you need to install the php cs fixer extension.

The most complete and safe category of rules is . It’s a good point to start. To enable this set of rules in VS Code, open  and add the following line:

To auto-format your PHP files on save, you also need to add the following lines to your :

If you want to disable or enable some specific linting rules, you can do it in a  file at the root folder of your project. If this configuration file is present, VS Code takes it into account and overrides the configuration found in . You can find more information about the  file in the GitHub repository. A simple config file to use  category and disable some rules could be:

Prettier for PHP is a code formatter which makes some improvements that PHP CS Fixer does not do. It allows you, among other things, to configure a max line length and makes your code even cleaner.

To add Prettier for PHP to your PHP CS Fixer configuration, you can follow these instructions.

Depending on your computer’s speed, the length of your files and the number of rules you activate, linting a file on save can be slow, causing VS Code to refuse it. To fix this behavior, change the formatOnSaveTimeout in your :

To activate syntax highlighting for Twig files in VS Code, you need to install the Twig Language 2 extension.

To enable emmet suggestions like in HTML files, add the following line to your :

A good VS Code extension to manage your databases is SQLTools. Feel free to install it and manage your databases directly from your IDE!

Enjoy coding in PHP with VS Code 🚀 and feel free to give me your feedback! 😉

Want to learn more about how Theodo can help bring your Symfony project to the next level?Learn more about our team of Symfony experts.

Source: Theodo.com

Powered by NewsAPI.org

Keywords:

Source code • PHP • Software development • Computer programming • PHP • Symfony • Source code • Node.js • Vue.js • Source code • PHP • PhpStorm • Integrated development environment • Integrated development environment • PhpStorm • Command-line interface • Ubuntu (operating system) • Classic Mac OS • Microsoft Windows • Symfony • Application software • PHP • Source code • PHP • PHP • Command-line interface • Library (computing) • Symfony • Application software • Open-source software • Source code • Software development • Server (computing) • Source code • Computer file • Symfony • Computer file • Command-line interface • Computer terminal • Source code • Bookmark (World Wide Web) • Web browser • Symfony • Website • Application software • Computer file • Directory (computing) • Source code • BASIC • Source code • Generic programming • Source code • PHP • Filename extension • Source code • PHP • Intelligent code completion • PHP • Source code • Namespace • Data validation • PHP • Method (computer programming) • PHP • BASIC • Source code • Open-source software • Source code • Computer file • Event (computing) • Icon (computing) • PHP • Source code • PHPDoc • Source code • Integrated development environment • Source code • Source code • Source code • Comment (computer programming) • Open-source software • Computer file • PHP • Directory (computing) • Computer file • Subroutine • Computer program • Subroutine • Computer program • Filename extension • Context menu • PHP • PHP • Source code • Debugger • PHP • Source code • Xdebug • Profiling (computer programming) • Debugger • Programming tool • PHP • Xdebug • PHP • Integrated development environment • Execution (computing) • PHP • Docker (software) • Operating-system-level virtualization • Video • File system • IP address • Interface (computing) • Classic Mac OS • Xdebug • Xdebug • Source code • PHP • Debugger • Filename extension • Debugger • Tab (GUI) • Source code • Accessibility • Menu (computing) • Event (computing) • Configuration file • PHP • Close (system call) • Computer file • Debugging • Debugging • Query string • User (computing) • Source code • Source code • Debugging • Command-line interface • Breakpoint • Debugger • Source code • Debugger • Website • Software bug • Symfony • Debugging • Toolbar • Profiling (computer programming) • Computer file • Integrated development environment • Cursor (user interface) • PHP • Docker (software) • Docker (software) • Software bug • Computer program • Debugger • Toolbar • Source code • Software bug • Linter • PHP • PHP • Computer science • Public relations • Source code • Website • Information • PHP • Computer file • Source code • PHP • Computer science • Public relations • Category theory • Set (mathematics) • Open-source software • PHP • Lint (software) • Root directory • Configuration file • Source code • User (computing) • Information • GitHub • Repository (version control) • Configuration file • Rule-based system • PHP • Source code • PHP • Computer science • Public relations • Machine code • PHP • PHP • Computer science • Photographic fixer • Instruction set • Computer file • Computer file • Syntax highlighting • Computer file • Source code • Programming language • Filename extension • HTML • Computer file • Source code • Filename extension • Database • Database • Integrated development environment • Computer programming • PHP • Source code • Feedback • Theodo of Bavaria • Symfony • Project management • Symfony •