laravel-enum icon indicating copy to clipboard operation
laravel-enum copied to clipboard

Error when running PHPstan on a Model using `QueriesFlaggedEnums` trait

Open pacoorozco opened this issue 2 years ago • 2 comments

I'm using the QueriesFlaggedEnums trait in the Badge model. I've defined a local scope which use the hasAnyFlags(). PHPstan raises an error like:

------ ---------------------------------------------------------------------------------- 
  Line   app/Models/Badge.php                                                              
 ------ ---------------------------------------------------------------------------------- 
  109    Call to an undefined method Illuminate\Database\Eloquent\Builder::hasAnyFlags().  
 ------ ---------------------------------------------------------------------------------- 

This is the Laravel code:

107:    public function scopeWithActuatorsIn(Builder $query, array $actuators): Builder
107:    {
109:        return $query
110:            ->where('active', true)
111:            ->hasAnyFlags('actuators', $actuators);
112:    }

Someone was suggesting the use of PHP generics in order to avoid the problem. I've tried this approach using an stub file but nothing has changed.

The code could be find here: https://github.com/pacoorozco/gamify-laravel

Related to: nunomaduro/larastan#1307

pacoorozco avatar Jul 06 '22 06:07 pacoorozco

Have you tried adding generics to your own code? I think we should try and add them to this library too, but without having them in your code there is nothing to work with. This library cannot magically conjure up a connection from any Builder object to one where the generic TModel is provided.

spawnia avatar Jul 06 '22 08:07 spawnia

You have shared a very good point. I didn't think about it, let me try it. 🧑‍🏭

pacoorozco avatar Jul 06 '22 09:07 pacoorozco

Closing, as I no longer plan to develop this library further - see https://github.com/BenSampo/laravel-enum/issues/332.

spawnia avatar Feb 14 '24 14:02 spawnia