PHP-CS-Fixer: How to Install and Use in VS Code 2020 on Windows 10.

Raja Palanivel
4 min readJul 2, 2020

Why PHP-CS-Fixer for VS Code?

VS Code needs third party extensions like PHP-CS-Fixer to lint / format PHP code to coding standards. This guide will help with the installation of PHP-CS-Fixer in VS Code (2020). It is assumed that you have already used VS Code for PHP development and Composer.

Installation

(Come back after browsing this link to get an idea of what steps are involved.)

  1. Install php-cs-fixer globally using Composer.
$ composer global require friendsofphp/php-cs-fixer
Installing PHP-CS-Fixer globally using Composer

2. Set the PATH for Composer’s bin folder from VS Code’s terminal if required. I used Git Bash terminal for this example. If you are already using Composer for your PHP projects, the bin folder should already be in your PATH. So, this step is optional. Bit more info on this here.

$ export PATH=”$PATH:$HOME/.composer/vendor/bin”
Setting PATH (optional)

3. Install php cs fixer extension by junstyle in VS Code.

VS Code Extension Marketplace

Settings

4. Add these settings to your existing VS Code’s settings.json file. Replace your_user_name_here in these settings with yours in three places.

Settings to go in settings.json file
Creator’s comment
VS Code’s settings.json file

Leave "php-cs-fixer.allowRisky": as false, More on this setting is at Step:9.

5. Download this zip file and extract the config.php_cs file. Place the config.php_cs file in your Composer’s bin folder. (This is not the best practice to place a config file in Composer’s binary folder. You can place the config.php_cs file anywhere including your PHP Projects folder, but make sure you change the location path at "php-cs-fixer.config": in your VS Code’s settings.json file (Step:4) to match where you placed the config.php_cs file.)

Location of php-cs-fixer.bat and my config.php_cs file

Usage

6. That’s all. Now restart VS Code and format a PHP file in VS Code to see the results. (With the given settings at Step:4, VS Code should also automatically format the file on every save.)

Formatting a PHP file

7. If you see “php-cs-fixer is formatting and finishing” in the status bar but your PHP file is not formatting to your standards, then play with the rules in your config.php_cs file. At this point, all you need to get right is the config.php_cs file. See Step:8 for what rules worked for me.

php-cs-fixer is formatting and finishing

8. My config.php_cs file. I tested few PHP-CS-Fixer rules and added comments in this file.

Hint: Edit your config.php_cs file in NotePad++ rather than VS Code, to see Syntax highlighting, so you don’t make any mistakes like missing a comma or semi-colon. VS Code did not do Syntax highlighting for .php_cs files. config.php_cs file is basically a PHP file and NotePad++ recognizes it.

My config.php_cs file

9. Risky Rules (from Step: 4): For example, PHP-CS-Fixer can automatically change && in your PHP code to and but this is one of those Risky rules. Risky rules may/may not break your code, so research each rule before using them in your PHP-CS-Fixer. Learn more about Risky rules here.

A Risky rule

Conclusion

Hope this tutorial is helpful for you and saved you hours trying to figure-out how to install if you are new to PHP-CS-Fixer. Let me know in the comments how you go with your PHP-CS-Fixer install.

My Versions

  • PHP 7.4
  • Windows 10
  • VS Code v1.46.1
  • Composer v1.10.7

--

--