Skip to content

Instantly share code, notes, and snippets.

@scrubmx
Last active June 27, 2021 14:14
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save scrubmx/daf57b50897e7962e398 to your computer and use it in GitHub Desktop.
Save scrubmx/daf57b50897e7962e398 to your computer and use it in GitHub Desktop.
Setup Phpcs for Sublime Text

Setup Phpcs for Sublime Text

This plugin adds PHP CodeSniffer, PHP Code Beautifier and Fixer, PHP Coding Standards Fixer, the PHP Linter, PHP Mess Detector, Scheck support to Sublime Text.

For more information about this plugin such as features, installation requirements etc, please see: http://benmatselby.github.io/sublime-phpcs

Prerequisites

  • Download and install composer
curl -sS https://getcomposer.org/installer | php 
sudo mv composer.phar /usr/local/bin/composer
  • Make sure to add ~/.composer/vendor/bin directory to your PATH

Instructions

First install Phpcs plugin for Sublime Text

  • Using Package Control:
Preferences > Package Control > Install Package > Phpcs 
  • Or Manually:
cd ~/Library/Application\ Support/Sublime\ Text\ 3/Packages/
git clone git://github.com/benmatselby/sublime-phpcs.git Phpcs

Install all of the plugin dependecies

  • Install both phpcs and phpcbf with:
sudo composer global require "squizlabs/php_codesniffer=*"
  • Install phpmd:
sudo composer global require "phpmd/phpmd=*"
  • Finally install php-cs-fixer
sudo composer global require "fabpot/php-cs-fixer=*"
  • Take note of the path for the newly installed apps:
which phpcs
which phpcbf
which phpmd
which php-cs-fixer

(This path is usually /Users/NAME/.composer/vendor/bin/ on a Mac)

Update your phpcs.sublime-settings file:

This file is under Preferences > Package Settings > PHP Code Sniffer > Settings User

{
	"phpcs_php_path": "/path/to/bin/phpcbf",
	"phpcs_executable_path": "/path/to/bin/phpcs",
	"phpmd_executable_path": "/path/to/bin/phpmd",
	"php_cs_fixer_executable_path": "/path/to/bin/php-cs-fixer",
	"phpcs_show_errors_on_save": false,
	"phpcs_outline_for_errors": false,
	"php_cs_fixer_on_save": false
}

Update your Default.sublime-keymap file:

This file is under Preferences > Key Bindings - User

[
    { 
        "keys": ["super+k", "super+f"], 
        "command": "phpcs_fix_this_file", 
        "args": { "tool": "Fixer" }
    }
]

You now can press "super+k", "super+f" to format your code to PSR-2

@smileBeda
Copy link

Do not run Composer as root/super user! See https://getcomposer.org/root for details

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