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] Improve doctypes for Eloquent Factory guessing methods #41245

Merged
merged 2 commits into from
Feb 25, 2022
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
4 changes: 2 additions & 2 deletions src/Illuminate/Database/Eloquent/Factories/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,7 @@ public function modelName()
/**
* Specify the callback that should be invoked to guess model names based on factory names.
*
* @param callable(): class-string<\Illuminate\Database\Eloquent\Model|TModel> $callback
* @param callable(self): class-string<\Illuminate\Database\Eloquent\Model|TModel> $callback
* @return void
*/
public static function guessModelNamesUsing(callable $callback)
Expand Down Expand Up @@ -743,7 +743,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(): class-string<\Illuminate\Database\Eloquent\Model|TModel> $callback
* @param callable(class-string<\Illuminate\Database\Eloquent\Model>): class-string<\Illuminate\Database\Eloquent\Factories\Factory> $callback
* @return void
*/
public static function guessFactoryNamesUsing(callable $callback)
Expand Down
14 changes: 12 additions & 2 deletions types/Database/Eloquent/Factories/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,22 @@ public function definition()
// assertType('class-string<User>', $factory->modelName());
assertType('class-string<Illuminate\Database\Eloquent\Model>', $factory->modelName());

$factory->guessModelNamesUsing(function () {
return User::class;
Factory::guessModelNamesUsing(function (Factory $factory) {
return match (true) {
$factory instanceof UserFactory => User::class,
default => throw new LogicException('Unknown factory'),
};
});

$factory->useNamespace('string');

assertType(Factory::class, $factory::factoryForModel(User::class));

assertType('class-string<Illuminate\Database\Eloquent\Factories\Factory>', $factory->resolveFactoryName(User::class));

Factory::guessFactoryNamesUsing(function (string $modelName) {
return match ($modelName) {
User::class => UserFactory::class,
default => throw new LogicException('Unknown factory'),
};
});