İçeriğe geç →

Automate PHP Code Quality With Grumphp

Every software project need feature and that might have a bug, In order to keep code quality in big teams really hard. What if our continuous integration tools run when we do commit? of course, we can save a lot of time in the code review process

What is Grumphp ?

Sick and tired of defending code quality over and over again? GrumPHP will do it for you! This composer plugin will register some git hooks in your package repository. When somebody commits changes, 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.

grumphp

What is our purpose ?

In order to do commit Grumphp will check the following steps

  • Unit Test With PHPUnit | Of course, All of them should be passed
  • Phan for Better PHP
  • PHP CodeSniffer for Better Code Standart
  • Git Commit Message for Understandable work what did you do in code ?
  • Git Branch Name for Task-Based branch name

Let’s Begin

We need to set up some dependencies for code quality

composer require --dev phpro/grumphp

composer require phan/phan

composer require --dev phpunit/phpunit

composer require --dev squizlabs/php_codesniffer

Grumphp : While Grumphp is installing there will be a question about the configuration you can choose one of them in order to skip that step.

Phan : run ./vendor/bin/phan –init –init-level=1 there will be created .phan/config.php , you should edit target_php_version and directory_list

PHPUnit : If you want to run PHPUnit you need to configured phpunit.xml

Delete content in grumphp.yml and then paste following steps

grumphp:
  process_timeout: 600
  tasks:

For PHPUnit

In order to do commit your tests should be passed

grumphp:
  process_timeout: 600
  tasks:
    phpunit:
      config_file: ~
      testsuite: ~
      group: []
      always_execute: true

for more details https://github.com/phpro/grumphp/blob/master/doc/tasks/phpunit.md

For Git Branch Name

In order to understand what is the branch purpose, every branch name should be like JIRA-[TASKID]

git_branch_name:
      whitelist:
        - "/JIRA-\\d+/"
      blacklist:
        - "develop"
        - "master"
      additional_modifiers: ''
      allow_detached_head: true

for more details

https://github.com/phpro/grumphp/blob/master/doc/tasks/git_branch_name.md

For Git Commit Message

In order to understandable commit message should be started JIRA-[TASKID] | What have you done

For ex : “JIRA-3 | UserController feature test has done”

git_commit_message:
allow_empty_message: false
enforce_capitalized_subject: true
enforce_no_subject_punctuations: false
enforce_no_subject_trailing_period: true
enforce_single_lined_subject: true
type_scope_conventions: []
max_body_width: 72
max_subject_width: 60
matchers:
Must contain JIRA issue number: /JIRA-\d+ \|/
case_insensitive: true
multiline: true
additional_modifiers: ''

for more details

https://github.com/phpro/grumphp/blob/master/doc/tasks/git_commit_message.md

For Phan

Phan looks for common issues and will verify type compatibility on various operations when type information is available or can be deduced. Phan has a good (but not comprehensive) understanding of flow control and can track values in a few use cases (e.g. arrays, integers, and strings).

   phan:
      config_file: .phan/config.php
      output_mode: text
      output: null
      triggered_by: 

for more details

https://github.com/phpro/grumphp/blob/master/doc/tasks/phan.md

For PHP Code Sniffer

PHP_CodeSniffer is a set of two PHP scripts; the main phpcs script that tokenizes PHP, JavaScript and CSS files to detect violations of a defined coding standard, and a second phpcbf script to automatically correct coding standard violations. PHP_CodeSniffer is an essential development tool that ensures your code remains clean and consistent.

phpcs:
      standard: ['PSR2']
      severity: ~
      error_severity: ~
      warning_severity: ~
      tab_width: ~
      report: full
      report_width: ~
      whitelist_patterns: []
      encoding: ~
      ignore_patterns: ['.phan/config.php']
      sniffs: []
      triggered_by: 
      exclude: [
      ]

for more details

https://github.com/phpro/grumphp/blob/master/doc/tasks/phpcs.md

And of the config, the grumphp.yml file should be as below

grumphp:
  process_timeout: 600
  tasks:
    phpunit:
      config_file: ~
      testsuite: ~
      group: []
      always_execute: true
    git_branch_name:
      whitelist:
        - "/JIRA-\\d+/"
      blacklist:
        - "develop"
        - "master"
      additional_modifiers: ''
      allow_detached_head: true
    git_commit_message:
      allow_empty_message: false
      enforce_capitalized_subject: true
      enforce_no_subject_punctuations: false
      enforce_no_subject_trailing_period: true
      enforce_single_lined_subject: true
      type_scope_conventions: []
      max_body_width: 72
      max_subject_width: 60
      matchers:
        Must contain JIRA issue number: /JIRA-\d+ \|/
      case_insensitive: true
      multiline: true
      additional_modifiers: ''
    phan:
      config_file: .phan/config.php
      output_mode: text
      output: null
      triggered_by: 
    phpcs:
      standard: ['PSR2']
      severity: ~
      error_severity: ~
      warning_severity: ~
      tab_width: ~
      report: full
      report_width: ~
      whitelist_patterns: []
      encoding: ~
      ignore_patterns: ['.phan/config.php']
      sniffs: []
      triggered_by: 
      exclude: [
      ]

In the project folder run below commands

php ./vendor/bin/grumphp git:init
git add .
git commit -m "test"

You will see Grumphp face, smiling or sad.

for more details about grumphp

https://github.com/phpro/grumphp

Kategori: php

Yorumlar

Bir cevap yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir