Skip to content

Instantly share code, notes, and snippets.

@tomasdelvechio
Forked from scrubmx/sublime-phpcs.md
Created August 6, 2020 02:09
Show Gist options
  • Save tomasdelvechio/c6425f93657ca365665e80dc018f7a50 to your computer and use it in GitHub Desktop.
Save tomasdelvechio/c6425f93657ca365665e80dc018f7a50 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

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