diff --git a/src/Illuminate/Foundation/Console/ModelMakeCommand.php b/src/Illuminate/Foundation/Console/ModelMakeCommand.php index ebc570648b1d..9717a9d8eaa1 100644 --- a/src/Illuminate/Foundation/Console/ModelMakeCommand.php +++ b/src/Illuminate/Foundation/Console/ModelMakeCommand.php @@ -235,13 +235,7 @@ protected function getDefaultNamespace($rootNamespace) */ protected function buildClass($name) { - $replace = []; - - if ($this->option('factory')) { - $replace['{{ factoryDocBlock }}'] = $this->buildFactoryReplacements(); - } else { - $replace["\n {{ factoryDocBlock }}"] = ''; - } + $replace = $this->buildFactoryReplacements(); return str_replace( array_keys($replace), array_values($replace), parent::buildClass($name) @@ -249,17 +243,31 @@ protected function buildClass($name) } /** - * Build the replacements for a factory DocBlock. + * Build the replacements for a factory. * - * @return string + * @return array */ protected function buildFactoryReplacements() { - $factoryNamespace = '\\Database\\Factories\\'.Str::studly($this->argument('name')).'Factory'; + $replacements = []; + + if ($this->option('factory')) { + $factoryNamespace = '\\Database\\Factories\\'.Str::studly($this->argument('name')).'Factory'; + + $factoryCode = << */ + use HasFactory; + EOT; + + $replacements['{{ factory }}'] = $factoryCode; + $replacements['{{ factoryImport }}'] = 'use Illuminate\Database\Eloquent\Factories\HasFactory;'; + } else { + $replacements['{{ factory }}'] = '//'; + $replacements["{{ factoryImport }}\n"] = ''; + $replacements["{{ factoryImport }}\r\n"] = ''; + } - return << */ - EOT; + return $replacements; } /** diff --git a/src/Illuminate/Foundation/Console/stubs/model.stub b/src/Illuminate/Foundation/Console/stubs/model.stub index eb85079c53f1..7718ae87ee5a 100644 --- a/src/Illuminate/Foundation/Console/stubs/model.stub +++ b/src/Illuminate/Foundation/Console/stubs/model.stub @@ -2,11 +2,10 @@ namespace {{ namespace }}; -use Illuminate\Database\Eloquent\Factories\HasFactory; +{{ factoryImport }} use Illuminate\Database\Eloquent\Model; class {{ class }} extends Model { - {{ factoryDocBlock }} - use HasFactory; + {{ factory }} } diff --git a/tests/Integration/Generators/ModelMakeCommandTest.php b/tests/Integration/Generators/ModelMakeCommandTest.php index 7e9f2cb80ab4..78e8f0596811 100644 --- a/tests/Integration/Generators/ModelMakeCommandTest.php +++ b/tests/Integration/Generators/ModelMakeCommandTest.php @@ -26,8 +26,11 @@ public function testItCanGenerateModelFile() ], 'app/Models/Foo.php'); $this->assertFileDoesNotContains([ - '{{ factoryDocBlock }}', + '{{ factoryImport }}', + 'use Illuminate\Database\Eloquent\Factories\HasFactory;', + '{{ factory }}', '/** @use HasFactory<\Database\Factories\FooFactory> */', + 'use HasFactory;', ], 'app/Models/Foo.php'); $this->assertFilenameNotExists('app/Http/Controllers/FooController.php'); @@ -99,12 +102,18 @@ public function testItCanGenerateModelFileWithFactoryOption() $this->assertFileContains([ 'namespace App\Models;', + 'use Illuminate\Database\Eloquent\Factories\HasFactory;', 'use Illuminate\Database\Eloquent\Model;', 'class Foo extends Model', '/** @use HasFactory<\Database\Factories\FooFactory> */', 'use HasFactory;', ], 'app/Models/Foo.php'); + $this->assertFileNotContains([ + '{{ factoryImport }}', + '{{ factory }}', + ], 'app/Models/Foo.php'); + $this->assertFilenameNotExists('app/Http/Controllers/FooController.php'); $this->assertFilenameExists('database/factories/FooFactory.php'); $this->assertFilenameNotExists('database/seeders/FooSeeder.php');