Download the PHP package space48/magento2-code-quality without Composer
On this page you can find all versions of the php package space48/magento2-code-quality. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package magento2-code-quality
Space48 Code Quality tool
A module that helps to easily apply static code analysers to the project's code. It uses grumphp under the hood with a predefined set of rules based on Magento 2 coding standards but tuned to be less annoying!
Usage
Usage on a regular Space48 Warden based Magento 2 project:
After pulling and installing/updating project locally run following command to update local git hooks and install npm packages:
It will add precommit git hook, so on each commit linters will run for the files added to commit.
Git Pre-Commit
A commit will fail if linters found errors in your code, check the 'console' tab in 'Git' section of the PhpStorm for errors output by linters. Fix all errors and commit again.
Warning By default Autofix feature is turned ON. Once you try to commit, linters will automatically fix some errors. Double check changes after.
When fixing errors, to see predicted linters output you can instead of trying to commit again just run manually from console:
Usage on other projects
Run vendor/bin/grumphp git:init
to add githook
Grumphp will sniff your code on any git commit
Ignoring Rules
in the linters output you will see hints with rule names being violated. If for some reason you can not fix violated rule feel free to ignore it using Suppress Warning comment corresponding concrete linter type (see hints in output or google).
IMPORTANT! When ignoring a rule in the code always add a comment with the reason of why you had ignored it instead of fixing.
Finetuning
Rules can be tuned with parameters, excluded completely or rewritten on a Project Level
using the ruleset.xml, phpmd.xml, .eslintrc, .stylelint
files in the project's root folder and committing them to project's repo.
If you think everyone can benefit from your rules changes - feel free to create a PR.
CI Integration
To add Code Quality to Bitbucket pipelines as a mandatory step do following.
Edit Makefile
Add new command to makefile:
'111999' - is your starting commit hash
Configure bitbucket-pipelines.yml
Add new step to your build like this:
Check locally
To run linters as it would run on CI (from starting commit till HEAD):
Automatically fix some errors
Use any of this options:
Option 1: Set fixer/enabled
and fixer/fix_by_default
to true in grumphp.yml file
Option 2: Run command vendor/phpro/grumphp/bin/grumphp run
with '--fix' option
Installation
Add Code Quality tool to the Magento Project:
Warning!
Eslisnt and Stylelint requires NodeJs v10 or higher.
Installation on a regular Space48 Warden based Magento 2 project:
On Magento versions earlier than 2.4 add following to 'require' section of project's composer.json
:
1. Run following commands from project root:
Add the module
When it will ask for 'grumphp.yml' creation - answer no
:
Copy files and install npm packages:
Add git precommit hook:
Create .git
folder in warden:
(You can mount .git volume from host to inside container instead, but grumphp
only requires empty .git folder,
no much sense in syncronizing all .git/* files adding even more overhead for mutagen.)
Add configuration files to git:
2. Add following to project`s 'Makefile':
It contains following commands:
linters-init
- init linters on local machineanalyse
- analyses all code from starting commit hash to HEADanalyse-fix
- analyses all code from starting commit hash to HEAD and fixes autofixable errorsprecommit
- analyses code staged for commitprecommit-fix
- analyses code staged for commit and fixes autofixable errorsanalyse-ci
- Same as 'analyse' but modified to be called during build on CI env
Update CQ_STARTING_COMMIT_HASH
variable. Replace the sample a000z999
commit hash with the hash from the project where you want to start linting from.
Files modified after the starting commit hash will be linted during project build and will fail the build on linter violations.
Update CQ_STARTING_COMMIT_DATE
variable. Replace sample 01/01/2021
date with the date of the Starting Commit
(specified at CQ_STARTING_COMMIT_HASH
variable). This is required because CI uses shallow git clone.
Note:
If using warden, commit still fails with SplFileInfo::openFile(/var/www/html/.git/COMMIT_EDITMSG): failed to open st ream: No such file or directory
error: rename or revert to original git 'commit-msg' hook.
3. Commit to project`s repo.
Commit updated composer files, vendor folder, code-quality config files from the root and 'makefile' changes
Installation on any other Magento 2 project:
- add module
space48/code-quality
via Composer - run
vendor/space48/code-quality/script/install.sh
script to copy necessary files and install npm packages - in
grumphp.yml
remove configs marked as(remove on non warden environment)
- run
vendor/bin/grumphp git:init
to update precommit hooks
Configuration
Whitelist/exclude folders can be configured at {project_root}/grumphp.yml
:
To turn off whole linter type (for example 'eslint') - remove or comment out corresponding 'task' section.
Linter rules can be finetuned on a project level by editing ruleset.xml, phpmd.xml, .eslintrc, .stylelint
files.
See Space48/code-quality/rulesets/
for examples.
Some rules can be overwritten on a class level. See rulesets/PhpMd/extra.xml
for examples.
More info
For more info and for Configuration help refer to grumphp repo docs.
All versions of magento2-code-quality with dependencies
symfony/dom-crawler Version *
magento/magento-coding-standard Version *
phpmd/phpmd Version ^2
squizlabs/php_codesniffer Version >=2.9.2 <4.0.0
space48/grumphp-stylelint-task Version ^1.0