larastan: Error when there is no database

  • Larastan Version: 0.6.4
  • --level used: max

Description

I’m using spatie/laravel-permission package in my app. That registers some stuffs in service provider. When i setup a pipeline this error occured.

Similar issue: https://github.com/symfony/symfony/issues/37069

Error Output

------ --------------------------------------------------------------------- 
  Line   Http/Controllers/Api/RoleController.php                              
 ------ --------------------------------------------------------------------- 
         Internal error: SQLSTATE[HY000] [2002] Connection refused (SQL:      
         select column_name as `column_name` from information_schema.columns  
         where table_schema = forge and table_name = roles)                   
         Run PHPStan with --debug option and post the stack trace to:         
         https://github.com/phpstan/phpstan/issues/new                        
 ------ --------------------------------------------------------------------- 
 ------ --------------------------------------------------------------------- 
  Line   Http/Resources/PermissionResource.php                                
 ------ --------------------------------------------------------------------- 
         Internal error: SQLSTATE[HY000] [2002] Connection refused (SQL:      
         select column_name as `column_name` from information_schema.columns  
         where table_schema = forge and table_name = permissions)             
         Run PHPStan with --debug option and post the stack trace to:         
         https://github.com/phpstan/phpstan/issues/new                        
 ------ --------------------------------------------------------------------- 
 ------ --------------------------------------------------------------------- 
  Line   Rules/EmptyRole.php                                                  
 ------ --------------------------------------------------------------------- 
         Internal error: SQLSTATE[HY000] [2002] Connection refused (SQL:      
         select column_name as `column_name` from information_schema.columns  
         where table_schema = forge and table_name = roles)                   
         Run PHPStan with --debug option and post the stack trace to:         
         https://github.com/phpstan/phpstan/issues/new                        
 ------ --------------------------------------------------------------------- 

Laravel code where the issue was found

class PermissionController extends Controller
{
    public function index(): JsonResponse
    {
        $permissions = Permission::all();

        return new JsonResponse([
            'data' => $permissions->pluck('name'),
        ]);
    }
}

Another example

ConnectionFactory reads Salesforce config from .env and creates a soap client.

    public function register(): void
    {
        $this->app->singleton(SforceEnterpriseClient::class, static function (): SforceEnterpriseClient {
            $factory = new ConnectionFactory();

            return $factory->createConnection();
        });
    }

Error Output

 ------ --------------------------------------------------------------------- 
  Line   Salesforce/Services/UserService.php                                  
 ------ --------------------------------------------------------------------- 
         Internal error: INVALID_LOGIN: Invalid username, password, security  
         token; or user locked out.                                           
         Run PHPStan with --debug option and post the stack trace to:         
         https://github.com/phpstan/phpstan/issues/new                        
 ------ --------------------------------------------------------------------- 

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 1
  • Comments: 15 (4 by maintainers)

Most upvoted comments

@canvural That fixed it for me. Thanks!

In work projects, we are extending the \Spatie\Permission\Models\Role model and using our own Role model. So at first, I did not see this error.

But when I explicitly use that model like so:

public function doSomething(\Spatie\Permission\Models\Role $role): Collection
{
    return $role->permissions;
}

it produces the same error as in the original issue.

The error originates from here. The solution I’m thinking is wrapping this statement in try/catch block. And fallback to “guessing” the table name in catch block. Like this:

--- src/Properties/ModelPropertyExtension.php
+++ src/Properties/ModelPropertyExtension.php
@@ -73,9 +73,16 @@
         }
 
         $modelName = $classReflection->getNativeReflection()->getName();
+
         /** @var Model $modelInstance */
-        $modelInstance = new $modelName;
-        $tableName = $modelInstance->getTable();
+        try {
+            $modelInstance = new $modelName;
+
+            $tableName = $modelInstance->getTable();
+        } catch (\Exception $e) {
+            $tableName =  Str::snake(Str::pluralStudly(class_basename($modelName)));
+        }
+
 
         if (! array_key_exists($tableName, $this->tables)) {
             return false;

Or we can just return false from the catch block. Ignoring this model.

Any other suggestions?

Hi,

Can you run PHPStan only on one file, Http/Controllers/Api/RoleController.php for example, with --debug flag and post the stack trace here?

@szepeviktor We still boot up the Laravel.