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
apt-get install php-pear
2. PHP Code Sniffer (phpcs) Pear Package
pear install PHP_CodeSniffer
#after installation please check it with this code
which phpcs
# mine return /usr/bin/phpcs
3. PHP Depends Pear Package
pear channel-discover pear.pdepend.org
pear install pdepend/PHP_Depend
4. PHP Mess Detector (phpmd) Pear Package
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
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
apt-get install ocaml
7. Install zlib.h libraries. Somehow my zlib library has broken. You can skip this step.
apt-get install zlib1g-dev
8. Install pfff
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:
{
// *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.
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
try using pear install phpmd/PHP_PMD
try: pear install phpmd/PHP_PMD-1.5.0
pear remote-list -c phpmd
pear install phpmd/package_name
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’
Have you run the Step 1 of this article? looks like your phpcs is not properly installed.
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
Thanks Juan. I fixed it.
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”: {}
}
Wondering what is the issue?
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
How to add my own code style – ruleset.xml ?
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”: “”
},
[Phpcs] Project files:
[Phpcs] Current: None
[Phpcs] Last Known: None
[Phpcs] No project file defined, therefore skipping reload
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?
This is an old article, might not work anymore. I suggest to use phpcs and phpcsbf command line to fix it.
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
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
This is an old article, you can use composer to install phpcs and it will work better.