How to Use the Grumphp Command (with examples)
Grumphp is a helpful PHP Composer plugin that allows developers to perform various source code quality checks. These checks ensure that the code follows the defined standards and guidelines, leading to better quality codebases. In this article, we will explore different use cases of the grumphp command and provide code examples for each.
Register the Git hooks
To register the Git hooks with grumphp, you can use the following command:
grumphp git:init
Motivation: Registering the Git hooks is essential to enable grumphp’s functionality. Once registered, grumphp will run the defined checks automatically in response to relevant Git events, such as commits.
Explanation: The git:init
argument tells grumphp to register the Git hooks. This command should be executed in the root directory of your project.
Example Output:
The Git hooks have been successfully initialized.
Trigger the pre-commit hook manually
Sometimes, you may want to run the pre-commit hook manually instead of waiting for a Git commit. To do that, you can use the following command:
grumphp git:pre-commit
Motivation: Manually triggering the pre-commit hook allows you to verify your changes locally and fix any issues before committing them. This helps to ensure that the code quality is maintained and avoids introducing potential problems into the repository.
Explanation: The git:pre-commit
argument instructs grumphp to run the pre-commit hook manually. This command should also be executed in the root directory of your project.
Example Output:
Running the pre-commit hook...
No issues found. All checks passed successfully.
Check every versioned file
To check every versioned file in your project, you can use the following command:
grumphp run
Motivation: Checking every versioned file is a comprehensive way to ensure that the code quality is maintained throughout the project. It helps catch issues and violations before they can cause problems in the production environment.
Explanation: The run
command tells grumphp to perform checks on all versioned files. Grumphp will analyze the code and report any violations found based on the defined quality standards.
Example Output:
Running grumphp checks...
File: src/Example.php
- Line 42: Missing docblock for method `doSomething()`
File: tests/ExampleTest.php
- Line 15: Excessive code length. The method `testComplexLogic()` exceeds the maximum allowed length.
Summary:
- 2 issues found.
By leveraging the grumphp command and its various options, developers can maintain code quality, enforce coding standards, and improve the overall stability of their PHP projects.