|
| 1 | +# Space48 Code Quality tool |
| 2 | + |
| 3 | +A module that helps to easily apply static code analysers to the project`s code. |
| 4 | +It uses [grumphp](https://github.com/phpro/grumphp) under the hood and has predefined set of rules |
| 5 | +based on Magento 2 coding standards and tuned to be less annoying. |
| 6 | + |
| 7 | +## Usage |
| 8 | +After pulling and installing a project locally just run following command to update local git hooks |
| 9 | +and install npm packages inside Docker container: |
| 10 | +```shell |
| 11 | +make linters-install |
| 12 | +``` |
| 13 | +Now on pre-commit either from console or from phpstorm a linter will run against committed code |
| 14 | + |
| 15 | +To run precommit manually: |
| 16 | +```shell |
| 17 | +make precommit |
| 18 | +``` |
| 19 | +To run linters as it would run on CI (from starting commit till HEAD): |
| 20 | +```shell |
| 21 | +make analyse |
| 22 | +``` |
| 23 | + |
| 24 | +## Installation |
| 25 | + |
| 26 | +Add Code Quality tool to the Magento Project: |
| 27 | + |
| 28 | +### Installation on a regular _Space48 Warden based Magento 2_ project: |
| 29 | + |
| 30 | +#### 1. Run following commands from project root: |
| 31 | +``` |
| 32 | +warden env exec php-fpm composer require --dev space48/code-quality:@dev |
| 33 | +warden env exec php-fpm chmod +x ./vendor/space48/code-quality/script/install.sh |
| 34 | +warden env exec ./vendor/space48/code-quality/script/install.sh |
| 35 | +vendor/bin/grumphp git:init |
| 36 | +git add ruleset.xml phpmd.xml .eslintrc grumphp.yml |
| 37 | +``` |
| 38 | + |
| 39 | +#### 2. Add following to project`s 'Makefile': |
| 40 | +```makefile |
| 41 | +linters-install: # installs Space48 Code Quality |
| 42 | + warden env exec php-fpm chmod +x ./vendor/space48/code-quality/script/install.sh |
| 43 | + warden env exec php-fpm ./vendor/space48/code-quality/script/install.sh |
| 44 | + vendor/bin/grumphp git:init |
| 45 | + |
| 46 | +analyse: # analyses all code from starting commit hash to HEAD |
| 47 | + git diff e111c999..HEAD | warden env run --rm php-fpm 'vendor/phpro/grumphp/bin/grumphp' run |
| 48 | + |
| 49 | +precommit: # analyses code staged for commit |
| 50 | + git diff --staged | warden env run --rm php-fpm 'vendor/phpro/grumphp/bin/grumphp' run |
| 51 | +``` |
| 52 | + |
| 53 | +Replace the sample `e111c999` commit hash with the hash from the project where you want to start linting from. |
| 54 | +Files modified after the starting commit hash will be linted during project build and will fail the build on linter violations. |
| 55 | + |
| 56 | +#### 3. Commit to project`s repo. |
| 57 | +Commit updated composer files, vendor folder, code-quality config files from the root and 'makefile' changes |
| 58 | + |
| 59 | +### Installation on any other Magento 2 project: |
| 60 | +1. add module `space48/code-quality` via Composer |
| 61 | +2. run `vendor/space48/code-quality/script/install.sh` script to copy necessary files |
| 62 | +3. run `vendor/bin/grumphp git:init` to update precommit hooks |
| 63 | + |
| 64 | +## Configuration |
| 65 | + |
| 66 | +Whitelist/exclude folders can be configured at `{project_root}/grumphp.yml`: |
| 67 | +```yaml |
| 68 | +phpmd: |
| 69 | + whitelist_patterns: |
| 70 | + - /^app/ |
| 71 | + triggered_by: [ 'php', 'phtml' ] |
| 72 | + exclude: [ ] |
| 73 | +phpcs: |
| 74 | + whitelist_patterns: |
| 75 | + - /^app/ |
| 76 | + triggered_by: [ 'php', 'phtml' ] |
| 77 | + ignore_patterns: [ ] |
| 78 | +eslint: |
| 79 | + triggered_by: [ js, jsx, ts, tsx, vue ] |
| 80 | + whitelist_patterns: [ /^app/ ] |
| 81 | +``` |
| 82 | +To turn off whole linter type (for example 'eslint') - remove or comment out corresponding 'task' section. |
| 83 | + |
| 84 | +Linter rules can be finetuned on a project level by editing `ruleset.xml, phpmd.xml, .eslintrc` files. |
| 85 | +See `Space48/code-quality/rulesets/` for examples. |
| 86 | + |
| 87 | +## More info |
| 88 | +For more info and for Configuration help refer to [grumphp repo](https://github.com/phpro/grumphp) docs. |
0 commit comments