It’s one thing to be using PHP CodeSniffer manually but if you’re using a utility such as GrumPHP to check your work before committing it to the repository, then you’re likely going to want to use the version that you’re installing with your project.

This assumes that:

  1. You are installing PHP CodeSniffer local to your project,
  2. You want to install a specific set of coding standards to run against your code.

Remember, this is also done in the context of wanting to run automated scripts during the commit process and in a local environment (versus a global setting) with Composer.

Multiple Coding Standards with Composer

Composer offers the ability to write scripts during execution. From the manual:

A script, in Composer’s terms, can either be a PHP callback (defined as a static method) or any command-line executable command. Scripts are useful for executing a package’s custom code or package-specific commands during the Composer execution process.

To install multiple coding standards with Composer so we can do what we with GrumPHP and any pre-commit process, we need to take advantage of the pre-install-cmd and the pre-update-cmd scripts. Doing this will ensure that the proper coding standards are set for the project-level binary of PHP CodeSniffer.

Case in point:

View the code on Gist.

Note in the example above, I’m making sure to add the WordPress coding standards to PHP CodeSniffer.

But you can add multiple versions of coding standards, like this:

View the code on Gist.

And you can add any other standards you have permitting you point the script in the proper location of where they are installed.

Once you do that, the standards will be applied to PHP CodeSniffer and will be performed during both the install and the update Composer processes.


Leave a Reply