Use PHP Code Sniffer (phpcs) in Sublimetext 3

PS: This is an old article, it may not work anymore. I would suggest to try IDE such as VSCode or PHPStorm for better phpcs support.

Today i found a great plugin for Sublimetext 3, it is called sublime-phpcs on Sublimetext 3. This plugin is to check if my php code is good according to the PHP Standard (PSR-1 and PSR-2). After the  default installation, the plugin was not working and return a lot of errors. So i dig around a bit and finally it is working now.

Below are the requirements and how to install it (note that i’m on Ubuntu 14.04):

1. PHP Pear

1
apt-get install php-pear

2. PHP Code Sniffer (phpcs) Pear Package

1
2
3
4
pear install PHP_CodeSniffer
#after installation please check it with this code
which phpcs
# mine return /usr/bin/phpcs

3. PHP Depends Pear Package

1
2
pear channel-discover pear.pdepend.org
pear install pdepend/PHP_Depend

4. PHP Mess Detector (phpmd) Pear Package

1
2
3
4
5
pear channel-discover pear.phpmd.org
pear install phpmd
#after installation please check it with this code
which phpmd
# mine return /usr/bin/phpmd

5. Install PHP CS-Fixer

1
2
curl http://get.sensiolabs.org/php-cs-fixer.phar -o /usr/local/bin/php-cs-fixer
chmod a+x /usr/local/bin/php-cs-fixer

6. Install ocaml language interpreter

1
apt-get install ocaml

7. Install zlib.h libraries. Somehow my zlib library has broken. You can skip this step.

1
apt-get install zlib1g-dev

8. Install pfff

1
2
3
4
5
6
cd /opt/
git clone --depth=1 https://github.com/facebook/pfff.git
./configure
make depend
make
make opt

9. Install sublime-phpcs from Sublime Package Control. And configure it through Preferences > Package Settings > PHP Code Sniffer > Settings – User, and use the configuration below:

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
{
    // *nix based systems, Mac OS X and Linux
    // - All commands on and using PATHS
 
    // We want debugging on
    "show_debug": true,
 
    // Only execute for .php files
    "extensions_to_execute": ["php"],
 
    // Do not execute for twig files
    "extensions_to_blacklist": ["twig.php"],
 
    // Execute the sniffer on file save
    "phpcs_execute_on_save": false,
 
    // Show the error list after save.
    "phpcs_show_errors_on_save": true,
 
    // Show the errors in the gutter
    "phpcs_show_gutter_marks": true,
 
    // Show outline for errors
    "phpcs_outline_for_errors": true,
 
    // Show the errors in the status bar
    "phpcs_show_errors_in_status": true,
 
    // Show the errors in the quick panel so you can then goto line
    "phpcs_show_quick_panel": true,
 
    // PHP_CodeSniffer settings
    // Run the PHP_CodeSniffer
    "phpcs_sniffer_run": true,
 
    // Execute PHP_CodeSniffer on save
    "phpcs_command_on_save": true,
 
    // Path to phpcs
    "phpcs_executable_path": "/usr/bin/phpcs",
 
    // Run the PEAR standard without warnings
    "phpcs_additional_args": {
        "--standard": "PEAR",
        "-n": ""
    },
 
    // PHP-CS-Fixer settings
    // Do not automatically fix the errors
    "php_cs_fixer_on_save": false,
 
    // Show the fixes in the quick panel
    "php_cs_fixer_show_quick_panel": true,
 
    // Path to where php-cs-fixer.phar is
    "php_cs_fixer_executable_path": "/usr/local/bin/php-cs-fixer",
 
    // Run all levels of fixing
    "php_cs_fixer_additional_args": {
        "--level": "all"
    },
 
    // PHP Linter settings
    // Lint each file
    "phpcs_linter_run": true,
 
    // Execute the linter on save
    "phpcs_linter_command_on_save": true,
 
    // Use the $PATH version of php
    "phpcs_php_path": "",
 
    // Regex for the errors the linter produces
    "phpcs_linter_regex": "(?P<message>.*) on line (?P<line>\\d+)",
 
    // PHP Mess Detector settings
    // Execute phpmd
    "phpmd_run": true,
 
    // Execute the phpmd on file save
    "phpmd_command_on_save": true,
 
    // Path to where the phpmd application is
    "phpmd_executable_path": "/usr/bin/phpmd",
 
    // What args I want to pass to phpmd
    "phpmd_additional_args": {
        "codesize,unusedcode,naming": ""
    },
 
    // PHP Scheck settings
    // Execute scheck
    "scheck_run": true,
 
    // Execute the scheck on file save
    "scheck_command_on_save": false,
 
    // It seems python/sublime cannot always find the scheck application
    // If empty, then use PATH version of scheck, else use the set value
    "scheck_executable_path": "/opt/pfff/scheck",
 
    // Additional arguments you can specify into the application
    "scheck_additional_args": {
        "-strict" : ""
    }
}

