psalm / psalm-plugin-laravel

A Psalm plugin for Laravel
MIT License
302 stars 72 forks source link

Binding job dependency injection causes psalm to crash #128

Closed neclimdul closed 3 years ago

neclimdul commented 3 years ago

Description Binding job handle dependencies causes psalm to crash. The crash matches this earlier bug report #125.

From the documentation, if you need to specify how dependencies get injected to your job's handle method you need to use the bindMethod container method. This is a requirement if you have named services for any reason that can't be automatically resolved. https://laravel.com/docs/8.x/queues#handle-method-dependency-injection

The problem seems to be this method doesn't exist on the container interface(hinted in the ServiceProvider abstract parent class) but it does exist on the actual container object. I'm not sure why this causes a crash instead of a failure but that is the behavior.

An example provider:

<?php
namespace App\Providers;

use App\Jobs\PullContact;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider {

    /**
     * {@inheritDoc}
     */
    public function register() {
        $this->app->bindMethod(PullContact::class . '@handle', function ($job, $app) {
            return $job->handle($app->get('realservice'));
        });
     }
}

Impacted Versions my composer output as required. Though this binding behavior reaches well back into the 5.x releases of Laravel if not earlier.

barryvdh/laravel-ide-helper           v2.9.0    Laravel IDE Helper, generates correct PHPDocs for all Facade classes, to improve auto-completion.
laravel/framework                     v8.29.0   The Laravel Framework.
laravel/tinker                        v2.6.0    Powerful REPL for the Laravel framework.
psalm/plugin-laravel                  v1.4.2    A Laravel plugin for Psalm
vimeo/psalm                           4.6.1     A static analysis tool for finding errors in PHP applications
neclimdul commented 3 years ago

FWIW if you're running into this I worked around it by adding this to my service provider. Obviously it would be better if it worked out of the box or provided a failure and some useful feedback instead of crashing out entirely though.


    /**
     * The application instance.
     *
     * @var \Illuminate\Container\Container
     */
    protected $app;