Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Makes database factories generic #39169

Merged
merged 1 commit into from
Oct 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Illuminate/Database/Console/Factories/stubs/factory.stub
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ namespace {{ factoryNamespace }};
use Illuminate\Database\Eloquent\Factories\Factory;
use {{ namespacedModel }};

/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\{{ namespacedModel }}>
*/
class {{ factory }}Factory extends Factory
{
/**
Expand Down
75 changes: 39 additions & 36 deletions src/Illuminate/Database/Eloquent/Factories/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
use Illuminate\Support\Traits\Macroable;
use Throwable;

/**
* @template TModel of \Illuminate\Database\Eloquent\Model
*/
abstract class Factory
{
use ForwardsCalls, Macroable {
Expand All @@ -23,7 +26,7 @@ abstract class Factory
/**
* The name of the factory's corresponding model.
*
* @var string
* @var class-string<\Illuminate\Database\Eloquent\Model|TModel>
*/
protected $model;

Expand Down Expand Up @@ -137,14 +140,14 @@ public function __construct($count = null,
/**
* Define the model's default state.
*
* @return array
* @return array<string, mixed>
*/
abstract public function definition();

/**
* Get a new factory instance for the given attributes.
*
* @param callable|array $attributes
* @param (callable(): array<string, mixed>)|array<string, mixed> $attributes
* @return static
*/
public static function new($attributes = [])
Expand Down Expand Up @@ -176,9 +179,9 @@ public function configure()
/**
* Get the raw attributes generated by the factory.
*
* @param array $attributes
* @param array<string, mixed> $attributes
* @param \Illuminate\Database\Eloquent\Model|null $parent
* @return array
* @return array<int|string, mixed>
*/
public function raw($attributes = [], ?Model $parent = null)
{
Expand All @@ -194,8 +197,8 @@ public function raw($attributes = [], ?Model $parent = null)
/**
* Create a single model and persist it to the database.
*
* @param array $attributes
* @return \Illuminate\Database\Eloquent\Model
* @param array<string, mixed> $attributes
* @return \Illuminate\Database\Eloquent\Model|TModel
*/
public function createOne($attributes = [])
{
Expand All @@ -205,8 +208,8 @@ public function createOne($attributes = [])
/**
* Create a single model and persist it to the database.
*
* @param array $attributes
* @return \Illuminate\Database\Eloquent\Model
* @param array<string, mixed> $attributes
* @return \Illuminate\Database\Eloquent\Model|TModel
*/
public function createOneQuietly($attributes = [])
{
Expand All @@ -216,8 +219,8 @@ public function createOneQuietly($attributes = [])
/**
* Create a collection of models and persist them to the database.
*
* @param iterable $records
* @return \Illuminate\Database\Eloquent\Collection
* @param iterable<int, array<string, mixed>> $records
* @return \Illuminate\Database\Eloquent\Collection<int, \Illuminate\Database\Eloquent\Model|TModel>
*/
public function createMany(iterable $records)
{
Expand All @@ -231,8 +234,8 @@ public function createMany(iterable $records)
/**
* Create a collection of models and persist them to the database.
*
* @param iterable $records
* @return \Illuminate\Database\Eloquent\Collection
* @param iterable<int, array<string, mixed>> $records
* @return \Illuminate\Database\Eloquent\Collection<int, \Illuminate\Database\Eloquent\Model|TModel>
*/
public function createManyQuietly(iterable $records)
{
Expand All @@ -244,9 +247,9 @@ public function createManyQuietly(iterable $records)
/**
* Create a collection of models and persist them to the database.
*
* @param array $attributes
* @param array<string, mixed> $attributes
* @param \Illuminate\Database\Eloquent\Model|null $parent
* @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model
* @return \Illuminate\Database\Eloquent\Collection<int, \Illuminate\Database\Eloquent\Model|TModel>|\Illuminate\Database\Eloquent\Model|TModel
*/
public function create($attributes = [], ?Model $parent = null)
{
Expand All @@ -272,9 +275,9 @@ public function create($attributes = [], ?Model $parent = null)
/**
* Create a collection of models and persist them to the database.
*
* @param array $attributes
* @param array<string, mixed> $attributes
* @param \Illuminate\Database\Eloquent\Model|null $parent
* @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model
* @return \Illuminate\Database\Eloquent\Collection<int, \Illuminate\Database\Eloquent\Model|TModel>|\Illuminate\Database\Eloquent\Model|TModel
*/
public function createQuietly($attributes = [], ?Model $parent = null)
{
Expand All @@ -286,9 +289,9 @@ public function createQuietly($attributes = [], ?Model $parent = null)
/**
* Create a callback that persists a model in the database when invoked.
*
* @param array $attributes
* @param array<string, mixed> $attributes
* @param \Illuminate\Database\Eloquent\Model|null $parent
* @return \Closure
* @return \Closure(): (\Illuminate\Database\Eloquent\Collection<int, \Illuminate\Database\Eloquent\Model|TModel>|\Illuminate\Database\Eloquent\Model|TModel)
*/
public function lazy(array $attributes = [], ?Model $parent = null)
{
Expand Down Expand Up @@ -334,8 +337,8 @@ protected function createChildren(Model $model)
/**
* Make a single instance of the model.
*
* @param callable|array $attributes
* @return \Illuminate\Database\Eloquent\Model
* @param (callable(): array<string, mixed>)|array<string, mixed> $attributes
* @return \Illuminate\Database\Eloquent\Model|TModel
*/
public function makeOne($attributes = [])
{
Expand All @@ -345,9 +348,9 @@ public function makeOne($attributes = [])
/**
* Create a collection of models.
*
* @param array $attributes
* @param array<string, mixed> $attributes
* @param \Illuminate\Database\Eloquent\Model|null $parent
* @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model
* @return \Illuminate\Database\Eloquent\Collection<int, \Illuminate\Database\Eloquent\Model|TModel>|\Illuminate\Database\Eloquent\Model|TModel
*/
public function make($attributes = [], ?Model $parent = null)
{
Expand Down Expand Up @@ -465,7 +468,7 @@ protected function expandAttributes(array $definition)
/**
* Add a new state transformation to the model definition.
*
* @param callable|array $state
* @param (callable(): array<string, mixed>)|array<string, mixed> $state
* @return static
*/
public function state($state)
Expand Down Expand Up @@ -523,7 +526,7 @@ protected function guessRelationship(string $related)
* Define an attached relationship for the model.
*
* @param \Illuminate\Database\Eloquent\Factories\Factory|\Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model $factory
* @param callable|array $pivot
* @param (callable(): array<string, mixed>)|array<string, mixed> $pivot
* @param string|null $relationship
* @return static
*/
Expand Down Expand Up @@ -562,7 +565,7 @@ public function for($factory, $relationship = null)
/**
* Add a new "after making" callback to the model definition.
*
* @param \Closure $callback
* @param \Closure(\Illuminate\Database\Eloquent\Model|TModel): mixed $callback
* @return static
*/
public function afterMaking(Closure $callback)
Expand All @@ -573,7 +576,7 @@ public function afterMaking(Closure $callback)
/**
* Add a new "after creating" callback to the model definition.
*
* @param \Closure $callback
* @param \Closure(\Illuminate\Database\Eloquent\Model|TModel): mixed $callback
* @return static
*/
public function afterCreating(Closure $callback)
Expand Down Expand Up @@ -656,8 +659,8 @@ protected function newInstance(array $arguments = [])
/**
* Get a new model instance.
*
* @param array $attributes
* @return \Illuminate\Database\Eloquent\Model
* @param array<string, mixed> $attributes
* @return \Illuminate\Database\Eloquent\Model|TModel
*/
public function newModel(array $attributes = [])
{
Expand All @@ -669,7 +672,7 @@ public function newModel(array $attributes = [])
/**
* Get the name of the model that is generated by the factory.
*
* @return string
* @return class-string<\Illuminate\Database\Eloquent\Model|TModel>
*/
public function modelName()
{
Expand All @@ -689,7 +692,7 @@ public function modelName()
/**
* Specify the callback that should be invoked to guess model names based on factory names.
*
* @param callable $callback
* @param callable(): class-string<\Illuminate\Database\Eloquent\Model|TModel> $callback
* @return void
*/
public static function guessModelNamesUsing(callable $callback)
Expand All @@ -711,8 +714,8 @@ public static function useNamespace(string $namespace)
/**
* Get a new factory instance for the given model name.
*
* @param string $modelName
* @return static
* @param class-string<\Illuminate\Database\Eloquent\Model> $modelName
* @return \Illuminate\Database\Eloquent\Factories\Factory
*/
public static function factoryForModel(string $modelName)
{
Expand All @@ -724,7 +727,7 @@ public static function factoryForModel(string $modelName)
/**
* Specify the callback that should be invoked to guess factory names based on dynamic relationship names.
*
* @param callable $callback
* @param callable(): class-string<\Illuminate\Database\Eloquent\Model|TModel> $callback
* @return void
*/
public static function guessFactoryNamesUsing(callable $callback)
Expand All @@ -745,8 +748,8 @@ protected function withFaker()
/**
* Get the factory name for the given model name.
*
* @param string $modelName
* @return string
* @param class-string<\Illuminate\Database\Eloquent\Model> $modelName
* @return class-string<\Illuminate\Database\Eloquent\Factories\Factory>
*/
public static function resolveFactoryName(string $modelName)
{
Expand Down
Loading