8000 Makes database factories generic (#39169) · laravel/framework@760d705 · GitHub
[go: up one dir, main page]

Skip to content

Commit 760d705

Browse files
authored
Makes database factories generic (#39169)
1 parent 438eba7 commit 760d705

File tree

3 files changed

+213
-36
lines changed

3 files changed

+213
-36
lines changed

src/Illuminate/Database/Console/Factories/stubs/factory.stub

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ namespace {{ factoryNamespace }};
55
use Illuminate\Database\Eloquent\Factories\Factory;
66
use {{ namespacedModel }};
77

8+
/**
9+
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\{{ namespacedModel }}>
10+
*/
811
class {{ factory }}Factory extends Factory
912
{
1013
/**

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

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
use Illuminate\Support\Traits\Macroable;
1515
use Throwable;
1616

17+
/**
18+
* @template TModel of \Illuminate\Database\Eloquent\Model
19+
*/
1720
abstract class Factory
1821
{
1922
use ForwardsCalls, Macroable {
@@ -23,7 +26,7 @@ abstract class Factory
2326
/**
2427
* The name of the factory's corresponding model.
2528
*
26-
* @var string
29+
* @var class-string<\Illuminate\Database\Eloquent\Model|TModel>
2730
*/
2831
protected $model;
2932

@@ -137,14 +140,14 @@ public function __construct($count = null,
137140
/**
138141
* Define the model's default state.
139142
*
140-
* @return array
143+
* @return array<string, mixed>
141144
*/
142145
abstract public function definition();
143146

144147
/**
145148
* Get a new factory instance for the given attributes.
146149
*
147-
* @param callable|array $attributes
150+
* @param (callable(): array<string, mixed>)|array<string, mixed> $attributes
148151
* @return static
149152
*/
150153
public static function new($attributes = [])
@@ -176,9 +179,9 @@ public function configure()
176179
/**
177180
* Get the raw attributes generated by the factory.
178181
*
179-
* @param array $attributes
182+
* @param array<string, mixed> $attributes
180183
* @param \Illuminate\Database\Eloquent\Model|null $parent
181-
* @return array
184+
* @return array<int|string, mixed>
182185
*/
183186
public function raw($attributes = [], ?Model $parent = null)
184187
{
@@ -194,8 +197,8 @@ public function raw($attributes = [], ?Model $parent = null)
194197
/**
195198
* Create a single model and persist it to the database.
196199
*
197-
* @param array $attributes
198-
* @return \Illuminate\Database\Eloquent\Model
200+
* @param array<string, mixed> $attributes
201+
* @return \Illuminate\Database\Eloquent\Model|TModel
199202
*/
200203
public function createOne($attributes = [])
201204
{
@@ -205,8 +208,8 @@ public function createOne($attributes = [])
205208
/**
206209
* Create a single model and persist it to the database.
207210
*
208-
* @param array $attributes
209-
* @return \Illuminate\Database\Eloquent\Model
211+
* @param array<string, mixed> $attributes
212+
* @return \Illuminate\Database\Eloquent\Model|TModel
210213
*/
211214
public function createOneQuietly($attributes = [])
212215
{
@@ -216,8 +219,8 @@ public function createOneQuietly($attributes = [])
216219
/**
217220
* Create a collection of models and persist them to the database.
218221
*
219-
* @param iterable $records
220-
* @return \Illuminate\Database\Eloquent\Collection
222+
* @param iterable<int, array<string, mixed>> $records
223+
* @return \Illuminate\Database\Eloquent\Collection<int, \Illuminate\Database\Eloquent\Model|TModel>
221224
*/
222225
public function createMany(iterable $records)
223226
{
@@ -231,8 +234,8 @@ public function createMany(iterable $records)
231234
/**
232235
* Create a collection of models and persist them to the database.
233236
*
234-
* @param iterable $records
235-
* @return \Illuminate\Database\Eloquent\Collection
237+
* @param iterable<int, array<string, mixed>> $records
238+
* @return \Illuminate\Database\Eloquent\Collection<int, \Illuminate\Database\Eloquent\Model|TModel>
236239
*/
237240
public function createManyQuietly(iterable $records)
238241
{
@@ -244,9 +247,9 @@ public function createManyQuietly(iterable $records)
244247
/**
245248
* Create a collection of models and persist them to the database.
246249
*
247-
* @param array $attributes
250+
* @param array<string, mixed> $attributes
248251
* @param \Illuminate\Database\Eloquent\Model|null $parent
249-
* @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model
252+
* @return \Illuminate\Database\Eloquent\Collection<int, \Illuminate\Database\Eloquent\Model|TModel>|\Illuminate\Database\Eloquent\Model|TModel
250253
*/
251254
public function create($attributes = [], ?Model $parent = null)
252255
{
@@ -272,9 +275,9 @@ public function create($attributes = [], ?Model $parent = null)
272275
/**
273276
* Create a collection of models and persist them to the database.
274277
*
275-
* @param array $attributes
278+
* @param array<string, mixed> $attributes
276279
* @param \Illuminate\Database\Eloquent\Model|null $parent
277-
* @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model
280+
* @return \Illuminate\Database\Eloquent\Collection<int, \Illuminate\Database\Eloquent\Model|TModel>|\Illuminate\Database\Eloquent\Model|TModel
278281
*/
279282
public function createQuietly($attributes = [], ?Model $parent = null)
280283
{
@@ -286,9 +289,9 @@ public function createQuietly($attributes = [], ?Model $parent = null)
286289
/**
287290
* Create a callback that persists a model in the database when invoked.
288291
*
289-
* @param array $attributes
292+
* @param array<string, mixed> $attributes
290293
* @param \Illuminate\Database\Eloquent\Model|null $parent
291-
* @return \Closure
294+
* @return \Closure(): (\Illuminate\Database\Eloquent\Collection<int, \Illuminate\Database\Eloquent\Model|TModel>|\Illuminate\Database\Eloquent\Model|TModel)
292295
*/
293296
public function lazy(array $attributes = [], ?Model $parent = null)
294297
{
@@ -334,8 +337,8 @@ protected function createChildren(Model $model)
334337
/**
335338
* Make a single instance of the model.
336339
*
337-
* @param callable|array $attributes
338-
* @return \Illuminate\Database\Eloquent\Model
340+
* @param (callable(): array<string, mixed>)|array<string, mixed> $attributes
341+
* @return \Illuminate\Database\Eloquent\Model|TModel
339342
*/
340343
public function makeOne($attributes = [])
341344
{
@@ -345,9 +348,9 @@ public function makeOne($attributes = [])
345348
/**
346349
* Create a collection of models.
347350
*
348-
* @param array $attributes
351+
* @param array<string, mixed> $attributes
349352
* @param \Illuminate\Database\Eloquent\Model|null $parent
350-
* @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model
353+
* @return \Illuminate\Database\Eloquent\Collection<int, \Illuminate\Database\Eloquent\Model|TModel>|\Illuminate\Database\Eloquent\Model|TModel
351354
*/
352355
public function make($attributes = [], ?Model $parent = null)
353356
{
@@ -465,7 +468,7 @@ protected function expandAttributes(array $definition)
465468
/**
466469
* Add a new state transformation to the model definition.
467470
*
468-
* @param callable|array $state
471+
* @param (callable(): array<string, mixed>)|array<string, mixed> $state
469472
* @return static
470473
*/
471474
public function state($state)
@@ -523,7 +526,7 @@ protected function guessRelationship(string $related)
523526
* Define an attached relationship for the model.
524527
*
525528
* @param \Illuminate\Database\Eloquent\Factories\Factory|\Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model $factory
526-
* @param callable|array $pivot
529+
* @param (callable(): array<string, mixed>)|array<string, mixed> $pivot
527530
* @param string|null $relationship
528531
* @return static
529532
*/
@@ -562,7 +565,7 @@ public function for($factory, $relationship = null)
562565
/**
563566
* Add a new "after making" callback to the model definition.
564567
*
565-
* @param \Closure $callback
568+
* @param \Closure(\Illuminate\Database\Eloquent\Model|TModel): mixed $callback
566569
* @return static
567570
*/
568571
public function afterMaking(Closure $callback)
@@ -573,7 +576,7 @@ public function afterMaking(Closure $callback)
573576
/**
574577
* Add a new "after creating" callback to the model definition.
575578
*
576-
* @param \Closure $callback
579+
* @param \Closure(\Illuminate\Database\Eloquent\Model|TModel): mixed $callback
577580
* @return static
578581
*/
579582
public function afterCreating(Closure $callback)
@@ -656,8 +659,8 @@ protected function newInstance(array $arguments = [])
656659
/**
657660
* Get a new model instance.
658661
*
659-
* @param array $attributes
660-
* @return \Illuminate\Database\Eloquent\Model
662+
* @param array<string, mixed> $attributes
663+
* @return \Illuminate\Database\Eloquent\Model|TModel
661664
*/
662665
public function newModel(array $attributes = [])
663666
{
@@ -669,7 +672,7 @@ public function newModel(array $attributes = [])
669672
/**
670673
* Get the name of the model that is generated by the factory.
671674
*
672-
* @return string
675+
* @return class-string<\Illuminate\Database\Eloquent\Model|TModel>
673676
*/
674677
public function modelName()
675678
{
@@ -689,7 +692,7 @@ public function modelName()
689692
/**
690693
* Specify the callback that should be invoked to guess model names based on factory names.
691694
*
692-
* @param callable $callback
695+
* @param callable(): class-string<\Illuminate\Database\Eloquent\Model|TModel> $callback
693696
* @return void
694697
*/
695698
public static function guessModelNamesUsing(callable $callback)
@@ -711,8 +714,8 @@ public static function useNamespace(string $namespace)
711714
/**
712715
* Get a new factory instance for the given model name.
713716
*
714-
* @param string $modelName
715-
* @return static
717+
* @param class-string<\Illuminate\Database\Eloquent\Model> $modelName
718+
* @return \Illuminate\Database\Eloquent\Factories\Factory
716719
*/
717720
public static function factoryForModel(string $modelName)
718721
{
@@ -724,7 +727,7 @@ public static function factoryForModel(string $modelName)
724727
/**
725728
* Specify the callback that should be invoked to guess factory names based on dynamic relationship names.
726729
*
727-
* @param callable $callback
730+
* @param callable(): class-string<\Illuminate\Database\Eloquent\Model|TModel> $callback
728731
* @return void
729732
*/
730733
public static function guessFactoryNamesUsing(callable $callback)
@@ -745,8 +748,8 @@ protected function withFaker()
745748
/**
746749
* Get the factory name for the given model name.
747750
*
748-
* @param string $modelName
749-
* @return string
751+
* @param class-string<\Illuminate\Database\Eloquent\Model> $modelName
752+
* @return class-string<\Illuminate\Database\Eloquent\Factories\Factory>
750753
*/
751754
public static function resolveFactoryName(string $modelName)
752755
{

0 commit comments

Comments
 (0)
0