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:
-
Use a SQLite In-Memory Database for Testing: Modify your
phpunit.xmlor.envfile 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
.envorphpunit.xml:DB_CONNECTION=sqlite DB_DATABASE=:memory: -
Disable Service Providers in Testing Environment: If certain service providers are causing the issue, you can conditionally disable them in your
AppServiceProvideror directly in theconfig/app.phpfile 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); } } -
Use
--no-scriptsand Mock Database Connections: Since using--no-scriptsallows Composer to install without running into the database issue, you can mock or disable database connections when running PHPStan.In your
phpstan.neonorphpstan.neon.dist, you can configure PHPStan to ignore certain paths or classes that require a database connection. -
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
envsection 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 -
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.