Save the file and you can use the PHP Code Sniffer in Sublimetext 3 through Right Click > PHP Code Sniffer > Sniff this file.

If it still no response see the output console using CTRL+~` and paste the error in the comment box below. I’ll try to help you.

And i found this tools is really useful, now i my php code is much tidy and standardized.

Comments

  1. lyhong says:

    I got an error “No releases available for package “pear.php.net/phpmd”
    install failed” when I installed phpmd by using command pear install phpmd

  2. Vinz says:

    pear remote-list -c phpmd
    pear install phpmd/package_name

  3. sanket says:

    startup, version: 3083 linux x64 channel: stable
    executable: /opt/sublime_text/sublime_text
    working dir: /
    packages path: /home/sanket/.config/sublime-text-3/Packages
    state path: /home/sanket/.config/sublime-text-3/Local
    zip path: /opt/sublime_text/Packages
    zip path: /home/sanket/.config/sublime-text-3/Installed Packages
    ignored_packages: [“Vintage”]
    pre session restore time: 0.116635
    startup time: 0.232684
    first paint time: 0.308568
    reloading plugin Default.block
    reloading plugin Default.comment
    reloading plugin Default.copy_path
    reloading plugin Default.delete_word
    reloading plugin Default.detect_indentation
    reloading plugin Default.duplicate_line
    reloading plugin Default.echo
    reloading plugin Default.exec
    reloading plugin Default.fold
    reloading plugin Default.font
    reloading plugin Default.goto_line
    reloading plugin Default.history_list
    reloading plugin Default.indentation
    reloading plugin Default.kill_ring
    reloading plugin Default.mark
    reloading plugin Default.new_templates
    reloading plugin Default.open_context_url
    reloading plugin Default.open_file_settings
    reloading plugin Default.open_in_browser
    reloading plugin Default.pane
    reloading plugin Default.paragraph
    reloading plugin Default.paste_from_history
    reloading plugin Default.quick_panel
    reloading plugin Default.save_on_focus_lost
    reloading plugin Default.scroll
    reloading plugin Default.set_unsaved_view_name
    reloading plugin Default.side_bar
    reloading plugin Default.sort
    reloading plugin Default.swap_line
    reloading plugin Default.switch_file
    reloading plugin Default.symbol
    reloading plugin Default.transform
    reloading plugin Default.transpose
    reloading plugin Default.trim_trailing_white_space
    reloading plugin CSS.css_completions
    reloading plugin Diff.diff
    reloading plugin HTML.encode_html_entities
    reloading plugin HTML.html_completions
    reloading plugin 0_package_control_loader.00-package_control
    reloading plugin 0_package_control_loader.01-ssl-linux
    Linux SSL: successfully loaded _ssl module for libssl.so.1.0.0
    reloading plugin 0_package_control_loader.02-bz2
    reloading plugin Package Control.Package Control
    reloading plugin Package Control.bootstrap
    reloading plugin Phpcs.phpcs
    plugins loaded
    Package Control: Skipping automatic upgrade, last run at 2015-04-06 13:50:18, next run at 2015-04-06 14:50:18 or after
    [Phpcs] php -l -d display_errors=On /var/www/html/localbanya/application/libraries/MY_Form_validation.php
    [Phpcs] php -l -d display_errors=On /var/www/html/localbanya/application/libraries/MY_Form_validation.php
    [Phpcs] cwd: /home/sanket
    [Phpcs] No syntax errors detected in /var/www/html/localbanya/application/libraries/MY_Form_validation.php

    [Phpcs] /usr/bin/phpcs –report=checkstyle –standard=PEAR -n /var/www/html/localbanya/application/libraries/MY_Form_validation.php
    [Phpcs] /usr/bin/phpcs –report=checkstyle –standard=PEAR -n /var/www/html/localbanya/application/libraries/MY_Form_validation.php
    [Phpcs] cwd: /home/sanket
    Traceback (most recent call last):
    File “/opt/sublime_text/sublime_plugin.py”, line 556, in run_
    return self.run(edit)
    File “phpcs in /home/sanket/.config/sublime-text-3/Installed Packages/Phpcs.sublime-package”, line 647, in run
    File “phpcs in /home/sanket/.config/sublime-text-3/Installed Packages/Phpcs.sublime-package”, line 465, in run
    File “phpcs in /home/sanket/.config/sublime-text-3/Installed Packages/Phpcs.sublime-package”, line 143, in get_errors
    File “phpcs in /home/sanket/.config/sublime-text-3/Installed Packages/Phpcs.sublime-package”, line 219, in execute
    File “phpcs in /home/sanket/.config/sublime-text-3/Installed Packages/Phpcs.sublime-package”, line 222, in parse_report
    File “phpcs in /home/sanket/.config/sublime-text-3/Installed Packages/Phpcs.sublime-package”, line 173, in shell_out
    File “./subprocess.py”, line 824, in __init__
    File “./subprocess.py”, line 1448, in _execute_child
    FileNotFoundError: [Errno 2] No such file or directory: ‘/usr/bin/phpcs’

  4. Juan M. says:

    Hi, great post!

    I found some possible mistakes:
    – In step 4, line 4: which phpcs must be which phpmd
    – In step 8, line 3, before execute ./configure, must execute cd pfff

  5. please tell me how to set the path in windows 7 . . my user setting file code is this –

    {
    // Example for:
    // – Windows 7
    // – With phpcs and php-cs-fixer support

    // We want debugging on
    “show_debug”: true,

    // Only execute the plugin for php files
    “extensions_to_execute”: [“php”],

    // Do not execute for twig files
    “extensions_to_blacklist”: [“twig.php”],

    // Execute the sniffer on file save
    “phpcs_execute_on_save”: true,

    // Show the error list after save.
    “phpcs_show_errors_on_save”: true,

    // Show the errors in the gutter
    “phpcs_show_gutter_marks”: true,

    // Show outline for errors
    “phpcs_outline_for_errors”: true,

    // Show the errors in the status bar
    “phpcs_show_errors_in_status”: true,

    // Show the errors in the quick panel so you can then goto line
    “phpcs_show_quick_panel”: true,

    // Path to php on windows installation
    // This is needed as we cannot run phars on windows, so we run it through php
    “phpcs_php_prefix_path”: “C:/xampp/php/”,

    // We want the fixer to be run through the php application
    “phpcs_commands_to_php_prefix”: [ ],

    // PHP_CodeSniffer settings
    // Yes, run the phpcs command
    “phpcs_sniffer_run”: true,

    // And execute it on save
    “phpcs_command_on_save”: true,

    // This is the path to the bat file when we installed PHP_CodeSniffer
    “phpcs_executable_path”: “C:/xampp/php/pear/phpcs.bat”,

    // I want to run the PSR2 standard, and ignore warnings
    “phpcs_additional_args”: {
    “–standard”: “PSR2”,
    “-n”: “”
    },

    // PHP-CS-Fixer settings
    // Don’t want to auto fix issue with php-cs-fixer
    “php_cs_fixer_on_save”: false,

    // Show the quick panel
    “php_cs_fixer_show_quick_panel”: true,

    // The fixer phar file is stored here:
    “php_cs_fixer_executable_path”: “C:/xampp/php/pear/php-cs-fixer.phar”,

    // Additional arguments, run all levels of fixing
    “php_cs_fixer_additional_args”: {
    },

    // PHP Linter settings
    // Yes, lets lint the files
    “phpcs_linter_run”: true,

    // And execute that on each file when saved (php only as per extensions_to_execute)
    “phpcs_linter_command_on_save”: true,

    // Path to php
    “phpcs_php_path”: “C:/xampp/php/”,

    // This is the regex format of the errors
    “phpcs_linter_regex”: “(?P.*) on line (?P\\d+)”,

    // PHP Mess Detector settings
    // Not turning on the mess detector here
    “phpmd_run”: false,
    “phpmd_command_on_save”: false,
    “phpmd_executable_path”: “”,
    “phpmd_additional_args”: {}
    }

  6. Robby says:

    the code sniffer still no response, me using windows

    Traceback (most recent call last):
    File “C:\Program Files\Sublime Text 3\sublime_plugin.py”, line 556, in run_
    return self.run(edit)
    File “phpcs in C:\Users\IT-01\AppData\Roaming\Sublime Text 3\Installed Packages\Phpcs.sublime-package”, line 649, in run
    File “phpcs in C:\Users\IT-01\AppData\Roaming\Sublime Text 3\Installed Packages\Phpcs.sublime-package”, line 467, in run
    File “phpcs in C:\Users\IT-01\AppData\Roaming\Sublime Text 3\Installed Packages\Phpcs.sublime-package”, line 143, in get_errors
    File “phpcs in C:\Users\IT-01\AppData\Roaming\Sublime Text 3\Installed Packages\Phpcs.sublime-package”, line 219, in execute
    File “phpcs in C:\Users\IT-01\AppData\Roaming\Sublime Text 3\Installed Packages\Phpcs.sublime-package”, line 222, in parse_report
    File “phpcs in C:\Users\IT-01\AppData\Roaming\Sublime Text 3\Installed Packages\Phpcs.sublime-package”, line 173, in shell_out
    File “./subprocess.py”, line 824, in __init__
    File “./subprocess.py”, line 1118, in _execute_child
    FileNotFoundError: [WinError 2] The system cannot find the file specified

    hope you can help

  7. lim69 says:

    How to add my own code style – ruleset.xml ?

  8. thanks a lot for your help.
    do you know how could I add my standard?
    https://github.com/PrestaShop/PrestaShop-norm-validator

    • Change the part of this line:
      // Run the PEAR standard without warnings
      “phpcs_additional_args”: {
      “–standard”: “PEAR”,
      “-n”: “”
      },

      to

      // Run the PEAR standard without warnings
      “phpcs_additional_args”: {
      “–standard”: “PSR2 PrestaShop/classes/”,
      “-n”: “”
      },

  9. [Phpcs] Project files:
    [Phpcs] Current: None
    [Phpcs] Last Known: None
    [Phpcs] No project file defined, therefore skipping reload

  10. Yu says:

    Hi, thanks for the post. But I have one question:
    After using php-cs-fixer in sublime, all the tabs changed to blank. How can I change it back?

  11. Traceback (most recent call last):
    File “C:\Program Files (x86)\Sublime Text 3\sublime_plugin.py”, line 1088, in run_
    return self.run(edit)
    File “C:\Users\ShoaibMunir\AppData\Roaming\Sublime Text 3\Installed Packages\Phpcs.sublime-package\phpcs.py”, line 661, in run
    File “C:\Users\ShoaibMunir\AppData\Roaming\Sublime Text 3\Installed Packages\Phpcs.sublime-package\phpcs.py”, line 479, in run
    File “C:\Users\ShoaibMunir\AppData\Roaming\Sublime Text 3\Installed Packages\Phpcs.sublime-package\phpcs.py”, line 149, in get_errors
    File “C:\Users\ShoaibMunir\AppData\Roaming\Sublime Text 3\Installed Packages\Phpcs.sublime-package\phpcs.py”, line 231, in execute
    File “C:\Users\ShoaibMunir\AppData\Roaming\Sublime Text 3\Installed Packages\Phpcs.sublime-package\phpcs.py”, line 234, in parse_report
    File “C:\Users\ShoaibMunir\AppData\Roaming\Sublime Text 3\Installed Packages\Phpcs.sublime-package\phpcs.py”, line 176, in shell_out
    File “./python3.3/subprocess.py”, line 819, in __init__
    File “./python3.3/subprocess.py”, line 1110, in _execute_child
    FileNotFoundError: [WinError 2] The system cannot find the file specified

  12. Shekhar Suman says:

    Hi Ivan,
    I am getting below in my console. I am want to use sublime phpcs in magento2 project. I have installed phpcs,php-cs-fixer,phpmd,phpcbf in my linux system. Install package phpcs in sublime.

    SublimeLinter: WARNING: phpcs output:
    PHP Fatal error: Uncaught Error: Class ‘PHP_CodeSniffer\Runner’ not found in /var/www/html/magento/vendor/bin/phpcs:17
    Stack trace:
    #0 {main}
    thrown in /var/www/html/ecomm_dev_ee/vendor/bin/phpcs on line 17

Give me your feedback

This site uses Akismet to reduce spam. Learn how your comment data is processed.