Setup CodeSniffer for CakePHP 2.x in VSCode (Per-Project Installation)

I have recently switched my code editor from Sublime Text 3 to Visual Studio Code (VSCode). Here I’m going to share how I integrated PHP CodeSniffer with CakePHP 2 standards.

Before I’m going to proceed, I just want to let you know that the code sniffer for CakePHP 2 uses an older version of PHP CodeSniffer. So if you have the code sniffer already installed globally to your computer, you’d want to check the version. If the phpcs version is greater than version 1.5.6, you would have to either downgrade it or have it installed per-project. I prefer to do the latter and I’m going to show you how I did it. So let’s get started!

Install Composer

First of all, you have to install composer. As for composer, you may also install it locally/per-project, but I prefer to install it globally.

For the installation, you may find it in their website here.

Install PHP CodeSniffer

composer require --dev squizlabs/php_codesniffer:^1.5

Install the CakePHP 2 Coding Standard

Go to your CakePHP 2.x project’s root directory, and then run the following command:

composer require --dev cakephp/cakephp-codesniffer:^1.0.0

When you have the packages above installed, let’s test it by showing list of coding standards: (Make sure that you are still inside the root directory of your project.)

./vendors/bin/phpcs -i // Output The installed coding standards are MySource, PEAR, PHPCS, PSR1, PSR2, Squiz and Zend

As you can see from the result above, CakePHP is not listed in the installed coding standards even if we have the CakePHP code sniffer package installed. It doesn’t get configured automatically.

We have to configure phpcs manually by running the command below:

./vendors/bin/phpcs --config-set installed_paths "$(pwd)/vendors/cakephp/cakephp-codesniffer" // Output Config value "installed_paths" added successfully

Let’s check it again

./vendors/bin/phpcs -i // Output The installed coding standards are MySource, PEAR, PHPCS, PSR1, PSR2, Squiz, Zend and CakePHP

And as you can see, we are able to successfully ad the CakePHP coding standard.

You can now run code check from the terminal with the command below:

./vendors/bin/phpcs -p --extensions=php --standard=CakePHP ./app

Install PHP CodeSniffer for Visual Studio Code

Open Visual Studio Code and install the phpcs extension.


Next, you have to open your project folder in VSCode.

Now open the Workspace Settings and add the configuration below:

{ "phpcs.executablePath": "vendors/bin/phpcs", "phpcs.standard": "CakePHP", }

Now try to open some random php file and convert the indentation to spaces. You should see some warning saying, “Tabs must be used to indent lines”.

And that’s it!

If you encounter “Unable to locate phpcs” error, try setting the absolute path of your phpcs like below:

{ "phpcs.executablePath": "/Users/ono/codes/cakephp-app/vendors/bin/phpcs", "phpcs.standard": "CakePHP", }