Skip to content

Commit

Permalink
Merge pull request #40902 from axlon/patch-3
Browse files Browse the repository at this point in the history
[9.x] Improve types on model factory methods
  • Loading branch information
nunomaduro authored Feb 10, 2022
2 parents 589f55d + e22ec12 commit 0feef93
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
16 changes: 8 additions & 8 deletions src/Illuminate/Database/Eloquent/Factories/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ abstract public function definition();
/**
* Get a new factory instance for the given attributes.
*
* @param (callable(): array<string, mixed>)|array<string, mixed> $attributes
* @param (callable(array<string, mixed>): array<string, mixed>)|array<string, mixed> $attributes
* @return static
*/
public static function new($attributes = [])
Expand Down Expand Up @@ -180,7 +180,7 @@ public function configure()
/**
* Get the raw attributes generated by the factory.
*
* @param array<string, mixed> $attributes
* @param (callable(array<string, mixed>): array<string, mixed>)|array<string, mixed> $attributes
* @param \Illuminate\Database\Eloquent\Model|null $parent
* @return array<int|string, mixed>
*/
Expand All @@ -198,7 +198,7 @@ public function raw($attributes = [], ?Model $parent = null)
/**
* Create a single model and persist it to the database.
*
* @param array<string, mixed> $attributes
* @param (callable(array<string, mixed>): array<string, mixed>)|array<string, mixed> $attributes
* @return \Illuminate\Database\Eloquent\Model|TModel
*/
public function createOne($attributes = [])
Expand All @@ -209,7 +209,7 @@ public function createOne($attributes = [])
/**
* Create a single model and persist it to the database.
*
* @param array<string, mixed> $attributes
* @param (callable(array<string, mixed>): array<string, mixed>)|array<string, mixed> $attributes
* @return \Illuminate\Database\Eloquent\Model|TModel
*/
public function createOneQuietly($attributes = [])
Expand Down Expand Up @@ -248,7 +248,7 @@ public function createManyQuietly(iterable $records)
/**
* Create a collection of models and persist them to the database.
*
* @param array<string, mixed> $attributes
* @param (callable(array<string, mixed>): array<string, mixed>)|array<string, mixed> $attributes
* @param \Illuminate\Database\Eloquent\Model|null $parent
* @return \Illuminate\Database\Eloquent\Collection<int, \Illuminate\Database\Eloquent\Model|TModel>|\Illuminate\Database\Eloquent\Model|TModel
*/
Expand Down Expand Up @@ -338,7 +338,7 @@ protected function createChildren(Model $model)
/**
* Make a single instance of the model.
*
* @param (callable(): array<string, mixed>)|array<string, mixed> $attributes
* @param (callable(array<string, mixed>): array<string, mixed>)|array<string, mixed> $attributes
* @return \Illuminate\Database\Eloquent\Model|TModel
*/
public function makeOne($attributes = [])
Expand All @@ -349,7 +349,7 @@ public function makeOne($attributes = [])
/**
* Create a collection of models.
*
* @param array<string, mixed> $attributes
* @param (callable(array<string, mixed>): array<string, mixed>)|array<string, mixed> $attributes
* @param \Illuminate\Database\Eloquent\Model|null $parent
* @return \Illuminate\Database\Eloquent\Collection<int, \Illuminate\Database\Eloquent\Model|TModel>|\Illuminate\Database\Eloquent\Model|TModel
*/
Expand Down Expand Up @@ -469,7 +469,7 @@ protected function expandAttributes(array $definition)
/**
* Add a new state transformation to the model definition.
*
* @param (callable(): array<string, mixed>)|array<string, mixed> $state
* @param (callable(array<string, mixed>): array<string, mixed>)|array<string, mixed> $state
* @return static
*/
public function state($state)
Expand Down
32 changes: 30 additions & 2 deletions types/Database/Eloquent/Factories/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ public function definition()
}
}

