如果 运行 phpcs,PhpStorm 会抛出错误

PhpStorm is throwing an error if running phpcs

首先,让我列出一些版本,以便您了解设置。

OS: Windows 10
php: 7.2.7 NTS with xDebug 2.6.1 active
PhpStorm: 2016.2.2
PHP_CodeSniffer: Squiz (http://www.squiz.net)
版本 3.4.0(稳定) PEAR: 1.10.7

现在让我描述一下问题:

代码嗅探器是通过 pear 安装的。我正在使用以下 bat 脚本来启动嗅探器。

@echo off

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

php "%phpcs%" %*

如果我使用以下命令通过 PowerShell 启动代码嗅探器:

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

我得到了一个有效的输出:

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>

在 PHPStorm 中,设置如下所示:

如果我验证安装,PhpStorm 告诉我一切正常:

检查页面上的编码标准数据是自动加载的,所以这似乎也有效。

如果 PhpStorm 运行正在运行脚本,我会收到以下错误:

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

我正在通过被调用的脚本公开一些数据:

PHP代码嗅探器

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"?>

我确实检查了临时文件夹的写入权限,并检查文件是否在上述路径上正确创建。我确实在文件夹创建后立即复制了它,并成功地在 PowerShell 中手动执行了 运行 命令行。

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

wich 提供以下输出:

<?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>

我不知道如何解决这个问题,如果你能给我一些想法,我会很高兴。

问题解决了,我确实用官方脚本替换了phpcs.bat文件的内容:

@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

我之前尝试过,但没有用,我也给了自己对 PHP 安装文件夹的完全访问权限。看来问题现在已经解决了。

还是非常感谢大家的观看

编辑: 我再次检查了我的 bat 文件,它与 git 存储库中的文件并不完全相同。我确实将我的旧代码作为注释留在了文件中。今天早上我清理完后,嗅探器不再工作了,在我再次阅读评论后,该功能又开始工作了。所以这里是当前工作状态下文件的完整内容:

@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

我们确实在另一台安装了相同软件的计算机上确认了此行为,所以这似乎是问题所在。

Edit2: 看来您只需要在原始脚本的最后一行之后添加注释行。我确实更新了我目前正在使用的代码片段。