phpcs and phpmd in sublime text 3 on osx

phpcs and phpmd in sublime text 3 on osx

This post will show you how to install phpcs and phpmd in sublime text on your mac:

Following on from my post about using git and phpcs in brackets, I decided to try the same setup in sublime text.

The first part is some background setup – installing phpcs and phpmd on your mac:

open terminal to install phpcs and phpmd in sublime text

 
     mkdir ~/bin
     cd ~/bin
     # install phpcs and phpmd
     git clone https://github.com/squizlabs/PHP_CodeSniffer.git phpcs
     git clone git://github.com/phpmd/phpmd.git

     # install composer into your bin folder
     curl -s http://getcomposer.org/installer | php     

     # add composer, phpcs and phpmd to your path
     sudo ln -s ~/bin/phpcs/scripts/phpcs /usr/local/bin/phpcs
     sudo ln -s ~/bin/phpmd/src/bin/phpmd /usr/local/bin/phpmd
     sudo ln -s ~/bin/composer.phar /usr/local/bin/composer

     # configure phpmd
     cd ~/bin/phpmd
     composer install

Optional part to tell phpcs about wordpress coding standards:


     #optional part to install wordpress coding standards
     git clone https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards.git wpcs
     
     # tell phpcs about the wordpress coding standards
     cd ~/bin/phpcs
     scripts/phpcs --config-set installed_paths ../wpcs
     
     # we probably want to ignore the space indenting on the files
     vim ~/bin/ruleset.xml

Paste in the following:

       
<?xml version="1.0"?>
<ruleset name="Custom">
    <description>Wordpress, but without linelength check.</description>
    <rule ref="WordPress">
        <exclude name="Generic.Files.LineLength"/>
        <exclude name="Generic.WhiteSpace.ScopeIndent"/>
        <exclude name="Generic.WhiteSpace.DisallowSpaceIndent"/>
        <exclude name="Generic.WhiteSpace.DisallowTabIndent"/>
        <exclude name="WordPress.WhiteSpace.ControlStructureSpacing.NoSpaceBeforeCloseParenthesis"/>
        <exclude name="WordPress.WhiteSpace.ControlStructureSpacing.NoSpaceAfterOpenParenthesis"/>
        <exclude name="WordPress.Arrays.ArrayDeclaration"/>
        <exclude name="WordPress.PHP.YodaConditions"/>
        <exclude name="WordPress.XSS.EscapeOutput"/>
        <exclude name="WordPress.Arrays.ArrayKeySpacingRestrictions"/>
        <exclude name="WordPress.WhiteSpace.CastStructureSpacing"/>
        <exclude name="PEAR.Functions.FunctionCallSignature.Indent"/>
        <exclude name="PEAR.Functions.FunctionCallSignature.SpaceBeforeCloseBracket"/>
        <exclude name="PEAR.Functions.FunctionCallSignature.SpaceAfterOpenBracket"/>
    </rule>
</ruleset>

phpcs and phpmd in sublime text

phpcs and phpmd in sublime text

Now that we have phpcs and phpmd (and some optional coding standards) we can use the sublime-phpcs plugin to plug this into sublime text: open sublime text

If you dont have ‘package manager’ installed, install it in your sublime text from here

press cmd+shift+p to open the quick list, type: install
then type ‘phpcs’

or:

Preferences > Package Control > Install Package > Phpcs

once the plugin is installed, open the user prefs:

Preferences > Package Settings > Php Code Sniffer > User settings

Drop the following in:


{
    "phpcs_executable_path" : "/usr/local/bin/phpcs",
    "phpmd_executable_path" : "/usr/local/bin/phpmd",
    "phpcs_additional_args": {
        "--standard": "~/bin/ruleset.xml",
        "-n": ""
    },
}

Now, when you save a file using phpcs and phpmd in sublime text your files will be automatically tested :)
You can also right click on a file and check it form there :D

  • I followed your instructions but I can’t make the part about the xml file work, it seems as if I can only specify a predefined standard name, and not the path to an xml file. So “–standard”: “WordPress” works fine, but “–standard”: “/path/to/ruleset.xml” doesn’t work and I get no error messages in the console, nothing happens.