$factory = UserFactory::new();
assertType('UserFactory', $factory);
assertType('UserFactory', $factory = UserFactory::new());
assertType('UserFactory', UserFactory::new(['string' => 'string']));
assertType('UserFactory', UserFactory::new(function ($attributes) {
// assertType('array<string, mixed>', $attributes);
return ['string' => 'string'];
}));

assertType('array<string, mixed>', $factory->definition());

Expand All @@ -39,16 +43,28 @@ public function definition()

assertType('array<int|string, mixed>', $factory->raw());
assertType('array<int|string, mixed>', $factory->raw(['string' => 'string']));
assertType('array<int|string, mixed>', $factory->raw(function ($attributes) {
// assert('array<string, mixed>', $attributes);
return ['string' => 'string'];
}));

// assertType('User', $factory->createOne());
// assertType('User', $factory->createOne(['string' => 'string']));
assertType('Illuminate\Database\Eloquent\Model', $factory->createOne());
assertType('Illuminate\Database\Eloquent\Model', $factory->createOne(['string' => 'string']));
assertType('Illuminate\Database\Eloquent\Model', $factory->createOne(function ($attributes) {
// assertType('array<string, mixed>', $attributes);
return ['string' => 'string'];
}));

// assertType('User', $factory->createOneQuietly());
// assertType('User', $factory->createOneQuietly(['string' => 'string']));
assertType('Illuminate\Database\Eloquent\Model', $factory->createOneQuietly());
assertType('Illuminate\Database\Eloquent\Model', $factory->createOneQuietly(['string' => 'string']));
assertType('Illuminate\Database\Eloquent\Model', $factory->createOneQuietly(function ($attributes) {
// assertType('array<string, mixed>', $attributes);
return ['string' => 'string'];
}));

// assertType('Illuminate\Database\Eloquent\Collection<int, User>', $factory->createMany([['string' => 'string']]));
assertType('Illuminate\Database\Eloquent\Collection<int, Illuminate\Database\Eloquent\Model>', $factory->createMany(
Expand All @@ -68,6 +84,10 @@ public function definition()
assertType('Illuminate\Database\Eloquent\Collection<int, Illuminate\Database\Eloquent\Model>|Illuminate\Database\Eloquent\Model', $factory->create([
'string' => 'string',
]));
assertType('Illuminate\Database\Eloquent\Collection<int, Illuminate\Database\Eloquent\Model>|Illuminate\Database\Eloquent\Model', $factory->create(function ($attributes) {
// assertType('array<string, mixed>', $attributes);
return ['string' => 'string'];
}));

// assertType('Illuminate\Database\Eloquent\Collection<int, User>|User', $factory->createQuietly());
// assertType('Illuminate\Database\Eloquent\Collection<int, User>|User', $factory->createQuietly([
Expand Down Expand Up @@ -95,6 +115,10 @@ public function definition()
assertType('Illuminate\Database\Eloquent\Model', $factory->makeOne([
'string' => 'string',
]));
assertType('Illuminate\Database\Eloquent\Model', $factory->makeOne(function ($attributes) {
// assert('array<string, mixed>', $attributes);
return ['string' => 'string'];
}));

// assertType('Illuminate\Database\Eloquent\Collection<int, User>|User', $factory->make());
// assertType('Illuminate\Database\Eloquent\Collection<int, User>|User', $factory->make([
Expand All @@ -104,6 +128,10 @@ public function definition()
assertType('Illuminate\Database\Eloquent\Collection<int, Illuminate\Database\Eloquent\Model>|Illuminate\Database\Eloquent\Model', $factory->make([
'string' => 'string',
]));
assertType('Illuminate\Database\Eloquent\Collection<int, Illuminate\Database\Eloquent\Model>|Illuminate\Database\Eloquent\Model', $factory->make(function ($attributes) {
// assert('array<string, mixed>', $attributes);
return ['string' => 'string'];
}));

assertType('UserFactory', $factory->state(['string' => 'string']));
assertType('UserFactory', $factory->state(function () {
Expand Down

0 comments on commit 0feef93

Please sign in to comment.