Usage of PhpStan on Github via Pull Request for Drupal Applications

Usage of PhpStan on Github via Pull Request for Drupal Applications

On5th Jul 2022, 2024-12-13T17:24:47+05:30 ByKarthik Kumar D K | read
Listen Pause Resume Stop

In this article, we are going to see how some tools & libraries will make people's lives easier during the development and code review process. We have a similar helpful article related to Phpcs, have a check of this.

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 PhpStan, a static analysing tool which primarily focuses on finding errors in your code without actually running.

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

We could run this locally as well by having the PhpStan library, which will help developers to make sure none of the issues will go to the higher environment.

Also, 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:
  phpstan-report:
    runs-on: ubuntu-latest
    steps:
     - name: Clone the code
       uses: actions/checkout@v3
     - name: Setup PHP 7.4
       uses: shivamathur/setup-php@v2
       with:
          php-version: 7.4
     - name: Composer install
       run: composer install
     - name: Analyse codebase
       run: ./vendor/bin/phpstan analyse ./web/modules/custom

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

  1. Where this job run’s on the Ubuntu machine.
  2. This job has 4 steps
    1. To clone the code
    2. To setup the php 7.4 version.
    3. To run the composer install
    4. To run the phpstan 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.

Related Articles

Recent Articles

Recent Quick Read

Recent Great People

We Need Your Consent
By clicking “Accept Cookies”, you agree to the storing of cookies on your device to enhance your site navigation experience.
I Accept Cookies