How to say phpstan that class is replaced by child

Viewed 144

I have a laravel package for logging. It's used like this.

activity()
   ->performedOn($model)
   ->causedBy($user)
   ->log('Look, I logged something');

The activity() helper returns the vendor class ActivityLogger which is take from the container by class name.

I have to add additional method to vendor logger, so code will look like:

activity()
   ->performedOn($model)
   ->causedBy($user)
   ->relates('#123') // new decorator
   ->log('Look, I logged something');

I've created a child class and replaced in container the vendor class by new one. Everything works ok, except phpstan complaints about relates method not found in ActivityLogger.

Can I say phpstan that for my application, ActivityLogger is replaced by MyActivityLogger ?

1 Answers

I guess you can use stub files to overwrite the return type of activity function.

stubs/activity.stub:

/**
 * @phpstan-param string $logName
 * @phpstan-return YOUR_CUSTOM_OBJECT_TYPE_HERE
 */
function activity(string $logName = null) {}

then in your phpstan.neon config file:

parameters:
    stubFiles:
        - stubs/activity.stub
Related