logomichael sumner
Contact

How to Install PHPCS using Composer Globally on Mac

There isn’t an up-to-date article on installing PHP Code Sniffer PHPCS through Composer on your Mac, as there tends to be issues with doing so. It is also impossible to find one that worked for me, hence myself writing this article. This might be the same for you, and I hope that this article is useful. If not then contact me.

First off, we have to install composer. You can check the Composer website on how to do so.

Next, we have to install Homebrew. Again it’s best for me to reference the website as they will have the up-to-date instructions on the installation command.

Once we have both Composer and Homebrew installed, the next thing we want is to remove the PHP bundled with the Mac. Since this is a developers blog I reckon you are using PHP within an isolated environment, and not the Apple-supplied one.

You can find a good article on how to remove the Apple-supplied PHP here, testerlogic.com has a good set of instructions to remove the apple-supplied PHP installation.

Or you can follow by running the below commands and instructions:

xcode-select --install
brew update
brew upgrade
brew cleanup
brew doctor

What we’re doing here is installing xcode in case it isn’t installed yet, and we are performing brew checks to make sure everything is alright.

Next, you will want to check if anything pops up on here. If there are, then identify what programs are running these PHP scripts as we will have to close those programs.

ps ax | grep php

This is checking the processes have any mention of php and listing them, if so.

If there is nothing that shows up after executing the above command, let’s continue.

brew cleanup
brew doctor
brew install php
hash -r

The code above does the brew checks again, and also installs PHP within Homebrew. Afterwards we run the last command to refresh zsh’s internal paths.

Now we have to follow what brew doctor says, usually it will come up with an error and say you need to overwrite some files:

brew link --force --overwrite php
brew doctor
php -v

This will re-link the Homebrew PHP and check to remove the link of the Apple-supplied PHP, which we no longer want (since Apple is using it for legacy apps). We then check brew doctor to make sure everything is okay, and to finalise we check the php command for its version.

Installing PHPCS PHP Codesniffer

The next phase of this article is to install PHPCS. What may be happening in your case (if it’s the same as mine) was that PHPCS command cannot be found, as we still have to add the global composer to your $PATH variable. Execute the following commands below:

nano ~/.zshrc

If you’re finding trouble saving this on the nano editor interface, use the shortcut Ctrl+X and then press Y for yes.

And on a new line, add the following code:

export PATH="$HOME/.composer/vendor/bin:$PATH"

Save the file, close it, and get back onto the terminal with the following command:

source ~/.zshrc

This will refresh the .zshrc for your current terminal window.

And now let’s install PHP Codesniffer within the global Composer (system-wide):

composer global require squizlabs/php_codesniffer

Check it is working by running the code:

which phpcs

You should get the following:

/Users/$USERNAME/.composer/vendor/bin/phpcs
This completes the installation process. Note that $USERNAME will be your own machine’s username.

Changing PHP Versions

You may realise that you might have installed the latest PHP version, but you would like to use a different version instead. You can do so with the following command:

brew install [email protected] # or the old version of PHP you would like

Now we have to find out the name of the latest version of PHP you would like to unlink:

brew list

This will display the list of brews. Find your one, mine is php in this example:

brew unlink php
brew link --force --overwrite [email protected]
brew doctor
php -v

You will notice that php has no version, that is because homebrew will detect the latest version of PHP to use and regularly update it. You can, of course, set this as a version of its own.

Now, let’s return to the latest version of PHP:

brew unlink [email protected]
brew link --force --overwrite php
brew doctor
php -v

You will now be running the latest version of PHP in this example.

Have fun with PHP Code Sniffer!