Git hooks to improve code quality (grumphp, phpcs, phpcpd and phpstan)

Photo by Damir Spanic on Unsplash

Git hooks are scripts that run automatically every time a particular event occurs in a Git repository. They let you customize Git’s internal behavior and trigger customizable actions at key points in the development life cycle.

  • pre-commit: Check the commit message for spelling errors.
  • pre-receive: Enforce project coding standards.
  • post-commit: Email/SMS team members of a new commit.
  • post-receive: Push the code to production.

Calling Grumphp with a Git hook

composer require --dev phpro/grumphp
php ./vendor/bin/grumphp configure
parameters:
ascii:
failed: ~
succeeded: ~
tasks:
phplint: ~
behat: ~
phpunit: ~
phpcs:
standard:
- 'PSR12'
php ./vendor/bin/grumphp git:pre-commit
$ git commit -m "test grumpphp hook"
GrumPHP detected a pre-commit command.
GrumPHP is sniffing your code!
Running task 1/2: Phpcs ...
running task 2/2: Phpunit ...
FILE: /path/to/code/myfile.php
--------------------------------------------------------------------------------
FOUND 5 ERRORS AFFECTING 4 LINES
--------------------------------------------------------------------------------
2 | ERROR | [ ] Missing file doc comment
3 | ERROR | [x] TRUE, FALSE and NULL must be lowercase; expected "false" but
| | found "FALSE"
5 | ERROR | [x] Line indented incorrectly; expected at least 4 spaces, found 1
8 | ERROR | [ ] Missing function doc comment
8 | ERROR | [ ] Opening brace should be on a new line
--------------------------------------------------------------------------------
PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
--------------------------------------------------------------------------------
$ phpcs /path/to/code/myfile.phpFILE: /path/to/code/myfile.php
--------------------------------------------------------------------------------
FOUND 5 ERRORS AFFECTING 4 LINES
--------------------------------------------------------------------------------
2 | ERROR | [ ] Missing file doc comment
3 | ERROR | [x] TRUE, FALSE and NULL must be lowercase; expected "false" but
| | found "FALSE"
5 | ERROR | [x] Line indented incorrectly; expected at least 4 spaces, found 1
8 | ERROR | [ ] Missing function doc comment
8 | ERROR | [ ] Opening brace should be on a new line
--------------------------------------------------------------------------------
PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
--------------------------------------------------------------------------------

PHP scripts to detect violations

PHPCS

$ composer require "squizlabs/php_codesniffer=*"
/vendor/bin/phpcs
/vendor/bin/phpcbf
$ ./vendor/bin/phpcs /path/to/code-directory

PHPCPD

composer require --dev sebastian/phpcpd
# grumphp.yml
grumphp:
tasks:
phpcpd:
directory: ['.']
exclude: ['vendor']
fuzzy: false
min_lines: 5
min_tokens: 70
triggered_by: ['php']

PHPStan

composer require --dev phpstan/phpstan
# grumphp.yml
grumphp:
tasks:
phpstan:
autoload_file: ~
configuration: ~
level: null
force_patterns: []
ignore_patterns: []
triggered_by: ['php']
memory_limit: "-1"
use_grumphp_paths: true
$ vendor/bin/phpstan analyse src tests

References:

--

--

--

Software Engineer | Technical Writer | IT Enthusiast

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Movement Of a 2D Player in Unity.

Solving Algeria’s engineering Brain Drain ? 🧠✈️ — A study about Salaries💰 and Military training

Agile methods (alone) don’t scale

How to install an SSL Certificate on Webmin?

/e/OS’ user interface is evolving with a new, streamlined, visual identity

Data Scientist’s Guide: Setting up AWS DataPipeline for running python ETL scripts using…

Introducing Canopy Charts: A Visual Editor for Dynamic Charts

What are variables in programming

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Aicha Fatrah

Aicha Fatrah

Software Engineer | Technical Writer | IT Enthusiast

More from Medium

SE3050 -Application Frameworks Blog

Effective Session Management using Supertokens

Populate Your Hubspot Sandbox with Production or Production-like Data

Git Feature — A new methodology to manage your development work