Skip to content

LARAVEL_STATIC_TO_INJECTION in AppServiceProvider breaks app after injecting dependencies in constructor #315

Open
@gjvanahee

Description

@gjvanahee

Rector proposes the following change, but there is no injection in service providers. The constructor requires an Application instance $app as it's first (and only) argument.

 class AppServiceProvider extends ServiceProvider
 {
+    public function __construct(private readonly \Illuminate\Contracts\Auth\Access\Gate $gate)
+    {
+    }

     public function boot(): void
     {
-        Gate::before(fn ($user, $ability) => $user->email === '***');
+        $this->gate->before(fn ($user, $ability) => $user->email === '***');
     }
 }
    ----------- end diff -----------

The accepted code will throw an exception:

App\Providers\AppServiceProvider::__construct(): Argument # 1 ($gate) must be of type Illuminate\Contracts\Auth\Access\Gate, Illuminate\Foundation\Application given

rector/rector: 2.0.10
driftingly/rector-laravel: 2.0.2
laravel/framework: 11.44.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions