top of page

A Nifty Guide to Configure PHP Style Checks with Composer



Local configuration files are an efficient alternative to composer hooks. If you are wondering whether phpcs includes a local configuration file or not, then let me tell you that phpcs supports a local configuration file called php.xml or phpcs.xml.dist.


Following is the manner in which the default standard of a project is set:

This step will ensure that a configuration file inside the vendor/directory notes this down. This means that version control cannot be used for the configuration. If a new team member has to be included then they need to run this command to avoid the use of different style checks.

Fortunately, this problem can be resolved with the help of composer command events, specifically post-install-cmd and post-update-cmd events, which can point to a PHP class that carries out the event. The following is an example to make this clear:


Once the class is created, register it in composer.json.


It should be noted that both post-install-cmd and post-update-cmd are registered to make sure that the hook runs after squizlabs/php_codesniffer gets updated. This is necessary because the process will overwrite our configuration.

Once the hook has been defined, the missing pieces needs to be filled in. To ensure that codesniffer is installed we include a generic helper method, which may be reused when needed.


Next step is the execution of config-set commands with the help of phpcs.


Now, we have all the parts needed to check the successful installation of codesniffer. To set the configuration, the postUpdate method needs to be updated and put to use.


It’s done! Now all the team members can employ the same style checks simply by running phpcs src/ post the installation/update of their local dependencies.

Below is the entire class copy/paste.


Are you a newbie who is interested in web development? Then it is best to start with PHP. Enroll yourself for the PHP course training in Kolkata at Techtree Technologies- a premier analytics training institute in Kolkata.


34 views0 comments
bottom of page