Libraries will make people's lives easier during the development

10th Feb 2023
Check Drupal coding standard by phpcs in git pre-commit hook
Table of contents

In this article, we are going to see how some tools & libraries will make people's lives easier during the development & code review process.

And to make developer life easier, developers look for tools or libraries which can automated code review and if needed make any corrections in the code automatically. Here comes the PHP codesniffer and Drupal coder module.

>> Tập tành tìm hiểu Headless and Hybrid Drupal 101

>> Here are the examples from Caching Deep Dive 2.0

If you are maintaining your code in Github, then Github is providing continuous integration support within their workflows.

It's pretty simple, if we already know PHPCS and PHPCBF libraries are used to do the code reviews and checks on your local machines and the same thing can be done once the pull request is raised by the developer.

Having the code standard checks done at this level (on PR create level), will ensure the project is maintained with proper drupal coding standards.

To do this, you need to create a folder structure like .github/workflows within your project (if not created) and Create a file ci.yml within the folder, once created the file path will look as seen below

.github/workflows/ci.yml

Add the below code to the ci.yml file.

name: pr-review
on: [pull_request]
jobs:
    check-drupal-code-standards:
        runs-on: ubuntu-latest
        steps:
        - name: Install PHPCS
          run: composer global require –dev drupal/coder dealerdirect/php codesniffer-composer-installer
        - name: Clone code
          run: actions/checkout@v3
        - name: Check drupal coding standards
          run: ~/.composer/vendor/bin/phpcs –standard=”Drupal,DrupalPractice” –extensions=”php,module,inc,install,test,profile,theme,css,info”

The YML file is configured to trigger a job upon the pull request creation.

  • Where this job run’s on the Ubuntu machine.
  • This job has 3 steps
    • To install PHPCS
    • To clone the code
    • To run the phpcs checks (like we run locally)

Once this ci.yml is pushed to your github repository, then from next time onwards if any PR is created then this automatic checks will be triggered and will show the pass or fail data on the PR itself.

By this, One level of coding standard check is performed before you push the code to master branch or branch which has the stable code.

Bạn thấy bài viết này như thế nào?
0 reactions

Add new comment

Image CAPTCHA
Enter the characters shown in the image.

Related Articles

Master list (in progress) of how to get parts of fields for use in Twig templates. I’m always having to look these up, so I thought I’d hash them out and write them down.

Litespeed Cache là plugin WordPress dùng để kết hợp với Web Server LiteSpeed nhằm tăng tốc website WordPress của bạn gấp nhiều lần

In this tutorial, you will learn how to improve the custom code, theme and module, and general code development by using the pre-commit hook on git

Trước khi tìm hiểu xem PHP Code Sniffer là gì thì các bạn cần phải nắm được coding convention là gì đã.

Hướng dẫn từng bước set up một project PHP theo mô hình MVC. Bạn có thể sử dụng source này để tiết kiệm thời gian set up cho project của mình.