When you’re working with PHP CodeSniffer, and you’re doing so in the terminal, you’re likely looking to output errors into something other than your IDE.

I mean, if that were the case, you’d just have it running in your editor, right?

But there are times where you may be interested in finding the problems in other people’s code. Perhaps it’s a dependency, perhaps it’s a third-party piece of software, or perhaps it’s a favor.

Whatever the case, if you’ve set up the project directory in a way that uses Composer to include PHP CodeSniffer and you’re using the WordPress Coding Standards, then you’re likely going to need to exclude files from PHP CodeSniffer when running the program.

And this is how you can do that.

Exclude Files From PHP CodeSniffer

Before getting into the commands that are necessary, I assume you have a directory setup something like this:

Exclude Files From PHP CodeSniffer: A potential directory structure.

A potential directory structure.

That is, you have:

  • a bunch of PHP files (and subdirectories containing PHP files),
  • a composer.json file for bringing PHP CodeSniffer into the project
  • a vendor directory that includes the dependencies for PHP CodeSniffer

If you have the WordPress Coding Standards installed but don’t have them set for PHP CodeSniffer, issue this command:

$ ./vendor/bin/phpcs --config-set installed_paths ~/path/to/wpcs

So here’s the problem:

You want to exclude files from PHP CodeSniffer, but you have a lot of files to sniff (namely since you introduced the vendor directory). How do we exclude files from PHP CodeSniffer while still getting the results that we want?

You need a few additional commands:

  1. make sure you’ve included the WordPress Coding Standards as part of the configuration,
  2. explicitly tell PHP CodeSniffer to use the WordPress standard
  3. tell PHP CodeSniffer which directories to ignore and which files to process

Sound complicated? The command is really easy:

$ ./vendor/bin/phpcs --ignore=*/vendor/* --standard=WordPress .

And the terminal should end up looking like this:

Excluding Files from PHP CodeSniffer

The net result of sniffing only PHP files for the current directory.

At this point, you should be able to view the errors in your terminal (hopefully there are none, right? 😏) or you can pipe them into another file to review in another application or at a later date.