SublimeLinter:模块phpcs和phpmd无法正常工作

时间:2016-09-02 11:27:54

标签: sublimetext3 sublimelinter phpmd phpcs

Sublime Text 3我通过以下Package Manager软件包安装:

  • SublimeLinter
  • SublimeLinter-phpmd
  • SublimeLinter-phpcs
  • 和其他几个人..

问题是phpmdphpcs都没有效果。在ST3中打开php文件然后单击:ctrl和“我得到以下调试信息:

SublimeLinter: debug mode: on 
SublimeLinter: temp directory: c:\users\alekspav\appdata\local\temp\SublimeLinter3-alekspav 
SublimeLinter: annotations activated: <builtin> 
SublimeLinter: json activated: <builtin> 
SublimeLinter: WARNING: phpcs deactivated, cannot locate 'phpcs' 
SublimeLinter: annotations activated: <builtin> 
SublimeLinter: WARNING: jshint deactivated, cannot locate 'jshint' 
SublimeLinter: php activated: C:\xampp\php\php.exe 
SublimeLinter: WARNING: phpmd deactivated, cannot locate 'phpmd'
SublimeLinter: WARNING: htmltidy deactivated, cannot locate 'tidy' 
SublimeLinter: WARNING: csslint deactivated, cannot locate 'csslint' 
SublimeLinter: php: submitter.php ['C:\\xampp\\php\\php.exe', '-l', '-n', '-d', 'display_errors=On', '-d', 'log_errors=Off'] 
SublimeLinter: php output:
No syntax errors detected in - 

我特别感兴趣的是这两行:

  • SublimeLinter:警告:phpcs已停用,无法找到“ phpcs
  • SublimeLinter:警告:phpmd已停用,无法找到“ phpmd

我尝试解决问题的方法是编辑用户配置文件: Sublime text 3: Preferences -> package settings -> sublime linter-> settings - user

然后添加cmd密钥如下:

    "phpcs": {
        "@disable": false,
        "args": [],
        "cmd": "C:/xampp/htdocs/web/vendor/bin/phpcs/",
        "excludes": [],
        "standard": "PSR2"
    },
    "phpmd": {
        "@disable": false,
        "args": [],
        "cmd": "C:/xampp/htdocs/web/vendor/bin/phpmd/",
        "excludes": [],
        "rulesets": "cleancode,codesize,controversial,design,naming,unusedcode"
    }

正如您所猜测的那样 - 这并没有解决问题。我还尝试在没有/的情况下编写路径,并尝试使用\ delimeter而不是/。我还尝试直接指定PFAM文件..并且还尝试使用$ {project}变量而不是整个C:/ path。 ST3重启后我仍然收到警告。

我的另一个问题是 - 如何在调试窗口中输出“$ {project}”目录?因为我不确定它是否设置正确所以我想测试它。

更多信息:

Sublime项目目录是:C:\xampp\htdocs\web\test.sublime-project

以下是phpmdphpcs次安装:

  • “C:\ XAMPP \ htdocs中\网络\厂商\ BIN \ phpcs \ phpcbf.phar”
  • “C:\ XAMPP \ htdocs中\网络\厂商\ BIN \ phpcs \ phpcs.phar”
  • “C:\ XAMPP \ htdocs中\网络\厂商\ BIN \ phpmd \ phpmd.phar”

修改

好像我做错了。我已将composer.json添加到项目目录中,其中包含以下内容:

{
    "require-dev": {
        "squizlabs/php_codesniffer": "2.*",
        "phpmd/phpmd" : "@stable",
        "mongodb/mongodb" : "@dev"
    }
}

使用composer install命令安装完所有内容后,我为我创建了vendor文件夹,里面有很多目录,包括bin文件夹。它现在有所有必要的文件,但我仍然得到同样的错误。由于某种原因无法找到模块。

2 个答案:

答案 0 :(得分:2)

好,

