2

I'm trying to implement other coding standards for phpcs. Currently, I have setup phpcs for visual studio code and I have the following coding standards

current/path> phpcs -i
The installed coding standards are MySource, PEAR, PSR1, PSR2, Squiz and Zend

I want to set up other standards listed here: php-fig

For example PSR4

How do I go about setting these standards for my development?

AashikP
  • 43
  • 1
  • 6
  • PHPCS enforces coding style standards, like PSR-1 and PSR-2. PSR-4 is an autoloading standard and PHPCS will not be able to enforce this. So you've got PHPCS installed correctly, but you'd need to add support for other PSRs using different tools (I'm not sure which ones). – Greg Sherwood Dec 11 '17 at 22:06

2 Answers2

2

Expanding @Vladd's answer with an example.

Let's say we would like to enable the WordPress coding standards from this repo . We can follow the following steps:

  1. Download and save the file to a local directory. For some reason I wanted it to be in the same folder as default phpcs standards (global installation). So I've saved it to: C:\Users\USER_NAME\AppData\Roaming\Composer\vendor\wpcs

  2. If you have git setup, you can directly clone the repo to this folder by accessing the folder (vendor), hitting shift+right click and clicking on 'Open Command Window Here'. Once the command prompt is loaded, just paste this code:

    git clone -b master https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards.git wpcs

    This will clone the standards into a folder called wpcs within your vendor folder.

  3. Once you have the standards downloaded, you'll need to let phpcs know where to find the new standards. That's done by this command:

    phpcs --config-set installed_paths C:\Users\USER_NAME\AppData\Roaming\Composer\vendor\wpcs

    Make sure you change the path to where you have downloaded the standards.

  4. You can cross check all the installed coding standards using the phpcs -i command. you should see the following result:

    The installed coding standards are MySource, PEAR, PSR1, PSR2, Squiz, Zend, WordPress, WordPress-Core, WordPress-Docs, WordPress-Extra and WordPress-VIP

  5. You can now enable the standard you would like to use for your project. For example. If I want to switch from the default standard to wordpress, I'd use the following command:

    phpcs --config-set default_standard wordpress

AashikP
  • 43
  • 1
  • 6
0

You may set the default_standard used by phpcs using the following command:

phpcs --config-set default_standard <value>

or when using composer dependency manager from the root of your project issue the following command:

./vendor/bin/phpcs --config-set default_standard <value>

Keep in mind that PS4 is autloading standard, not an coding standard.

You can create a custom, your own, coding standard and then you can try to install your custom coding standard:

phpcs --config-set installed_paths <path/to/custom/coding/standard>

or when using composer dependency manager from the root of your project issue the following command:

./vendor/bin/phpcs --config-set installed_paths <path/to/custom/coding/standard> 

Hope it helps.

Vladd
  • 124
  • 5
  • I wish I had this answer back then, could have saved me a couple of hours. Anyway, I did get to learn some more regarding the topic, and @Greg Sherwood reply helped as well. I'm marking this as the best answer and providing my own answer as an example. – AashikP May 09 '18 at 11:24