Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

garrettmassey's avatar

PHPStan on Github Actions for Laravel Project failing on Composer Install

I'm working on setting up static analysis for our laravel app that runs when someone pushes to the main branch or a PR is accepted, but I keep getting SQL connection errors, and it has something to do with the artisan package-discover script that runs after composer installs the dependencies.

The full error is here:

Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi

   Illuminate\Database\QueryException 

  SQLSTATE[HY000] [2002] Connection refused (Connection: mysql, SQL: select table_name as `name`, (data_length + index_length) as `size`, table_comment as `comment`, engine as `engine`, table_collation as `collation` from information_schema.tables where table_schema = 'forge' and table_type in ('BASE TABLE', 'SYSTEM VERSIONED') order by table_name)

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:829
    825▕                     $this->getName(), $query, $this->prepareBindings($bindings), $e
    826▕                 );
    827▕             }
    828▕ 
  ➜ 829▕             throw new QueryException(
    830▕                 $this->getName(), $query, $this->prepareBindings($bindings), $e
    831▕             );
    832▕         }
    833▕     }

  1   [internal]:0
      Illuminate\Foundation\Application::Illuminate\Foundation\{closure}()
      +19 vendor frames 

  21  nova-components/Modules/src/Support/Manager.php:26
      Illuminate\Support\Facades\Facade::__callStatic()

Script @php artisan package:discover --ansi handling the post-autoload-dump event returned with error code 1
Error: Process completed with exit code 1.

and here is my .yml file:

name: PHPStan

on:
    push:
        branches: [main]
    pull_request:
        branches: [main]

jobs:
  phpstan:
    name: phpstan
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
 
      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: '8.2'
          coverage: none
 
      - name: Install composer dependencies
        run: composer install  --no-interaction --no-progress --prefer-dist
 
      - name: Run Static Analysis
        run: ./vendor/bin/phpstan --error-format=github

I don't know why it seems to require a database connection. I also added the --no-scripts flag to the composer install command, and then got this error instead:

Run ./vendor/bin/phpstan --error-format=github
  
Note: Using configuration file /home/runner/work/intranet/intranet/phpstan.neon.
Illuminate\Database\QueryException thrown in /home/runner/work/intranet/intranet/vendor/laravel/framework/src/Illuminate/Database/Connection.php on line 829 while loading bootstrap file /home/runner/work/intranet/intranet/vendor/larastan/larastan/bootstrap.php: SQLSTATE[HY000] [2002] Connection refused (Connection: mysql, SQL: select table_name as `name`, (data_length + index_length) as `size`, table_comment as `comment`, engine as `engine`, table_collation as `collation` from information_schema.tables where table_schema = 'forge' and table_type in ('BASE TABLE', 'SYSTEM VERSIONED') order by table_name)
Error: Process completed with exit code 1.

So with the --no-scripts flag, composer successfully installs, but phpstan doesn't run, and without the --no-scripts phpstan never runs and composer fails. Both of the errors seem to be referencing SQL connections.

0 likes
1 reply
LaryAI's avatar
Level 58

The issue you're encountering is related to the fact that some parts of your Laravel application are trying to connect to a database during the Composer install process or when running PHPStan. This is likely due to some service providers or packages that are being loaded and require a database connection.

Here are a few steps you can take to resolve this issue:

  1. Use a SQLite In-Memory Database for Testing: Modify your phpunit.xml or .env file to use an in-memory SQLite database when running tests or static analysis. This way, you won't need a real database connection.

    Update your .env or phpunit.xml:

    DB_CONNECTION=sqlite
    DB_DATABASE=:memory:
    
  2. Disable Service Providers in Testing Environment: If certain service providers are causing the issue, you can conditionally disable them in your AppServiceProvider or directly in the config/app.php file based on the environment.

    In AppServiceProvider:

    public function register()
    {
        if ($this->app->environment('testing')) {
            // Disable specific service providers or packages
            $this->app->register(SomeServiceProvider::class, false);
        }
    }
    
  3. Use --no-scripts and Mock Database Connections: Since using --no-scripts allows Composer to install without running into the database issue, you can mock or disable database connections when running PHPStan.

    In your phpstan.neon or phpstan.neon.dist, you can configure PHPStan to ignore certain paths or classes that require a database connection.

  4. Ensure Environment Variables are Set Correctly: Make sure your GitHub Actions workflow sets the necessary environment variables for the database connection. You can set these in the env section of your workflow file.

    jobs:
      phpstan:
        name: phpstan
        runs-on: ubuntu-latest
        env:
          DB_CONNECTION: sqlite
          DB_DATABASE: ':memory:'
        steps:
          - uses: actions/checkout@v3
          - name: Setup PHP
            uses: shivammathur/setup-php@v2
            with:
              php-version: '8.2'
              coverage: none
          - name: Install composer dependencies
            run: composer install --no-interaction --no-progress --prefer-dist --no-scripts
          - name: Run Static Analysis
            run: ./vendor/bin/phpstan --error-format=github
    
  5. Check for Database Calls in Bootstrap Files: Ensure that no database calls are being made in any bootstrap files or during the application bootstrapping process. This includes checking for any eager-loaded configurations or services that might be making database queries.

By following these steps, you should be able to resolve the database connection issue during your GitHub Actions workflow for PHPStan.

Please or to participate in this conversation.