diff --git a/src/Illuminate/Database/Eloquent/Factories/Factory.php b/src/Illuminate/Database/Eloquent/Factories/Factory.php index 8ac964225342..43d37719dfda 100644 --- a/src/Illuminate/Database/Eloquent/Factories/Factory.php +++ b/src/Illuminate/Database/Eloquent/Factories/Factory.php @@ -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) @@ -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) diff --git a/types/Database/Eloquent/Factories/Factory.php b/types/Database/Eloquent/Factories/Factory.php index 816b03815390..8ddbcb5a2490 100644 --- a/types/Database/Eloquent/Factories/Factory.php +++ b/types/Database/Eloquent/Factories/Factory.php @@ -183,8 +183,11 @@ public function definition() // assertType('class-string', $factory->modelName()); assertType('class-string', $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'); @@ -192,3 +195,10 @@ public function definition() assertType(Factory::class, $factory::factoryForModel(User::class)); assertType('class-string', $factory->resolveFactoryName(User::class)); + +Factory::guessFactoryNamesUsing(function (string $modelName) { + return match ($modelName) { + User::class => UserFactory::class, + default => throw new LogicException('Unknown factory'), + }; +});