我不确定这是一个错误还是一个功能,但我找到了问题的解决方案。

  1. 首先 - 您必须使用phpcs安装phpmdpear!没有这个 - 你会看到最初发布的警告。
  2.   

    phpcs: pear安装PHP_CodeSniffer

         

    phpmd:这里的说明:http://pear.phpmd.org/(我不记得另一个存储库的名称,但是在第一次安装过程中你会遇到一些lib缺失的错误,系统会提示你添加一个文件缺少phpmd pear安装依赖关系的存储库。一切都很简单)

    启动ST3并检查。不需要编辑配置。这两个模块应该在Sublime Text 3中工作!但我们现在使用pear模块而不是composer模块。我最初的想法是使用conmposer。保存和更新要容易得多。

    如果你不喜欢它 - 谷歌。在项目目录中,您应该有一个名为composer.json的文件,其内容类似于:

    {
        "require-dev": {
            "squizlabs/php_codesniffer":    "2.*",
            "phpmd/phpmd" :                 "@stable"
        }
    }
    

    然后使用带有Windows command prompt命令的composer install安装所有模块

    1. 正如in SublimeLinter的用户配置中的intiail post所述,请使用以下配置:

          "phpcs": {
              "@disable": false,
              "args": [],
              "cmd": "${project}/vendor/bin/phpcs.bat",
              "excludes": [],
              "standard": "${project}/vendor/squizlabs/php_codesniffer/CodeSniffer/Standards/PSR2/ruleset.xml"
          },
          "phpmd": {
              "@disable": false,
              "args": [],
              "cmd": "${project}/vendor/bin/phpmd.bat",
              "excludes": [],
              "rulesets": "cleancode,codesize,controversial,design,naming,unusedcode"
          }
      
    2. 现在重新启动Sublime test 3并打开调试并打开PHP文件。结果是:

      reloading plugin SublimeLinter-phpcs.linter
      SublimeLinter: phpcs linter loaded 
      reloading plugin SublimeLinter-phpmd.linter
      SublimeLinter: phpmd linter loaded 
      ...
      reloading settings Packages/User/SublimeLinter.sublime-settings
      SublimeLinter: phpcs activated: C:\xampp\php\phpcs.bat 
      SublimeLinter: phpmd activated: C:\xampp\php\phpmd.bat 
      ...
      SublimeLinter: phpcs: index.php ['C:/xampp/htdocs/web/vendor/bin/phpcs.bat', '--report=checkstyle', '--standard=C:/xampp/htdocs/web/vendor/squizlabs/php_codesniffer/CodeSniffer/Standards/PSR2/ruleset.xml'] 
      SublimeLinter: phpcs output:
      .. errors in PHP file listed
      Package Control: Skipping automatic upgrade, last run at 2016-09-05 17:12:20, next run at 2016-09-05 18:12:20 or after
      ...
      SublimeLinter: phpmd: index.php ['C:/xampp/htdocs/web/vendor/bin/phpmd.bat', '@', 'text', 'cleancode,codesize,controversial,design,naming,unusedcode'] 
      SublimeLinter: phpmd output:
      .. errors in PHP file listed
      

      如您所见 - SublimeLinter首先在pear目录中加载bat PHP!但是在提交PHP文件进行分析时,它正在项目目录中使用新的bat

      我的解释是SublimeLinter始终需要安装pear phpcsphpmd才能使用composer等效项。如果你问我,这是非常愚蠢的..但我找不到任何其他解决方案。

答案 1 :(得分:0)

I also had hard time trying to get sublimeLinter-phpmd work. here is how I fixed it:

Assumes:

  • you're on windows
  • you have sublime-text 3 and sublimeLinter installed

Steps:

  1. install pear:http://pear.php.net/manual/en/installation.getting.php
  2. important: add pear directory into $PATH
  3. install phpmd using pear under your pear directory: https://github.com/SublimeLinter/SublimeLinter-phpmd
  4. restart sublime-text 3.

For me, it's because I missed step 2 so that phpmd linter won't work.

There is no need to install per-project phpmd using composer. and there is no need to manually modify SublimeLinter's setting after installed SublimeLinter-phpmd.

May this help.