PS: This is an old article, it may not work anymore. I would suggest trying an 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 checks 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 returned a lot of errors. So I dug around a bit and finally got it working.
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 was 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 has 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 tool is really useful; now my PHP code is much tidier and more standardized.
