Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hchouhan/385526ae9ff91ea175456f5044b75059 to your computer and use it in GitHub Desktop.
Save hchouhan/385526ae9ff91ea175456f5044b75059 to your computer and use it in GitHub Desktop.
Install phpcs with Homebrew

Install phpcs with Homebrew

To set up php linting, you’ll want to install this PHP CodeSniffer repo and configure with this WordPress Coding Standards repo: . There are a number of ways to do this, whether direct download, Composer, Homebrew, Pear, etc. The following is what works for me on MacOS using Homebrew:

In a terminal window on your Mac, start by updating your Homebrew.

brew doctor

Then install the Code Sniffer:

brew install php-code-sniffer

After that is done, you should be able to check that things installed correctly.

phpcs -i

will show you the Standards that you have installed. You will probably see something like:

The installed coding standards are MySource, PEAR, PSR1, PSR12, PSR2, Squiz, Zend

Once we see the installed standards, we can then download the WordPress Coding Standards. I usually just clone the repo to a directory in the root of my computer, but you can put it anywhere you like, assuming you’ll remember the path.

git clone https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards.git ~/wpcs

After that is downloaded, you’ll need to tell phpcs where to find the new coding standards. Assuming you downloaded the repo to the folder above, you can enter:

phpcs --config-set installed_paths ~/wpcs

Otherwise, you will need to point to whereever you intalled your WordPress Coding Standards.

Now that you've told phpcs where to find the xml files for WordPress Coding Standards, you can check to see that they are properly found:

phpcs -i

and now you should see:

The installed coding standards are MySource, PEAR, PSR1, PSR12, PSR2, Squiz, Zend, WordPress, WordPress-Extra, WordPress-Docs and WordPress-Core

composer global require "squizlabs/php_codesniffer=*"
phpcs --version

To lint the php files in a folder

phpcbf --extensions=php --standard=WordPress -v ./my-plugin-folder

My VSCode Settings

{
    "phpcs.enable": true,
    "phpcs.standard": "WordPress",
    "phpcbf.onsave": true,
    "phpcbf.standard": "WordPress",
    "window.zoomLevel": 1,
    "workbench.colorTheme": "Default Light+",
    "editor.fontSize": 13,
    "[javascript]": {
        "editor.defaultFormatter": "vscode.typescript-language-features"
    },
    "[php]": {
        "editor.defaultFormatter": "persoderlind.vscode-phpcbf"
    },
    "javascript.updateImportsOnFileMove.enabled": "always",
    "[javascriptreact]": {
        "editor.defaultFormatter": "esbenp.prettier-vscode"
    },
    "git.autofetch": true,
    "editor.insertSpaces": false,
    "editor.formatOnPaste": true,
    "editor.tabSize": 4,
    "editor.detectIndentation": false,
    "editor.quickSuggestions": true,
    "[json]": {
        "editor.defaultFormatter": "vscode.json-language-features"
    },
    "phpcs.executablePath": "/Users/pbarthma/.composer/vendor/squizlabs/php_codesniffer/bin/phpcs",
    "phpcbf.executablePath": "/Users/pbarthma/.composer/vendor/squizlabs/php_codesniffer/bin/phpcbf",
    "phpcs.showSources": true,
    "git.confirmSync": false,
    "remote.SSH.showLoginTerminal": true,
    "editor.defaultFormatter": "valeryanm.vscode-phpsab",
    "editor.codeActionsOnSave": null,
    "phpsab.standard": "valeryanm.vscode-phpsab",
    "phpsab.allowedAutoRulesets": [
    
        ".phpcs.xml",
        ".phpcs.xml.dist",
        "phpcs.xml",
        "phpcs.xml.dist",
        "phpcs.ruleset.xml",
        "ruleset.xml"
    ],
}

My SublimeText 3 PHPCS Settings

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

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

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

    // 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
    // - MessDetector
    // - CodeBeautifier
    //
    // 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_regions
    "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": "/usr/local/bin/phpcs",

    // Additional arguments you can specify into the application
    //
    // Example:
    // {
    //     "--standard": "PEAR",
    //     "-n"
    // }
    "phpcs_additional_args": {
        "--standard": "WordPress",
        "-n": ""
    },

    // PHP-CS-Fixer settings

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

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

    // Additional arguments you can specify into the application
    "php_cs_fixer_additional_args": {

    },

    // phpcbf settings

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

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

    // Path to where you have the phpcbf installed
    "phpcbf_executable_path": "/usr/local/bin/phpcbf",

    // Additional arguments you can specify into the application
    //
    // Example:
    // {
    //     "--level": "all"
    // }
    "phpcbf_additional_args": {
        "--standard": "WordPress",
        "-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": "/usr/bin/php",

    // 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+)",
}

SublimeText 3 Preferences Settings

{
	"color_scheme": "Packages/Theme - Cobalt2/cobalt2.tmTheme",
	"extensions_to_blacklist":
	[
	],
	"extensions_to_execute":
	[
		"php"
	],
	"font_size": 16,
	"ignored_packages":
	[
		"Vintage"
	],
	"phpcbf_additional_args":
	{
		"--standard": "WordPress",
		"-n": ""
	},
	"phpcbf_executable_path": "/usr/local/bin/phpcbf",
	"phpcbf_on_save": true,
	"phpcbf_show_quick_panel": true,
	"phpcs_additional_args":
	{
		"--standard": "WordPress",
		"-n": ""
	},
	"phpcs_command_on_save": true,
	"phpcs_commands_to_php_prefix":
	[
	],
	"phpcs_executable_path": "/usr/local/bin/phpcs",
	"phpcs_execute_on_save": true,
	"phpcs_icon_scope_color": "comment",
	"phpcs_outline_for_errors": false,
	"phpcs_php_prefix_path": "",
	"phpcs_show_errors_in_status": true,
	"phpcs_show_errors_on_save": false,
	"phpcs_show_gutter_marks": true,
	"phpcs_show_quick_panel": true,
	"phpcs_sniffer_run": true,
	"show_debug": true,
	"theme": "Default.sublime-theme"
}

SublimeText 3 Package Control Settings

{
	"bootstrapped": true,
	"in_process_packages":
	[
	],
	"installed_packages":
	[
		"DocBlockr",
		"Package Control",
		"PHP Codebeautifier",
		"Phpcs",
		"SublimeLinter-contrib-php-cs-fixer",
		"SublimeLinter-phpcs",
		"Theme - Cobalt2"
	]
}
@hchouhan
Copy link
Author

hchouhan commented Jan 21, 2022

I'm currently learning about this and struggling. Have you ever installed this using composer for say specific theme testing?

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