PhpStorm Can not run PHP Code CodeSniffer

Ted Logan picture Ted Logan · Aug 9, 2018 · Viewed 7.5k times · Source

Im trying to use CodeSniffer in PhpStorm.

In settings->PHP->CLI Interpreter I linked to the php.exe Im using https://windows.php.net/ but also tried it with Cygwin and XAMPP.

PhpStorm shows me the right PHP Version 7.2.5 and the php.ini

In the CodeSniffer configuration I selected the phpcs.bat When Im clicking on Validate I just got

Can not run PHP Code

Also added everything to the PATH

Did I miss something?

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

Answer

IVO GELOV picture IVO GELOV · Aug 9, 2018

You should put phpcs.bat and phpcs in your PHP folder - e.g. d:\program\php\phpcs. The CodeSniffer itself should reside in d:\program\php\PEAR\PHP\CodeSniffer - there will be a script autoload.php and a subfolder src.

Then in Settings -> Languages & Frameworks -> PHP -> Code Sniffer you specify the path to phpcs.bat and Validate it.

Then in Settings -> Editor -> Inspections you find the node PHP Code Sniffer validation and enable it. After enabling it you will be able to configure it - specifically choose the coding standard.

enter image description here enter image description here

This is my phpcs

#!D:\PROGRAM\Inet\Design\php\php.exe
<?php
/**
 * PHP_CodeSniffer detects violations of a defined coding standard.
 *
 * @author    Greg Sherwood <[email protected]>
 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
 */

if (is_file(__DIR__.'/../autoload.php') === true) {
    include_once __DIR__.'/../autoload.php';
} else {
    include_once 'PHP/CodeSniffer/autoload.php';
}

$runner   = new PHP_CodeSniffer\Runner();
$exitCode = $runner->runPHPCS();
exit($exitCode); 

This is my phpcs.bat

@echo off
REM PHP_CodeSniffer detects violations of a defined coding standard.
REM 
REM @author    Greg Sherwood <[email protected]>
REM @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
REM @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence

if "%PHPBIN%" == "" set PHPBIN=D:\PROGRAM\Inet\Design\php\php.exe
if not exist "%PHPBIN%" if "%PHP_PEAR_PHP_BIN%" neq "" goto USE_PEAR_PATH
GOTO RUN
:USE_PEAR_PATH
set PHPBIN=%PHP_PEAR_PHP_BIN%
:RUN
"%PHPBIN%" "D:\PROGRAM\Inet\Design\php\phpcs" %* 

This is my PEAR_ENV.reg which I have imported into Windows Registry

REGEDIT4
[HKEY_CURRENT_USER\Environment]
"PHP_PEAR_SYSCONF_DIR"="D:\\PROGRAM\\Inet\\Design\\php"
"PHP_PEAR_INSTALL_DIR"="D:\\PROGRAM\\Inet\\Design\\php\\pear"
"PHP_PEAR_DOC_DIR"="D:\\PROGRAM\\Inet\\Design\\php\\docs"
"PHP_PEAR_BIN_DIR"="D:\\PROGRAM\\Inet\\Design\\php"
"PHP_PEAR_DATA_DIR"="D:\\PROGRAM\\Inet\\Design\\php\\data"
"PHP_PEAR_PHP_BIN"="D:\\PROGRAM\\Inet\\Design\\php\\php.exe"
"PHP_PEAR_TEST_DIR"="Z:\\Temp\\"