Using GrumPHP with Laravel

Rafa Rafael
2 min readJul 2, 2021

My basic configurations to automate code quality checks using GrumpPHP in my Laravel project.

Our team already uses github actions where we can automate our work flow like knowing if we made a breaking changes and/or failing test(s) on our PR. But when I was assigned to another project, the github actions is not enough to me as I tend to forget to do some stuff like running php-cs-fixer and running tests before I commit and push my changes to the repository.

I am a hard worker with a lazy attitude towards a repetitive task so I decided to find a way to automate them. Luckily, I found GrumPHP a must have tool for me as it will not just automate the tasks I needed to do but also improve my codebase quality.

When somebody commits changes, GrumPHP will run some tests on the committed code. If the tests fail, you won’t be able to commit your changes. This handy tool will not only improve your codebase, it will also teach your co-workers to write better code following the best practices you’ve determined as a team.

As quoted above, every time you commit it will run the tasks base from your configuration then when one or more task fails you will not be able to commit the changes you’ve made. Cool, right?

As you can see on the image above, I have configured 4 tasks to run and phpcsfixer fails, it showed me the list of file that has errors on the codebase.

After I fixed that error above(by running vendor/bin/php-cs-fixer fix) you will showed a success message like below.

I only used basic configurations on my activated tasks as it was only what I needed for now. You can follow this blog if you want to advance your configurations, it is for Magento but you get the idea and it is not that hard to follow. You can also check their tasks guide to easily configure and activate tasks .

My GrumPHP configuration

Dependencies

GrumPHP

composer require --dev phpro/grumphp

After successfully installing GrumPHP, you will be ask to create a configuration file and add a task, just choose the index number of PhpUnit then press enter.

PHP_CodeSniffer

composer require --dev "squizlabs/php_codesniffer=*"

PHP Stan

composer require --dev phpstan/phpstan

PHP CS Fixer

composer global --dev friendsofphp/php-cs-fixer

Then my configuration file looked like this:

Again, every time you make a commit this tasks will run so adjust your configuration well that suits you best.

Enjoy!

--

--

Rafa Rafael

Dad / Husband / Full Stack Developer / Lifelong Learner