3

in Sublime Text 3 I have installed via the Package Manager following packages:

  • SublimeLinter
  • SublimeLinter-phpmd
  • SublimeLinter-phpcs
  • and few others..

The problem is that neither phpmd or phpcs are working. When opening a php file in ST3 and then clicking: ctrl and ` I get following debug info:

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 - 

I am particularry interested in these 2 lines:

  • SublimeLinter: WARNING: phpcs deactivated, cannot locate 'phpcs'
  • SublimeLinter: WARNING: phpmd deactivated, cannot locate 'phpmd'

What I tried to do to fix the problem is to edit users config file: Sublime text 3: Preferences -> package settings -> sublime linter-> settings - user

And then added cmd key as following:

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

As you have guessed - this didn't resolve the issue. I also tried to write path without / at the end and also tried to use \ delimeter instead of /. And I also tried to specify PFAM file directly.. And also tried to use ${project} variable rather than whole C:/ path. I am still getting warnings after ST3 restart.

My other question would be - how to output "${project}" directory in debug window? Because I am not sure it is set correctly so I want to test it.

More info:

Sublime project directory is: C:\xampp\htdocs\web\test.sublime-project

Here are phpmd and phpcs installations:

  • "C:\xampp\htdocs\web\vendor\bin\phpcs\phpcbf.phar"
  • "C:\xampp\htdocs\web\vendor\bin\phpcs\phpcs.phar"
  • "C:\xampp\htdocs\web\vendor\bin\phpmd\phpmd.phar"

Edit:

Seems like I was doing it wrong. I have added a composer.json into project directory with following contents:

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

After installing everything with composer install command - I got vendor folder created for me with lots of directories inside, including the bin folder. It has all the necessary files now, but I am still getting the same error. Modules could not be found for some reason.

Alex
  • 4,607
  • 9
  • 61
  • 99

2 Answers2

2

Okay,

I am not sure if it's a bug or a feature, but I found the solution to the problem.

  1. First - you have to install phpcs and phpmd using the pear! Without this - you will see the warnings from initial post.

phpcs: pear install PHP_CodeSniffer

phpmd: instructions here: http://pear.phpmd.org/ (I do not remember the name of another repository but during the first installation you will encounter the error that some lib is missing and you will be prompted with a text to add one more repository which has missing dependencies for phpmd pear installation. All easy)

Start ST3 and check. No edits in configuration are necessary. Both modules should be working in Sublime Text 3! But we are now using the pear modules instead of composer modules. My initial idea was to use the conmposer. It is much easier to mantain and update.

In case you are not familliar with it - google. In project directory you should have a file called composer.json with the content similar to:

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

Then go and install all modules using the Windows command prompt with composer install command

  1. As adressed in intiail post - in user config of SublimeLinter use the following config:

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

Now restart Sublime test 3 and turn on debugging and open PHP file. Result is:

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

As you can see - SublimeLinter first loads the pear bats in PHP directory! But when submitting the PHP file for analysis - it is using the new bats in project directory.

My explanation is that SublimeLinter always needs pear phpcs and phpmd to be installed before using composer equivalents. This is quite stupid, if you ask me.. but I couldn't find any other solution.

Alex
  • 4,607
  • 9
  • 61
  • 99
  • well, I think the issue will be resolved if you just add `BIN` folder path into the `PATH` environment variable.. The idea is that `SublimeLinter` is not looking for the "cmd" parameter during the first initiation. It fails to find the `bat` files in `PHP` directory because without `pear` installation of `phpcs` and `phpmd` - there will be no such files. So If you tell him "hey, look into this folder" - it should identify files in `BIN` folder instead. Anyway, I am happy with my first aproach and don't want to break anything :) – Alex Sep 05 '16 at 15:09
  • It's strange that I can't edit my own post.. Anyway, I was thinking that the issue can also be resolved by creating a symbolic link to `PHP` folder as well. I mean the `shortcut` of bat to `PHP` directory.. But not tested, of course. – Alex Sep 05 '16 at 15:15
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.

unifreak
  • 773
  • 6
  • 17