Laravel Factory Generics With Psalm

774 views Asked by At

I'm struggling to get the generic to work for the Laravel 9 factories with psalm.

Doc block:

/**
 * @extends Factory<Trip>
 */
class TripFactory extends Factory { ... }

Use case:

TripFactory::new()->create();

Error:

InvalidTemplateParam - app/Domains/Trips/Seeders/TripSeeder.php:23:9 - Extended template param TModel of Illuminate\Database\Eloquent\Collection<int, Illuminate\Database\Eloquent\Model> expects type Illuminate\Database\Eloquent\Model, type int given (see https://psalm.dev/183)

Where am I going wrong?

1

There are 1 answers

0
lwohlhart On

I was also dealing with the same issue. Which vimeo/psalm version are you using currently?

There's seems to be a fix for this in newer vimeo/psalm releases.

I was running:

"vimeo/psalm": "4.23.0"

after upgrading to 4.30.0 ( presumably also ^5.x should be fine )

"vimeo/psalm": "4.30.0"

the issue is resolved.

I can't pinpoint it exactly but i guess it's due to the fix in v4.25.0 which states:

Improve inferring the "final" static type when calling static methods inside a different class by @someniatko in #8249

This definitely seems to deal with this type of issue and I have to stop now because it's a deep rabbit hole ;-)