Skip to content

Instantly share code, notes, and snippets.

@ajcastro
Last active July 17, 2019 03:21
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajcastro/7b71caaf378cd930fbe3 to your computer and use it in GitHub Desktop.
Save ajcastro/7b71caaf378cd930fbe3 to your computer and use it in GitHub Desktop.
Sublime Phpcs plugin Installation Instructions (LATEST)

sublime_phpcs_installation

Sublime Phpcs plugin Installation Instructions

I got the tutorials originally from http://code.tutsplus.com/tutorials/psr-duh--net-31061. Install into your sublime text editor via Package Control the Phpcs plugin(https://github.com/benmatselby/sublime-phpcs).

Then these are the packages which should be installed in your machine:

  • phpcs "squizlabs/php_codesniffer":"*"
  • phpcbf "squizlabs/php_codesniffer":"*"
  • php-cs-fixer "fabpot/php-cs-fixer":"*"
  • phpmd "phpmd/phpmd":"*"

In Linux

First try first the commands if it is already installed by typing the command name, e.g., $ phpcs If already installed, type: which then the command name, e.g. $ which phpcs and it will give you the location of the binary which is the required settings of Phpcs sublime plugin.

Each package has .phar which should be downloaded and made as executable.

Package Download link
phpcs https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar
phpcbf https://squizlabs.github.io/PHP_CodeSniffer/phpcbf.phar
php-cs-fixer http://get.sensiolabs.org/php-cs-fixer.phar
phpmd http://static.phpmd.org/php/latest/phpmd.phar
  1. Download the .phar file of each package and install.
```
$ wget <download_link> -O <package>
$ sudo chmod a+x <package>
$ sudo mv <package> /usr/local/bin/<package>
```

Use Composer to download

composer global require squizlabs/php_codesniffer
composer global require friendsofphp/php-cs-fixer

In Linux via Composer

Include the packages in global composer.json and run the $ composer global update. The repository packages are mentioned above. Location of composer.json: ~/.composer/composer.json Then copy the paths of the package into the Phpcs plugin settings.

In Windows via Composer

Include the packages in global composer.json and run the $ composer global update. The repository packages are mentioned above. Location of composer.json: C:/Users/<user>/AppData/Roaming/Composer/composer.json Then copy the paths of the .bat packages into the Phpcs plugin settings.

Sample Phpcs plugin settings

{
   "phpcs_additional_args": {
       "--standard": "PSR2",
       "-n": ""
   },
   "phpcs_executable_path": "/usr/bin/phpcs",
   "phpmd_executable_path": "/usr/local/bin/phpmd",
   "phpcbf_executable_path": "/usr/bin/phpcbf",
   "php_cs_fixer_executable_path": "/usr/local/bin/php-cs-fixer"
}
@ajcastro
Copy link
Author

ajcastro commented Feb 27, 2019

Install this to get the necessary .bat or executable files, https://github.com/squizlabs/PHP_CodeSniffer

composer global require squizlabs/php_codesniffer

Then set the path in user settings of the package

{
    "phpcs_executable_path": "C:\\Users\\ajcastro\\AppData\\Roaming\\Composer\\vendor\\bin\\phpcs.bat",
    "phpcbf_executable_path": "C:\\Users\\ajcastro\\AppData\\Roaming\\Composer\\vendor\\bin\\phpcbf.bat",
}

That's all you need to make phpcs work.

@ajcastro
Copy link
Author

ajcastro commented Feb 27, 2019

Install this for php-cs-fixer, https://github.com/FriendsOfPHP/PHP-CS-Fixer

composer global require friendsofphp/php-cs-fixer

Add in the user settings the following php-cs-fixer path:

"php_cs_fixer_executable_path": "C:\\Users\\ajcastro\\AppData\\Roaming\\Composer\\vendor\\bin\\php-cs-fixer.bat"

The final user settings will look like this:

{
    "phpcs_executable_path": "C:\\Users\\ajcastro\\AppData\\Roaming\\Composer\\vendor\\bin\\phpcs.bat",
    "phpcbf_executable_path": "C:\\Users\\ajcastro\\AppData\\Roaming\\Composer\\vendor\\bin\\phpcbf.bat",
    "php_cs_fixer_executable_path": "C:\\Users\\ajcastro\\AppData\\Roaming\\Composer\\vendor\\bin\\php-cs-fixer.bat"
}

@ajcastro
Copy link
Author

@ajcastro
Copy link
Author

ajcastro commented Mar 3, 2019

Excluding phpcs rules: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Advanced-Usage#limiting-results-to-specific-sniffs

"phpcs_additional_args": {
        "--standard": "PSR2",
        "--exclude": "Generic.Files.LineEndings",
        "-n": "",
}

@ajcastro
Copy link
Author

Final settings:

{
    "phpcs_executable_path": "C:\\Users\\ajcastro\\AppData\\Roaming\\Composer\\vendor\\bin\\phpcs.bat",
    "phpcbf_executable_path": "C:\\Users\\ajcastro\\AppData\\Roaming\\Composer\\vendor\\bin\\phpcbf.bat",
    "php_cs_fixer_executable_path": "C:\\Users\\ajcastro\\AppData\\Roaming\\Composer\\vendor\\bin\\php-cs-fixer.bat",

    "phpcs_additional_args": {
        "--standard": "PSR2",
        "--exclude": "Generic.Files.LineEndings,PSR2.Namespaces.NamespaceDeclaration.",
        "-n": "",
    }
}

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