Skip to content

Commit 74af761

Browse files
authored
fix: Factory::state and ::prependState generics (#55915)
The model passed to the closure is not the generic type TModel, but rather the parent model (if any). Because we can't easily determine the type of the parent model, we just use the base Model.
1 parent a2d7dfc commit 74af761

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

src/Illuminate/Database/Eloquent/Factories/Factory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ protected function expandAttributes(array $definition)
518518
/**
519519
* Add a new state transformation to the model definition.
520520
*
521-
* @param (callable(array<string, mixed>, TModel|null): array<string, mixed>)|array<string, mixed> $state
521+
* @param (callable(array<string, mixed>, Model|null): array<string, mixed>)|array<string, mixed> $state
522522
* @return static
523523
*/
524524
public function state($state)
@@ -533,7 +533,7 @@ public function state($state)
533533
/**
534534
* Prepend a new state transformation to the model definition.
535535
*
536-
* @param (callable(array<string, mixed>, TModel|null): array<string, mixed>)|array<string, mixed> $state
536+
* @param (callable(array<string, mixed>, Model|null): array<string, mixed>)|array<string, mixed> $state
537537
* @return static
538538
*/
539539
public function prependState($state)

types/Database/Eloquent/Factories/Factory.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@ public function definition(): array
1616
}
1717
}
1818

19+
/** @extends Illuminate\Database\Eloquent\Factories\Factory<Post> */
20+
class PostFactory extends Factory
21+
{
22+
protected $model = Post::class;
23+
24+
/** @return array<string, mixed> */
25+
public function definition(): array
26+
{
27+
return [];
28+
}
29+
}
30+
1931
assertType('UserFactory', $factory = UserFactory::new());
2032
assertType('UserFactory', UserFactory::new(['string' => 'string']));
2133
assertType('UserFactory', UserFactory::new(function ($attributes) {
@@ -105,7 +117,7 @@ public function definition(): array
105117
}));
106118
assertType('UserFactory', $factory->state(function ($attributes, $model) {
107119
assertType('array<string, mixed>', $attributes);
108-
assertType('User|null', $model);
120+
assertType('Illuminate\Database\Eloquent\Model|null', $model);
109121

110122
return ['string' => 'string'];
111123
}));
@@ -164,3 +176,19 @@ public function definition(): array
164176
default => throw new LogicException('Unknown factory'),
165177
};
166178
});
179+
180+
UserFactory::new()->has(
181+
PostFactory::new()
182+
->state(function ($attributes, $user) {
183+
assertType('array<string, mixed>', $attributes);
184+
assertType('Illuminate\Database\Eloquent\Model|null', $user);
185+
186+
return ['user_id' => $user?->getKey()];
187+
})
188+
->prependState(function ($attributes, $user) {
189+
assertType('array<string, mixed>', $attributes);
190+
assertType('Illuminate\Database\Eloquent\Model|null', $user);
191+
192+
return ['user_id' => $user?->getKey()];
193+
}),
194+
);

0 commit comments

Comments
 (0)