Hello everyone, welcome to my blog. Today I am going to discuss the installation and implementation of GrumPHP. If you want to connect with me or want to discuss any specific topic, want to share your view or anything, please feel free to write a comment below or write me an email on my email id i.e. email@example.com I will definitely try to address all your queries.
So let’s start. As an extension developer, we all know the importance of coding standards and removing unwanted code from our extensions. In short, maintaining the code quality. We can do that with the help of PHPCS and PHPMD but sometimes, it’s very difficult to track all the changes, and we may commit buggy code. We can get help from GrumPHP to validate our code and pass all quality checks.
What is GrumPHP ?
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.
Installation of GrumPHP
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.
# grumphp.yml grumphp: tasks: phpcs: standard: "dev/tests/static/framework/Magento/ruleset.xml" tab_width: ~ ignore_patterns:  phpmd: exclude:  ruleset: ['dev/tests/static/testsuite/Magento/Test/Php/_files/phpmd/ruleset.xml']
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 like this.
Once you resolve all the issues and code quality matches with the standard, you will be able to see the following screen, and then you are able to push your code with ease.
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.
If you want to know more about grumphp configuration/implementation, please let me know through my email id i.e. firstname.lastname@example.org, or write down your thoughts in the comments section below. Let me know what you think about this blog. Goodbye for now and happy coding 🙂