Skip to content

Instantly share code, notes, and snippets.

@remoblaser
Last active June 21, 2021 15:56
Show Gist options
  • Save remoblaser/075c509c20a5cf49416a56768383e0aa to your computer and use it in GitHub Desktop.
Save remoblaser/075c509c20a5cf49416a56768383e0aa to your computer and use it in GitHub Desktop.
Sublime Text configuration for PSR-2 / cubetech

Install PHPcs

Use Sublime Text’s Package Control (Preferences -> Package Control -> Install Package -> Phpcs) to install the plugin.

Install required executables

In order for Phpcs to work, we need to install various command line tools.

  • phpcs: PHP Code Sniffer
  • php-cs-fixer: PHP Coding Standards Fixer
  • php-md: PHP Mess detector (optional)

The Sublime Text Plugin will use these binaries to complete actions. Basically it's just a wrapper for these tools.

Install Composer

A easy way of installing these binaries is via Composer, a PHP Package manager. This way we can simply pull in all the required packages and update them easily. To install composer, visit https://getcomposer.org/download/ and copy / paste + execute the code inside the checkbox to your terminal. (WARNING: Do not redistribute this code since it changes all the time!). This will create a composer.phar file inside the directory you're currently in. To make composer available globally we will move it:

  mv composer.phar /usr/local/bin/composer

Install phpcs

To install phpcs simply type:

  composer global require squizlabs/php_codesniffer

Install php-cs-fixer

To install php-cs-fixer simply type:

  composer global require friendsofphp/php-cs-fixer

Install php-md (optional)

To install php-md simply type:

  composer global require phpmd/phpmd

Add vendor directory to path

In order to have our Composer dependencies available in bash, we need to add the composer vendor/bin directory to our PATH.

.bashrc

  echo 'export PATH="$PATH:$HOME/.composer/vendor/bin"' >> ~/.bashrc
  source ~/.bashrc

.zshrc (oh-my-zsh users)

  echo 'export PATH="$PATH:$HOME/.composer/vendor/bin"' >> ~/.zshrc
  source ~/.zshrc

Configure Sublime Text + Phpcs

Get the Ruleset

Since we're using Tabs instead of Spaces, we need to tell the tools which ruleset we're using and what differences we have. This can be done via XML. Here's the Cubetech Ruleset, simply store it somewhere (e.g. ~/Library/Application Support/Sublime Text 3/Packages/User):

  <?xml version="1.0"?>
  <ruleset name="MyStandard">
    <description>PSR2 with tabs instead of spaces.</description>
    <arg name="tab-width" value="4"/>
    <rule ref="PSR2">
        <exclude name="Generic.WhiteSpace.DisallowTabIndent"/>
    </rule>
    <rule ref="Generic.WhiteSpace.DisallowSpaceIndent"/>
    <rule ref="Generic.WhiteSpace.ScopeIndent">
        <properties>
            <property name="indent" value="4"/>
            <property name="tabIndent" value="true"/>
        </properties>
    </rule>
  </ruleset>

Configure Sublime

In Sublime open up the Phpcs Settings: Sublime Text -> Preferences -> Package Settings -> PHP Code Sniffer -> Settings - User. Here you can configure if Phpcs should automatically run on save which ruleset to use etc. An example config which uses phpcs, php-cs-fixer, php lint and php-md:

  {
    // Plugin settings

    // Turn the debug output on/off
    "show_debug": false,

    // Which file types (file extensions), do you want the plugin to
    // execute for
    "extensions_to_execute": ["php"],

    // Do we need to blacklist any sub extensions from extensions_to_execute
    // An example would be ["twig.php"]
    "extensions_to_blacklist": [],

    // Execute the sniffer on file save
    "phpcs_execute_on_save": true,

    // Show the error list after save.
    "phpcs_show_errors_on_save": false,

    // 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": false,

    // Show the errors in the quick panel so you can then goto line
    "phpcs_show_quick_panel": false,

    // The path to the php executable.
    // Needed for windows, or anyone who doesn't/can't make phars
    // executable. Avoid setting this if at all possible
    "phpcs_php_prefix_path": "",

    // Options include:
    // - Sniffer
    // - Fixer
    // - Mess Detector
    //
    // This will prepend the application with the path to php
    // Needed for windows, or anyone who doesn't/can't make phars
    // executable. Avoid setting this if at all possible
    "phpcs_commands_to_php_prefix": [],

    // What color to stylise the icon
    // https://www.sublimetext.com/docs/3/api_reference.html#sublime.View
    // add_regsions
    "phpcs_icon_scope_color": "comment",


    // PHP_CodeSniffer settings

    // Do you want to run the phpcs checker?
    "phpcs_sniffer_run": true,

    // Execute the sniffer on file save
    "phpcs_command_on_save": true,

    // It seems python/sublime cannot always find the phpcs application
    // If empty, then use PATH version of phpcs, else use the set value
    "phpcs_executable_path": "/Users/<USERNAME>/.composer/vendor/bin/phpcs",

    // Additional arguments you can specify into the application
    //
    // Example:
    // {
    //     "--standard": "PEAR",
    //     "-n"
    // }
    "phpcs_additional_args": {
        "--standard": "/path/to/my/ct-ruleset.xml",
        "-n": ""
    },



    // PHP-CS-Fixer settings

    // Fix the issues on save
    "php_cs_fixer_on_save": true,

    // Show the quick panel
    "php_cs_fixer_show_quick_panel": false,

    // Path to where you have the php-cs-fixer installed
    "php_cs_fixer_executable_path": "/Users/<USERNAME>/.composer/vendor/bin/php-cs-fixer",

    // Additional arguments you can specify into the application
    "php_cs_fixer_additional_args": {
        "--standard": "/path/to/my/ct-ruleset.xml",
        "-n": ""
    },



    // phpcbf settings

    // Fix the issues on save
    "phpcbf_on_save": false,

    // Show the quick panel
    "phpcbf_show_quick_panel": false,

    // Path to where you have the phpcbf installed
    "phpcbf_executable_path": "/Users/remo/.composer/vendor/bin/phpcbf",

    // Additional arguments you can specify into the application
    //
    // Example:
    // {
    //     "--level": "all"
    // }
    "phpcbf_additional_args": {
        "--standard": "/Users/remo/Library/Application Support/Sublime Text 3/Packages/User/Prefs/php-ruleset.xml",
        "-n": ""
    },



    // PHP Linter settings

    // Are we going to run php -l over the file?
    "phpcs_linter_run": true,

    // Execute the linter on file save
    "phpcs_linter_command_on_save": true,

    // It seems python/sublime cannot always find the php application
    // If empty, then use PATH version of php, else use the set value
    "phpcs_php_path": "",

    // What is the regex for the linter? Has to provide a named match for 'message' and 'line'
    "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,

    // It seems python/sublime cannot always find the phpmd application
    // If empty, then use PATH version of phpmd, else use the set value
    "phpmd_executable_path": "/Users/<USERNAME>/.composer/vendor/bin/phpmd",

    // Additional arguments you can specify into the application
    //
    // Example:
    // {
    //     "codesize,unusedcode"
    // }
    "phpmd_additional_args": {
        "codesize,unusedcode,naming": ""
    },


    // PHP Scheck settings

    // Execute scheck
    "scheck_run": false,

    // 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": "",

    // Additional arguments you can specify into the application
    //
    //Example:
    //{
    //  "-php_stdlib" : "/path/to/pfff",
    //  "-strict" : ""
    //}
    "scheck_additional_args": {
        "-strict" : ""
    }
}

Save the file and from now on your PHP Code should get formatted automatically on save.

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