Usage of PhpStan on Github via Pull Request for Drupal Applications

ByKarthik Kumar D Kon5th Jul 2022, 2022-12-01T08:00:00+05:30
Read Article
Usage of PhpStan on Github via Pull Request for Drupal Applications

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


Add the below code to the ci.yml file.

name: pr-review
on: [pull_request]
    runs-on: ubuntu-latest
     - name: Clone the code
       uses: actions/checkout@v3
     - name: Setup PHP 7.4
       uses: shivamathur/setup-php@v2
          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.

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