GrumPHP is a composer library which helps to check the code quality at the time of code commit. GrumPHP creates a git hook before your commit and validates the code quality using the set of rules provided by us(in this case a set of rules provided by Magento). It helps to write the same quality of code as Magento core developers.
I am assuming that you have already installed Magento’s latest version and composer into your local system. Now, let’s open our terminal or command prompt, navigate to our Magento root directory and run the following command.
composer require --dev phpro/grumphp
This command will help us to install grumphp with all the dependencies. At the end of the installation, GrumPHP will ask to create a grumphp.yml file type ‘yes/y’ and then in the next step, we need to select the coding standard that we need to follow. For our Magento project select PHPCS.
Now, we need to install other required dependencies like phpcs and phpmd. PHPcs stands for PHP code-sniffer whereas phpmd stands for PHP mess detector.
Execute the following command from our terminal to install these dependencies.
composer require --dev squizlabs/php_codesniffer phpmd/phpmd
Once both the dependencies are installed successfully, it’s time to set our coding standards in a grumphp.yml file. Open grumphp.yml file in any text editor and add the following content.
Here, inside the tasks option, we have set the standards for PHPcs and PHPmd. We can add different sets of rules based on our requirements.
Now, to check that our grumphp is working or not, navigate to our Magento root directory and check the git status for any changes using the following command.
Now add the file that we need to commit using the following command
Git add <your-file-name>
You will not get any error or warning message this point of time, because the grumphp hook only works on before commit. Let’s commit our code using the following command.
Git commit -m <your-commit-message>
Once we execute this git commit command the code is validated by the coding standard that we have set inside the grumphp.yml file and it will throw an error message.
Once you resolve all the issues and code quality matches with the standard, you will be able to see the success message.
Grumphp adds a hook before your commit and terminates the process to restrict the code from being committed. This will help to match the code quality and maintain the coding standards as core Magento developers. Your colleagues will also learn to write better code according to the best practices of your team.