Skip to content

Instantly share code, notes, and snippets.

@pbrocks
Last active December 22, 2023 08:16
Show Gist options
  • Star 26 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save pbrocks/ab8d8c7ce200ce6f718181cebfc57a1e to your computer and use it in GitHub Desktop.
Save pbrocks/ab8d8c7ce200ce6f718181cebfc57a1e 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

hchouhan commented Nov 1, 2023

Is there a way to allow tabs? I keep getting warnings related to tabs.

@Marcel-wowww
Copy link

You installed phpcs with homebrew but in vscode settings.json you point your executables to the composer installed version.
(which is also available in. ~/.composer/vendor/bin). So you are working with 2 versions which can become troublesome. When using this approach i prefer adding alias phpcs-wp='~/composer/vendor/bin/phpcs --standard=WordPress --extensions=php to ~/.zprofile. This way you can create multiple aliases for different systems. phpcs-12 for PSR12, phpcs-drupal for drupal etc.

@hchouhan with phpcbf you can fix tabs that phpcs errors over with the same command, but use phpcbf instead of phpcs as executable.

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