phpstormphpcs

PhpStorm is throwing an error if running phpcs


First of all, let me list some versions so you know the setup.

OS: Windows 10
php: 7.2.7 NTS with xDebug 2.6.1 active
PhpStorm: 2016.2.2
PHP_CodeSniffer: version 3.4.0 (stable) by Squiz (http://www.squiz.net)
PEAR: 1.10.7

Now let me describe the problem:

The code sniffer was installed via pear. I'm using the following bat script for starting the sniffer.

@echo off

set folder=C:\Program Files\php
set phpcs=%folder%\phpcs

php "%phpcs%" %*

If I start the code sniffer via the PowerShell with the following command:

phpcs.bat index.php --standard=PSR2 --encoding=utf-8 --report=xml

I'm getting a valid output:

xml version="1.0" encoding="UTF-8"?>
<file name="C:\Users\simon\Documents\Repositories\mm-BIT\CatalogGenerator\index.php" errors="3" warnings="0" fixable="3">
    <error line="1" column="1" source="Generic.Files.LineEndings.InvalidEOLChar" severity="5" fixable="1">End of line character is invalid; expected &quot;\n&quot; but found &quot;\r\n&quot;</error>
    <error line="124" column="1" source="PSR2.Methods.FunctionCallSignature.SpaceBeforeOpenBracket" severity="5" fixable="1">Space before opening parenthesis of function call prohibited</error>
    <error line="129" column="1" source="PSR2.Methods.FunctionCallSignature.SpaceBeforeOpenBracket" severity="5" fixable="1">Space before opening parenthesis of function call prohibited</error>
</file>
</phpcs>

In PhpStorm the setting looks like this:

enter image description here

enter image description here

If I validate the installation PhpStorm tells me everything is fine:

enter image description here

The data for the coding standards on the Inspections page was loaded automatically, so this seems to work as well.

If PhpStorm is running the script I'm getting the following error:

PHP Code Sniffer
phpcs: xml version="1.0" encoding="UTF-8"?>

I was exposing some data via the script which is getting called:

PHP Code Sniffer

phpcs: C:/temp/___1.tmp/Core/DataContainers/Language.php --standard=PSR2 --encoding=utf-8 --report=xml
command: php "C:\Program Files\php\phpcs" C:/temp/___1.tmp/Core/DataContainers/Language.php --standard=PSR2 --encoding=utf-8 --report=xml
xml version="1.0" encoding="UTF-8"?>

I did check the temp folder for writing permissions and was checking if the file is created correctly on the path mentioned above. I did copy the folder as soon as it was created and did run the command line by hand in the PowerShell successfully.

php "C:\Program Files\php\phpcs"    
C:/temp/___.tmp/Core/DataContainers/Modules/SimpleTableModule.php -- 
standard=PSR2 --encoding=utf-8 --report=xml

which delivers the following output:

<?xml version="1.0" encoding="UTF-8"?>
<phpcs version="3.4.0">
xml version="1.0" encoding="UTF-8"?>
<file name="C:\temp\___.tmp\Core\DataContainers\Modules\SimpleTableModule.php" errors="1" warnings="1" fixable="1">
    <warning line="82" column="114" source="Generic.Files.LineLength.TooLong" severity="5" fixable="0">Line exceeds 120 characters; contains 123 characters</warning>
    <error line="106" column="1" source="PSR2.Files.EndFileNewline.NoneFound" severity="5" fixable="1">Expected 1 newline at end of file; 0 found</error>
</file>
</phpcs>

I don't know how to fix this if you could provide me with some ideas I would be really happy.


Solution

  • The problem was solved, I did replace the content of the phpcs.bat file with the official script:

    @echo off
    REM PHP_CodeSniffer detects violations of a defined coding standard.
    REM 
    REM @author    Greg Sherwood <gsherwood@squiz.net>
    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 "%PHP_PEAR_PHP_BIN%" neq "" (
        set PHPBIN=%PHP_PEAR_PHP_BIN%
    ) else set PHPBIN=php
    
    "%PHPBIN%" "%~dp0\phpcs" %*
    

    Script in the repository

    I was trying this before but it was not working, I was giving myself full access rights on the PHP installation folder as well. Seems like the problem is fixed now.

    Still a big thanks to everyone taking a look.

    Edit: I checked my bat file again and it was not completely identical with the one from the git repository. I did leave my old code as a comment in the file. After I cleaned this up this morning the sniffer wasn't working anymore, after I readded the comments again the feature was working again. So here is the complete content of the file in the current working state:

    @echo off
    REM PHP_CodeSniffer detects violations of a defined coding standard.
    REM 
    REM @author    Greg Sherwood <gsherwood@squiz.net>
    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 "%PHP_PEAR_PHP_BIN%" neq "" (
        set PHPBIN=%PHP_PEAR_PHP_BIN%
    ) else set PHPBIN=php
    
    "%PHPBIN%" "%~dp0\phpcs" %*
    REM End of file
    

    We did confirm this behavior on another computer with the same installed software, so this seems to be the problem.

    Edit2: Seems like you just need a comment line after the last line of the original script. I did update the code snippet I'm using at the moment.