Install PHPCS globally
Installing PHPCS globally makes PHPCS available for use from anywhere on a local machine. Following these instructions will install:
- The latest compatible version of PHPCS
- The latest release ofVIP-Coding-Standards (VIPCS)
- A compatible version of theWordPress Coding Standards(WordPressCS)
- Compatible versions of other standards packages, like VariableAnalysis, PHPCSUtils and PHPCSExtra
Note
PHPCS commands on a local machine runningWindows may require different formattingthan the command examples shown below.
Install
- Before beginning installation, ensure that Composer itself is up to date:
composer self-update && composer global update
- Run the command below in a terminal. This command can be run to update an existing global installation. Verbose output in the terminal will indicate what is being installed (or updated).
composer g require --dev automattic/vipwpcs -W
- The
phpcs
command should now be in the local machine’s PATH.
$ ls ~/ poser/vendor/bin phpcbf phpcs
- Check PHPCS to ensure it is up to date.
$ phpcs --version PHP_CodeSniffer version 3.7.2 (stable) by Squiz (http:// squiz.net)
Troubleshooting
If thephpcs
command does not work, the Composer bin directory on the local machine will need to be added to thePATH
environment variable. On most operating systems, thephpcbf
andphpcs
files are located in the~/ poser/vendor/bin
directory.
On a local machine running Linux, the file path might be~/.config/composer/vendor/bin
.Verify the location of thephpcbf
andphpcs
files on the user’s local machine, and modify the file path as needed when following the instructions below.
- Edit the local machine’s shell profile (e.g.,
~/.bash_profile
,~/.zshrc
,~/.bashrc
) and add thePATH
environment variable. The syntax (and the actual file that the shell loads on startup) will vary depending on the shell being used. For example, in~/.bash_profile
,add the following code to the end of the file:
export PATH= "$HOME/ poser/vendor/bin:$PATH"
- After adding and saving the
PATH
environment variable to the shell profile, either:- Open a new terminal window
- or source the shell profile in the existing terminal window by running:
source ~/.bash_profile
.
- Run the
phpcs
command again.
Last updated: December 23, 2023