ctrl+shift+p filters: :st2 :st3 :win :osx :linux
Browse

Phpcs

by benmatselby ALL Top 100

🔍 PHP CodeSniffer, PHP Coding Standard Fixer, Linter, and Mess Detector Support for Sublime Text

Details

Installs

  • Total 428K
  • Win 266K
  • Mac 80K
  • Linux 81K
Dec 30 Dec 29 Dec 28 Dec 27 Dec 26 Dec 25 Dec 24 Dec 23 Dec 22 Dec 21 Dec 20 Dec 19 Dec 18 Dec 17 Dec 16 Dec 15 Dec 14 Dec 13 Dec 12 Dec 11 Dec 10 Dec 9 Dec 8 Dec 7 Dec 6 Dec 5 Dec 4 Dec 3 Dec 2 Dec 1 Nov 30 Nov 29 Nov 28 Nov 27 Nov 26 Nov 25 Nov 24 Nov 23 Nov 22 Nov 21 Nov 20 Nov 19 Nov 18 Nov 17 Nov 16 Nov 15
Windows 3 4 7 1 5 7 6 10 5 7 5 11 4 8 5 4 6 5 4 2 7 12 3 6 5 4 3 3 8 3 6 6 7 0 0 0 1 4 3 12 4 0 3 5 9 7
Mac 0 0 2 2 0 0 2 3 3 1 2 1 1 2 1 0 1 1 0 2 0 1 1 1 0 0 0 6 3 0 1 2 2 0 0 0 0 1 0 0 0 1 2 1 4 4
Linux 1 1 1 2 1 1 3 0 2 3 2 1 1 3 7 2 0 0 1 4 2 2 0 3 2 3 5 2 1 0 1 0 0 0 0 0 0 0 2 5 0 1 0 5 2 2

Readme

Source
raw.​githubusercontent.​com

sublime-phpcs

This is a plugin for Sublime Text which provides checkstyle reports using the following tools (all optional):

  • PHP_CodeSniffer (phpcs)
  • Linter (php -l)
  • PHP Mess Detector (phpmd)

You can also configure the plugin to fix the issues using either

  • PHP Coding Standards Fixer (php-cs-fixer)
  • PHP Code Beautifier (phpcbf) application

Requirements

Requirements for this plugin, should you want all the options to work:

This plugin has been tested on:

  • Mac OS X 10.8.2
  • Ubuntu 11.10
  • Windows 7
  • Sublime Text 2
  • Sublime Text 3
  • Sublime Text 4

It may work with other versions, but we cannot confirm that.

Installation

Use Sublime Text’s Package Control (Tools -> Command Palette -> Package Control: Install Package -> Phpcs) to install this plugin. This is the recommended installation path.

Or

Simply checkout the git repo into “~/Library/Application Support/Sublime Text/Packages/ or the equivalent folder on Windows or Linux.

cd ~/Library/Application\ Support/Sublime\ Text/Packages/
git clone git://github.com/benmatselby/sublime-phpcs.git Phpcs

In both cases, you may need to then configure the following with the actual path to the application:

  • “phpcs_php_path”
  • “phpcs_executable_path”
  • “phpmd_executable_path”
  • “php_cs_fixer_executable_path”

They are optional for the plugin. The path needs to include the application such as /usr/local/bin/phpcs.

In order to get the path of the application (On a Mac/Linux based environment), you can use:

which phpcs
which phpmd
which php-cs-fixer
which phpcbf

Features

  • Ability to run PHP_CodeSniffer
  • Ability to run php -l on the open file
  • Ability to run PHP Mess Detector on the open file
  • Show cached results from PHP_CodeSniffer in open file
  • Show errors in the Quick Panel
  • Show errors in the Gutter
  • Highlight the errors in the editor
  • Show the error for a given line in the status bar
  • Ability to specify the regular expression of the linter errors
  • Ability to specify the location of the PHP_CodeSniffer application
  • Ability to specify the location of the PHP Mess Detector application
  • Ability to run the PHP Coding Standards Fixer tool which fixes most issues in your code when you want to follow the PHP coding standards as defined in the PSR-1 and PSR-2 documents
  • Ability to run the PHP Code Beautifier tool which fixes most issues in your code when you want to follow PHP coding standards

Once you have right clicked on a file and selected “PHP CodeSniffer” > “Sniff this file…” you will get the output as shown below (depending on the settings you have defined):

Configuration

You can also define the configuration for the following settings, be it for a project, user settings or the default settings:

