If you are concerned about delivering high-quality code, compliant with WordPress coding standards, you’d better use an automated tool for these checks.
PHP Code Sniffer together with PHPStorm editor is a great and powerful combination as you get all these code checks in real time, as you type your code.
There are many great tutorials on how to install PHP Code Sniffer with PHPStorm on Mac, but we did not find any article explaining how to install and configure these tools on Windows.
So here’s a small contribution for those who need to check their code on Windows:
- Using Composer
- Installing PHP Code Sniffer
Install composer: https://getcomposer.org/download/
Follow instruction from: https://github.com/squizlabs/PHP_CodeSniffer#composer - Update
Open cmd
Run this command: composer global update “squizlabs/php_codesniffer”
- Installing PHP Code Sniffer
- Using PEAR
- Install PHP. Suggested are bundle: XAMPP OR WAMP
- Restart computer
- XAMPP ship with PEAR and PHP_CodeSniffer you can skip steps 2-
- To check if PHP_CodeSniffer is installer run this command in CMD: phpcs -i
- Add System Environment variable: https://www.screencast.com/t/1zs1WY31l with link to PHP.exe from BIN folder
- How to install PEAR(if needed)
- Download from: http://pear.php.net/go-pear.phar
- Copy PEAR to php location
- Open CMD to this location. Can be done two ways
- RUN -> write cmd -> CD into your php server folder
- Open Explorer -> go to PHP location -> SHIFT + RIGHT CLICK there and open SHELL
- RUN command php go-pear.phar
- Check folder settings to be OK
- Follow on screen details and complete installation
- Install PHP_CodeSniffer
- Run command from cmd(see step 3-C-i and 3-C-ii) pear install PHP_CodeSniffer from PHP -> PEAR folder
- Download WORDPRESS STANDARDS from https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/archive/master.zip
- Unpack the archive folders: https://www.screencast.com/t/jgucqIDo to ~phpPEARphpCodeSniffersrcStandards where ~ means path to PHP
- For Updates you need to download the WORDPRESS STANDARDS from the link above and copy them to CodeSniffer location.
- Install PHP. Suggested are bundle: XAMPP OR WAMP
- PHPStorm settings – is suggested to work on “default settings” dialog (from File)
- Make PHPStorm exe run as administrator. From compatibility properties on exe file.
- Make sure you have a valid PHP interpretor setup in PHPStorm: https://www.screencast.com/t/y6F79xUg60
- Adding PHPCodeSniffer to PHPStorm CodeSniffer
- Setup Composer path in PHPStorm: https://www.screencast.com/t/52VzidnWb
- Go to Langauages & Frameworks -> PHP -> Code Sniffer https://www.screencast.com/t/APUubpoN
- Using PEAR: Setup point 3 from screenshot by adding: ~phpphpcs.bat
Using Composer: Set point 3 from screenshot by adding: path_to_composer_globalvendorsquizlabsphp_codesnifferbinphpcs.bat - Click Validate. If no error you are good!
- Change PHP inspector to: https://www.screencast.com/t/ySgOPmaVxQ
- Make sure PHP Code Sniffer validator is checked
Reference:
https://github.com/squizlabs/PHP_CodeSniffer
https://w3guy.com/setup-php-code-sniffer-phpstorm-windows/
https://kellenmace.com/set-up-php-codesniffer-in-phpstorm-with-wordpress-coding-standards/