Skip to content

Instantly share code, notes, and snippets.

@saji89
Last active May 1, 2023 21:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saji89/1e8b4a75346d51775d45f78d938864df to your computer and use it in GitHub Desktop.
Save saji89/1e8b4a75346d51775d45f78d938864df to your computer and use it in GitHub Desktop.
Steps to setup PHP code sniffer package in Sublime text

Steps

  1. Install the package using Package Control - PhpCs(sublime-phpcs)

  2. Download the phar files needed for the functioning of the plugin:

curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar
curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcbf.phar
curl -OL http://static.phpmd.org/php/latest/phpmd.phar
curl -OL  http://cs.sensiolabs.org/download/php-cs-fixer-v2.phar
  1. Add executable permission to the phar files(applicable only in Mac and Linux): chmod +x php-cs-fixer-v2.phar phpcs.phar phpcbf.phar phpmd.phar

  2. Configuring the plugin. In the Sublime text menu:

Preferences > Package Settings > PHP Code Sniffer, you can find the Settings - Default and Settings - User files. The former is the template file, you can start by copying its content to Settings - User file. Once this is done, we now have to set paths for some executables to get the plugin working.

For basic functionality, I suggest that the following entries are updated:

  1. phpcs_execute_on_save - By default the value is false, change that to true

  2. In the following entries, specify the absolute path to the phar files you have download previously:

"phpcs_executable_path"
"php_cs_fixer_executable_path"
"phpcbf_executable_path"
"phpmd_executable_path"
  1. For the following entries, you need to specify the absolute path to the php executable. Usually needed only in Windows, but can be used in Linux/Mac if you're using a non-standard install, which may not be present in the PATH environment variable:
"phpcs_php_prefix_path"
"phpcs_php_path"

Example values from a MAC machine:

"phpcs_executable_path": "/Users/username/phpcs.phar"
"php_cs_fixer_executable_path": "/Users/username/php-cs-fixer-v2.phar"
"phpcbf_executable_path": "/Users/username/phpcbf.phar"
"phpmd_executable_path": "/Users/username/phpmd.phar"

"phpcs_php_prefix_path" : "/Applications/MAMP/bin/php/php7.1.1/bin/php"
"phpcs_php_path" : "/Applications/MAMP/bin/php/php7.1.1/bin/php"

Useful reference links:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment