TLDR; this article is a tutorial setting up GrumPHP task runner for quality checks in Magento 2. An earlier video tutorial and presentation by me is already there.
Check out: https://youtu.be/tq-DPi9wMss and the presentation https://slides.com/milindsingh/virtual-magento-meetup/
-
why automate?
- developers are (a little) lazy
- running 6-7 tools manually is what I will try to skip few times ( and expect it would pass in deployment pipelines )
- we are overburdened (most of the times)
- development pipelines will fail
- suppose we miss a space at the end of the file, and phpcs fails while running the automated pipelines on pull request merge.
- fix the change and again push just to add a simple line at the end of the file (time taking)
- automation can be enforced
- grumphp can be configured to listen to git commit commands and will not allow until all quality checks passed.
- developers are (a little) lazy
-
what to automate?
- PHP Code Sniffer
- https://github.com/squizlabs/PHP_CodeSniffer
- https://github.com/magento/magento-coding-standard
- tokenizes PHP, JavaScript and CSS files to detect violations of a defined coding standard
- PHP Code Beautifier
- automatically correct coding standard violations
- PHP Coding Standards Fixer
- https://github.com/FriendsOfPHP/PHP-CS-Fixer
- automatically correct coding standard violations (more rules available, not needed to validate)
- PHP Mess Detector
- https://phpmd.org/
- auto-detects cyclomatic complex in the code
- PHP Stan
- Keywords
- print_r("test")
- die("hi")
- ObjectManager::getInstance()
- Custom
- Create your own automation!
- You can check out a small task created by me to validate swagger documentation before every commit. https://github.com/milindsingh/grumphp-swagger
- PHP Code Sniffer
how to automate?
- ### GrumPHP
GrumPHP has a set of common tasks built-in.
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.
- ### Installation
Install GrumPHP by running
+ `composer require --dev phpro/grumphp` (in current project only) or,
+ `composer global require --dev phpro/grumphp` (globally recommended)
-
Dependencies
+ #### PHPCS: - Install PHP CodeSniffer (skip if already installed) : + `composer global require --dev "squizlabs/php_codesniffer=*"` (globally recommended) + `composer require --dev "squizlabs/php_codesniffer=*"` (at project level, need to add `project-root/vendor/bin` to PATH for direct cli use) - Goto Magento2 root run following commands to install Magento2 coding standard : `composer require --dev magento/magento-coding-standard` - Set Magento2 Standard in PHP CodeSniffer available standards: `phpcs --config-set installed_paths ../../magento/magento-coding-standard/`
-
PHPCS Fixer 2
Install PHP Coding Standards Fixer (skip if already installed) :
-
composer global require --dev friendsofphp/php-cs-fixer
(globally recommended) -
composer require --dev friendsofphp/php-cs-fixer
(at project level, need to addproject-root/vendor/bin
to PATH for direct cli use)- #### PHPStan
-
composer global require --dev phpstan/phpstan
(globally recommended) -
composer require --dev phpstan/phpstan
(at project level, need to addproject-root/vendor/bin
to PATH for direct cli use) - #### PHPMD
-
composer global require --dev phpmd/phpmd
(globally recommended) -
composer require --dev phpmd/phpmd
(at project level, need to addproject-root/vendor/bin
to PATH for direct cli use) - #### PHPLint
-
composer global require --dev php-parallel-lint/php-parallel-lint
(globally recommended) -
composer require --dev php-parallel-lint/php-parallel-lint
(at project level, need to addproject-root/vendor/bin
to PATH for direct cli use) - ### Setup
GrumPHP can monitor each git repository push action by initializing it in the repository. GrumPHP can be configured at 2 levels:
-
Project Level Setup
- Goto the
magento-2-root
and run:-
php vendor/bin/grumphp git:init
orgrumphp git:init
(recommended)
-
- Create a grumphp.yml file in
magento-2-root
and copy all content as below code. - Though GrumPHP auto detect
git commit
command but you can manually test by runningphp vendor/bin/grumphp
run orgrumphp run
inside inmagento-2-root
.
- Goto the
-
Module Level Setup
-
Goto the module directory i.e.
magento-2-root/app/code/MyVendor/MyModule
and run:-
php vendor/bin/grumphp git:init
orgrumphp git:init
(recommended)
-
Create a grumphp.yml file in
magento-2-root/app/code/MyVendor/MyModule
and copy all content as below code.Add bin path
magento2-root/vendor/bin
to your modulecomposer.json
. Refer to composer.json.sample.
{ "config": { "bin-dir": "../../../../vendor/bin" } }
- Same as project level setup, GrumPHP auto detect
git commit
command but you can manually test by runningphp ../../../../vendor/bin/grumphp
run orgrumphp run
inside module.
-
- ### References
- https://www.integer-net.com/magento-2-automatic-code-quality-check-with-grumphp/
- https://github.com/phpro/grumphp/blob/master/doc/tasks.md
- https://devdocs.magento.com/guides/v2.4/test/testing.html
- https://github.com/milindsingh/magento2-grumphp
- https://github.com/milindsingh/grumphp-swagger
-
-
Top comments (0)
Some comments may only be visible to logged-in visitors. Sign in to view all comments.