Compliance with WordPress coding standards is often required when working on a project in a group. In addition, the code, executed according to standards, increases the applicant's assessment in the eyes of a potential employer.

The standards themselves are described in Codex, but connecting them to the Code Sniffer in phpStorm causes certain difficulties. Connection requires the installation and configuration of several packages, instructions for which are scattered across different sites.

What is the complete sequence of steps to install WordPress coding standards for phpStorm under Windows?

    1 answer 1

    Code analyzers such as PHP Code Sniffer and coding standards such as WordPress Coding Standards allow you to create modern, easily updated code, avoid a lot of errors at the stage of writing code, and are absolutely indispensable for team work on a project. These tools are built into phpStorm - a powerful modern development environment for php.

    At the same time, many developers have difficulty installing the PHP Code Sniffer code analyzer and WordPress Coding Standards code design rules for it in PhpStorm under Windows . The following summarizes the installation information gathered from various sources.

    The procedure for connecting coding standards for Windows is as follows.

    PHP installation

    Open the page http://windows.php.net/download/ and select a 32- bit or 64-bit file depending on the bit architecture of your system.

    Still there are options for TS (Thread Safe) and NTS (Non Thread Safe). TS is different because it allows PHP to work in multiple threads. For simple applications and interaction with the web server, NTS is also quite suitable.

    Unzip the contents of the zip archive, for example, in c:\php .

    Rename the file c:\php\php.ini-development to c:\php\php.ini .

    Find the line ;On windows: and uncomment after it

     extension_dir = "ext" 

    To work with WordPress will need several extensions.

    Find the section ; Windows Extensions ; Windows Extensions and uncomment the lines

     extension=php_mbstring.dll extension=php_mysqli.dll 

    Find the line ;date.timezone = , uncomment and set your time zone, for example:

     date.timezone = 'Europe/Moscow' 

    Finally, add lines for the Xdebug debugger to the very end.

     [xdebug] zend_extension=c:/php/ext/php_xdebug-2.5.5-7.1-vc14-x86_64.dll xdebug.remote_autostart=on xdebug.remote_enable=on xdebug.remote_handler="dbgp" xdebug.remote_host="localhost" xdebug.remote_port=9001 xdebug.remote_mode=req xdebug.idekey="ваш_ΠΏΠ°Ρ€ΠΎΠ»ΡŒ" 

    The debugger itself as a dll file should be downloaded from here: https://xdebug.org/download.php and placed in c:\php\ext . When downloading, select the same PHP version and bit depth that you chose when installing PHP.

    PEAR installation

    Save the page https://pear.php.net/go-pear.phar as a file c:\php\go-pear.phar .

    Run cmd.exe and enter the commands

     cd c:\php php go-pear.phar 

    Answer the questions by accepting the defaults by pressing Enter .

    Add paths to the system PATH by double-clicking on the file c:\php\PEAR_ENV.reg created during the installation process.

    Installing PHP Code Sniffer

    The pear.bat file appeared in the c:\php pear.bat , with which you need to install PHP Code Sniffer.

    The latest version of PHP Code Sniffer is https://pear.php.net/package/PHP_CodeSniffer/download .

    In cmd.exe run the command specified on the page with the latest version of PHP Code Sniffer. At the moment it is:

     pear install PHP_CodeSniffer-3.1.1 

    New files have appeared in the c:\php\phpcs.bat folder, including c:\php\phpcs.bat .

    Composer installation

    For further installation, you will need Composer .

    Download the https://getcomposer.org/Composer-Setup.exe file in the c:\php folder.

    Run the Composer-Setup.exe and perform the default installation.

    To activate the Composer paths, restart the command window.

    Composer is added, now you can use it to install other packages.

    Installing PHP Code Sniffer in PhpStorm

    Open project settings window in PhpStorm ( Ctrl+Alt+S ) and select Languages ​​& Frameworks β†’ PHP .

    Set PHP language level (7.1) .

    Click on the dot to the right of the CLI Interpreter .

    In the CLI Interpreters window, select the PHP executable ( C:\php\php.exe ) C:\php\php.exe and the Debugger extension ( C:\php\ext\php_xdebug-2.5.5-7.1-vc14-x86_64.dll ) C:\php\ext\php_xdebug-2.5.5-7.1-vc14-x86_64.dll .

    enter image description here

    Click OK .

    In the same installation window, select Languages ​​& Frameworks β†’ PHP β†’ Code Sniffer .

    Set to it the path C:\php\phpcs.bat .

    enter image description here

    Click OK .

    In the same installation window, select Editor β†’ Inspections β†’ PHP β†’ PHP Code Sniffer validation and the required Coding standard . In the image below, PEAR is selected.

    enter image description here

    Click OK .

    Now PHP Code Sniffer is installed, you can use the menu command Code-Inspect Code .

    Install WordPress Coding Standards

    In the c:\php folder, run the command (Composer came in handy)

     composer create-project wp-coding-standards/wpcs --no-dev 

    The folder c:\php\wpcs .

    Adding WordPress Coding Standards to PhpStorm

    In the settings window, select Languages ​​& Frameworks β†’ PHP β†’ Code Sniffer and specify the path C:\php\wpcs\vendor\bin\phpcs.bat .

    You can click the Validate button next to check.

    Pay attention - now you have connected another Code Sniffer from the c:\php\wpcs .

    Click OK .

    In the same installation window, select Editor β†’ Inspections β†’ PHP β†’ PHP Code Sniffer validation and the required Coding standard. If you don’t see WordPress in the list of standards, click on the update button on the right (the ring arrows).

    enter image description here

    I advise you to use WordPress Extra - the most stringent coding standard, with the exception of VIP . But VIP is designed for special WordPress installations, and on normal sites it’s impossible to meet its requirements. Read more here: https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards

    This completes the installation process, and now you can create professional code for your WordPress sites.