spatie / laravel-webhook-client

Receive webhooks in Laravel apps
https://freek.dev/1383-sending-and-receiving-webhooks-in-laravel-apps
MIT License
1k stars 147 forks source link

Static analysis fails when custom Model is used #181

Closed cxj closed 1 year ago

cxj commented 1 year ago

Execution works fine, but static analysis tools such as PHPStan complain about the variable type when a custom Model is used to decode the JSON payload into an object instead of an array.

The custom MyWebhookCall Model contains:

    protected $casts = [
        'headers'   => 'array',
        'payload'   => 'object',    // override
        'exception' => 'array',
    ];

Instead of the package WebhookCall Model which reads:

    protected $casts = [
        'headers' => 'array',
        'payload' => 'array',
        'exception' => 'array',
    ];

This results in static analysis errors in the ProcessWebhookJob::handle() method because the Spatie ProcessWebhookJob class hard-codes the type of the Model in the constructor:

    public function __construct(
        public WebhookCall $webhookCall
    ) {
    }

For example, the errors I get from PHPStan for my particular implementation of the WebhookCall Model and the ProcessWebhookJob class look like these:

------ -----------------------------------------------------------------------
  Line   MyApp/Jobs/MyWebhookJob.php
 ------ -----------------------------------------------------------------------
  28     Cannot access property $type on array.
  56     Parameter #1 $payload of callable
         Domain\MyApp\ShipmentCreatedAction|Domain\MyApp\ShipmentLegDeli
         veryAction expects object, array|null given.
 ------ -----------------------------------------------------------------------

One possible solution might be declare an interface as the type hint for the constructor to the base ProcessWebhookJob class, and have the Model implement it. There may be other, better solutions.