Skip to content

Commit 05b184c

Browse files
[11.x] Feat: remove HasFactory in model when not required (#53104)
* Refactor ModelMakeCommand to improve factory handling * lint * tweak * tweak-2 * adding code which failing test in local on windows machine * Refactor ModelMakeCommand to handle factory imports on Windows * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent c08e4f7 commit 05b184c

File tree

3 files changed

+33
-17
lines changed

3 files changed

+33
-17
lines changed

src/Illuminate/Foundation/Console/ModelMakeCommand.php

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -235,31 +235,39 @@ protected function getDefaultNamespace($rootNamespace)
235235
*/
236236
protected function buildClass($name)
237237
{
238-
$replace = [];
239-
240-
if ($this->option('factory')) {
241-
$replace['{{ factoryDocBlock }}'] = $this->buildFactoryReplacements();
242-
} else {
243-
$replace["\n {{ factoryDocBlock }}"] = '';
244-
}
238+
$replace = $this->buildFactoryReplacements();
245239

246240
return str_replace(
247241
array_keys($replace), array_values($replace), parent::buildClass($name)
248242
);
249243
}
250244

251245
/**
252-
* Build the replacements for a factory DocBlock.
246+
* Build the replacements for a factory.
253247
*
254-
* @return string
248+
* @return array<string, string>
255249
*/
256250
protected function buildFactoryReplacements()
257251
{
258-
$factoryNamespace = '\\Database\\Factories\\'.Str::studly($this->argument('name')).'Factory';
252+
$replacements = [];
253+
254+
if ($this->option('factory')) {
255+
$factoryNamespace = '\\Database\\Factories\\'.Str::studly($this->argument('name')).'Factory';
256+
257+
$factoryCode = <<<EOT
258+
/** @use HasFactory<$factoryNamespace> */
259+
use HasFactory;
260+
EOT;
261+
262+
$replacements['{{ factory }}'] = $factoryCode;
263+
$replacements['{{ factoryImport }}'] = 'use Illuminate\Database\Eloquent\Factories\HasFactory;';
264+
} else {
265+
$replacements['{{ factory }}'] = '//';
266+
$replacements["{{ factoryImport }}\n"] = '';
267+
$replacements["{{ factoryImport }}\r\n"] = '';
268+
}
259269

260-
return <<<EOT
261-
/** @use HasFactory<$factoryNamespace> */
262-
EOT;
270+
return $replacements;
263271
}
264272

265273
/**

src/Illuminate/Foundation/Console/stubs/model.stub

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22

33
namespace {{ namespace }};
44

5-
use Illuminate\Database\Eloquent\Factories\HasFactory;
5+
{{ factoryImport }}
66
use Illuminate\Database\Eloquent\Model;
77

88
class {{ class }} extends Model
99
{
10-
{{ factoryDocBlock }}
11-
use HasFactory;
10+
{{ factory }}
1211
}

tests/Integration/Generators/ModelMakeCommandTest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@ public function testItCanGenerateModelFile()
2626
], 'app/Models/Foo.php');
2727

2828
$this->assertFileDoesNotContains([
29-
'{{ factoryDocBlock }}',
29+
'{{ factoryImport }}',
30+
'use Illuminate\Database\Eloquent\Factories\HasFactory;',
31+
'{{ factory }}',
3032
'/** @use HasFactory<\Database\Factories\FooFactory> */',
33+
'use HasFactory;',
3134
], 'app/Models/Foo.php');
3235

3336
$this->assertFilenameNotExists('app/Http/Controllers/FooController.php');
@@ -99,12 +102,18 @@ public function testItCanGenerateModelFileWithFactoryOption()
99102

100103
$this->assertFileContains([
101104
'namespace App\Models;',
105+
'use Illuminate\Database\Eloquent\Factories\HasFactory;',
102106
'use Illuminate\Database\Eloquent\Model;',
103107
'class Foo extends Model',
104108
'/** @use HasFactory<\Database\Factories\FooFactory> */',
105109
'use HasFactory;',
106110
], 'app/Models/Foo.php');
107111

112+
$this->assertFileNotContains([
113+
'{{ factoryImport }}',
114+
'{{ factory }}',
115+
], 'app/Models/Foo.php');
116+
108117
$this->assertFilenameNotExists('app/Http/Controllers/FooController.php');
109118
$this->assertFilenameExists('database/factories/FooFactory.php');
110119
$this->assertFilenameNotExists('database/seeders/FooSeeder.php');

0 commit comments

Comments
 (0)