macOS is still shipping with PHP (though how this looks for future versions of the OS is likely going to change in some way.).
Anyway, imagine you’re in a situation where you’re working on a project that requires three different components:
- A set of coding standards,
- PHP CodeSniffer that work with said coding standards,
- The inability to properly see results of sniffing the code either in your terminal or your IDE.
When this happens, this is almost always a result of a version of PHP, the version of the various dependencies, and making sure they all play well together.
In short, it’s about making sure the tools like PHP CodeSniffer work well with the same version of PHP you have installed. And in this case, the latest and greatest isn’t necessarily the best course of action.
It is, however, close. And in the following steps, you should be able to get everything working exactly as needed for your specific set up.
PHP and PHP CodeSniffer Errors
I’m assuming that you’re running a recent version of macOS and you’ve already got PHP up and running along with Hombrew. Skip the following section, if so.
If not, make sure to run the following commands:
$ xcode-select --install
This will install necessary tools for Homebrew to use.
$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
Once done, run the following in your terminal:
$ brew --version
And you should see something like this:
If you’re used to using Homebrew, it’s not a bad idea to run the following (though it’s not necessary):
$ brew doctor
$ brew cleanup
Once that’s done, you’re ready to install a different version (or even versions) of PHP.
Installing a Compatible Version of PHP
At this time of this post, I’ve found PHP 7.3.21 and PHP CodeSniffer 3.3.1 to work the best with each other.
To install PHP that version of PHP you can run:
$ brew install firstname.lastname@example.org
This should automatically link this version of PHP from what you’ve already installed. You can verify this by once again running:
$ php -v
And you should see PHP 7.3.21.
What About PHP CodeSniffer?
There are a number of ways to install this particular package but I’m a fan of Composer so running the following should take care of everything.:
$ composer global require "squizlabs/php_codesniffer=3.*"
And that should wrap up everything that you need to have for PHP CodeSniffer.
This should resolve any errors you see in your terminal and/or your IDE around PHP and PHP CodeSniffer errors. For more information on everything you’ve seen here, check out the following: