Skip to content

Commit

Permalink
[11.x] Feat: remove HasFactory in model when not required (laravel#53104
Browse files Browse the repository at this point in the history
)

* 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 <taylor@laravel.com>
  • Loading branch information
2 people authored and timacdonald committed Oct 15, 2024
1 parent 13b5942 commit d4368b8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
34 changes: 21 additions & 13 deletions src/Illuminate/Foundation/Console/ModelMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,31 +235,39 @@ 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)
);
}

/**
* Build the replacements for a factory DocBlock.
* Build the replacements for a factory.
*
* @return string
* @return array<string, string>
*/
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 = <<<EOT
/** @use HasFactory<$factoryNamespace> */
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
/** @use HasFactory<$factoryNamespace> */
EOT;
return $replacements;
}

/**
Expand Down
5 changes: 2 additions & 3 deletions src/Illuminate/Foundation/Console/stubs/model.stub
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
}
11 changes: 10 additions & 1 deletion tests/Integration/Generators/ModelMakeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit d4368b8

Please sign in to comment.