How to Add Yii2 Coding Standards in PHP_CodeSniffer

In this tutorial, you will learn how to add Yii2 Coding Standards in PHP_CodeSniffer.

Prerequisites

Yii2 Coding Standards have the ruleset of PHP_CodeSniffer. The ruleset is a set of sniffs that looks for things that are wrong and tells you about them so that you can fix them. So if we go to GitHub and look up the Yii2 Coding Standards and navigate to the Yii2 directory, we find the ruleset having a bunch of instructions for PHP_CodeSniffer to follow. Now ruleset tells PHP_CodeSniffer what to do.

To get this ruleset on your computer you have to use git so I am assuming that you have already installed git on your computer and we will install it as a standalone set so you can use it globally. Now copy & paste the following command which cloned the master branch and we will put it in yii2cs directory on our computer. In my case, I am on my D Drive and I have a special directory in my D Drive called utilities so I will change my directory by using the window command cd and navigate to utilities folder i.e.

cd utilities

and clone them.

git clone -b master git://github.com/yiisoft/yii2-coding-standards.git yii2cs

Now we have to tell PHP_CodeSniffer to go and look at this ruleset and to do that we will run a command phpcs and set some configuration with –config-set specifically, we want you to point at a new path with installed_paths and the path where we cloned it i.e.

phpcs --config-set installed_paths "D:\utilities\yii2cs"

After run above command, we will get the following output.

Config value "installed_paths" updated successfully;

Now if we type the following command and hit enter.

phpcs -i

We will see that we have the Yii2 Coding Standards available via the command line on PHP_CodeSniffer.

phpcsyii2-coding-standards