Plugin

  • show_debug – Do you want the debug information to be sent to the console?
  • extensions_to_execute – Which filetypes do you want the plugin to execute for?
  • extensions_to_blacklist – Override the extensions_to_execute in case you have a sub extension such as twig.php etc.
  • phpcs_execute_on_save – Do you want the code sniffer plugin to run on file save for php files?
  • phpcs_show_errors_on_save – Do you want the errors to be displayed in quick_panel on save?
  • phpcs_show_gutter_marks – Do you want the errors to be displayed in the gutter?
  • phpcs_outline_for_errors – Do you want the errors to be highlighted in the editor?
  • phpcs_show_errors_in_status – Do you want the errors to be displayed in status bar when clicking on the line with error?
  • phpcs_show_quick_panel – Do you want the errors to be displayed in the quick panel?
  • phpcs_php_prefix_path – Needed on windows for phar based applications. Also if you cannot make phar executable. Avoid if possible
  • phpcs_commands_to_php_prefix – List of commands you want the php path to prefix. This would be useful, if you have some commands as a phar that cannot be run without the php prefix, and others using native command.
  • phpcs_icon_scope_color - What colour to stylise the icon. This needs knowledge of theming of Sublime Test, as it uses scope colours from the theme to “tint” the dot icon. See here

PHP_CodeSniffer

  • phpcs_sniffer_run – Do you want the PHPCS checker to run?
  • phpcs_command_on_save – Do you want the command to execute on save?
  • phpcs_executable_path – The path to the phpcs executable. If empty string, use PATH to find it
  • phpcs_additional_args – This is the extra information you want to pass to the phpcs command. For example which “standard” you want to run, and if you want to show warnings or not

PHP CodeSniffer Fixer

  • php_cs_fixer_on_save – Do you want to run the fixer on file save?
  • php_cs_fixer_show_quick_panel – Do you want the quick panel to display on execution?
  • php_cs_fixer_executable_path – The path to the php-cs-fixer application.
  • php_cs_fixer_additional_args – This is the extra information you want to pass to the php-cs-fixer command. For example which “fixers” you want to run

PHP Code Beautifier

  • phpcbf_on_save – Do you want to run the fixer on file save?
  • phpcbf_show_quick_panel – Do you want the quick panel to display on execution?
  • phpcbf_executable_path – The path to the phpcbf application.
  • phpcbf_additional_args – This is the extra information you want to pass to the phpcbf command. For example which “standard” to use in order to fix the issues

PHP Linter

  • phpcs_linter_run – Do you want the PHP linter to run?
  • phpcs_linter_command_on_save – Do you want the command to execute on save?
  • phpcs_php_path – The path to the PHP executable. If empty string, use PATH to find it
  • phpcs_linter_regex – The regex for the PHP linter output

PHP Mess Detector

  • phpmd_run – Do you want the PHPMD to run? Off by default
  • phpmd_command_on_save – Do you want the command to execute on save?
  • phpmd_executable_path – The path to the phpmd executable. If empty string, use PATH to find it
  • phpmd_additional_args – This is the extra information you want to pass to the phpmd command. For example which “rulesets” you want to run

Project Based Settings

Your .project file should look something like this:

{
  "folders": [{}],
  "settings": {
    "phpcs": {
      "phpcs_additional_args": {
        "--standard": "/path/to/.composer/vendor/drupal/coder/coder_sniffer/Drupal"
      }
    }
  }
}

Of course this is a example to apply Drupal code sniffer. This could be anything. Whatever you can have on this package settings it can be overwritten under the settings -> phpcs

FAQ

What do I do when I get “OSError: [Errno 8] Exec format error”?

  • This seems to be an issue you may get with regards to wrapper scripts.
  • Please make sure that the application/script you are referencing has the correct shebang line, as per GH-79

What do I do when I get “OSError: [Error 2] No such file or directory”?

  • Well, first of all you need to check that you have PHP_CodeSniffer, and if being used, the phpmd application.
  • If you have these applications installed, then it sounds like those applications are not in your PATH, or cannot be found in your PATH by the Python runtime, so configure “phpcs_php_path”, “phpcs_executable_path”, “phpmd_executable_path” and “php_cs_fixer_executable_path” with the actual paths to those applications

What do I do when I get “OSError: [Errno 13] Permission denied”?

  • It sounds like your path settings are incorrect.
  • You need to make sure that when you specifiy the path you include the entire path including the application
$ which phpcs
/usr/local/bin/phpcs
  • That entire output is the path you need in your configs.

What if I've installed the applications using Homebrew?

If you have installed php-cs-fixer, phpmd or phpcs via homebrew then please make sure that you define the “_executable_path” option to the .phar application and not the wrapper script that is placed in your bin folder, as this will cause odd behaviour.

What other Key Bindings can I setup?

The following is a list of commands that you can bind to a keyboard shortcut:

  • phpcs_fix_this_file
  • phpcs_clear_sniffer_marks
  • phpcs_goto_next_error
  • phpcs_show_previous_errors
  • phpcs_sniff_this_file

In order to achieve this you need to add the following to one of your key bindings settings file:

{ "keys": ["ctrl+super+t"], "command": "phpcs_clear_sniffer_marks" }

To decide which “Fixer” to use, you can do:

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

or

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

You can then change the ctrl+super+t combination to something of your choosing.