【问题标题】:PhpStorm is throwing an error if running phpcs如果运行 phpcs,PhpStorm 会抛出错误
【发布时间】:2019-02-26 08:52:54
【问题描述】:

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

操作系统: Windows 10
php: 7.2.7 NTS,xDebug 2.6.1 处于活动状态
PhpStorm: 2016.2。 2
PHP_CodeSniffer: 3.4.0 版(稳定版),由 Squiz (http://www.squiz.net)
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 告诉我一切都很好:

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

如果 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>

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

【问题讨论】:

    标签: phpstorm phpcs


    【解决方案1】:

    问题解决了,我确实用官方脚本替换了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 存储库中的不完全相同。我确实在文件中留下了旧代码作为注释。今天早上我清理完后,嗅探器不再工作,在我再次读取 cmets 后,该功能再次工作。所以这里是当前工作状态下文件的完整内容:

    @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: 似乎您只需要在原始脚本的最后一行之后添加一个注释行。我确实更新了我目前正在使用的代码 sn-p。

    【讨论】:

    • 在 GitHub 上的项目中添加了ticket
    猜你喜欢
    • 2015-11-19
    • 1970-01-01
    • 2023-01-25
    • 1970-01-01
    • 1970-01-01
    • 2019-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多