Skip to content

Commit

Permalink
[9.x] Improve doctypes for Eloquent Factory guessing methods (#41245)
Browse files Browse the repository at this point in the history
* Improve doctypes for Factory::guessModelNamesUsing and ::guessFactoryNameUsing

* Apply StyleCI
  • Loading branch information
bastien-phi committed Feb 25, 2022
1 parent 36de8d6 commit d1c3d4f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
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'),
};
});

0 comments on commit d1c3d4f

Please sign in to comment.