Skip to content

Commit a8bca2f

Browse files
author
Nathan Esayeas
authored
Cleanup fixtures (#368)
1 parent f3684a5 commit a8bca2f

File tree

60 files changed

+169
-154
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+169
-154
lines changed

src/Blueprint.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Blueprint\Contracts\Generator;
66
use Blueprint\Contracts\Lexer;
7+
use Illuminate\Support\Facades\App;
78
use Illuminate\Support\Str;
89
use Symfony\Component\Yaml\Yaml;
910

@@ -29,6 +30,11 @@ public static function appPath()
2930
return str_replace('\\', '/', config('blueprint.app_path'));
3031
}
3132

33+
public static function isLaravel8OrHigher()
34+
{
35+
return version_compare(App::version(), '8.0.0', '>=');
36+
}
37+
3238
public function parse($content, $strip_dashes = true)
3339
{
3440
$content = str_replace(["\r\n", "\r"], "\n", $content);

src/Generators/FactoryGenerator.php

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

33
namespace Blueprint\Generators;
44

5+
use Blueprint\Blueprint;
56
use Blueprint\Contracts\Generator;
67
use Blueprint\Models\Column;
78
use Blueprint\Models\Model;
89
use Blueprint\Tree;
9-
use Illuminate\Support\Facades\App;
1010
use Illuminate\Support\Str;
1111
use Shift\Faker\Registry as FakerRegistry;
1212

@@ -28,16 +28,15 @@ public function output(Tree $tree): array
2828
{
2929
$output = [];
3030

31-
32-
if ($this->isLaravel8Up()) {
31+
if (Blueprint::isLaravel8OrHigher()) {
3332
$stub = $this->files->stub('factory.stub');
3433
} else {
3534
$stub = $this->files->stub('factory.closure.stub');
3635
}
3736

3837
/** @var \Blueprint\Models\Model $model */
3938
foreach ($tree->models() as $model) {
40-
if (! $this->isLaravel8Up()) {
39+
if (! Blueprint::isLaravel8OrHigher()) {
4140
$this->addImport($model, 'Faker\Generator as Faker');
4241
}
4342
$this->addImport($model, $model->fullyQualifiedClassName());
@@ -75,7 +74,7 @@ protected function populateStub(string $stub, Model $model)
7574
{
7675
$stub = str_replace('{{ model }}', $model->name(), $stub);
7776
$stub = str_replace('//', $this->buildDefinition($model), $stub);
78-
if ($this->isLaravel8Up()) {
77+
if (Blueprint::isLaravel8OrHigher()) {
7978
$stub = str_replace('use {{ namespacedModel }};', $this->buildImports($model), $stub);
8079
} else {
8180
$stub = str_replace('use Faker\Generator as Faker;'.PHP_EOL.'use {{ namespacedModel }};', $this->buildImports($model), $stub);
@@ -121,11 +120,11 @@ protected function buildDefinition(Model $model)
121120

122121
$class = Str::studly(Str::singular($table));
123122

124-
if ($this->isLaravel8Up()) {
123+
if (Blueprint::isLaravel8OrHigher()) {
125124
$this->addImport($model, $model->fullyQualifiedNamespace().'\\'.$class);
126125
}
127126
if ($key === 'id') {
128-
if ($this->isLaravel8Up()) {
127+
if (Blueprint::isLaravel8OrHigher()) {
129128
$definition .= str_repeat(self::INDENT, 3)."'{$column->name()}' => ";
130129
$definition .= sprintf('%s::factory()', $class);
131130
$definition .= ','.PHP_EOL;
@@ -135,7 +134,7 @@ protected function buildDefinition(Model $model)
135134
$definition .= ','.PHP_EOL;
136135
}
137136
} else {
138-
if ($this->isLaravel8Up()) {
137+
if (Blueprint::isLaravel8OrHigher()) {
139138
$definition .= str_repeat(self::INDENT, 3)."'{$column->name()}' => ";
140139
$definition .= sprintf('%s::factory()->create()->%s', $class, $key);
141140
$definition .= ','.PHP_EOL;
@@ -151,7 +150,7 @@ protected function buildDefinition(Model $model)
151150
$name = Str::beforeLast($column->name(), '_id');
152151
$class = Str::studly($column->attributes()[0] ?? $name);
153152

154-
if ($this->isLaravel8Up()) {
153+
if (Blueprint::isLaravel8OrHigher()) {
155154
$this->addImport($model, $model->fullyQualifiedNamespace().'\\'.$class);
156155
$definition .= str_repeat(self::INDENT, 3)."'{$column->name()}' => ";
157156
$definition .= sprintf('%s::factory()', $class);
@@ -162,7 +161,7 @@ protected function buildDefinition(Model $model)
162161
$definition .= ','.PHP_EOL;
163162
} elseif (in_array($column->dataType(), ['enum', 'set']) && ! empty($column->attributes())) {
164163
$faker = FakerRegistry::fakerData($column->name()) ?? FakerRegistry::fakerDataType($column->dataType());
165-
if ($this->isLaravel8Up()) {
164+
if (Blueprint::isLaravel8OrHigher()) {
166165
$definition .= str_repeat(self::INDENT, 3)."'{$column->name()}' => ";
167166
$definition .= '$this->faker->'.$faker;
168167
} else {
@@ -177,7 +176,7 @@ protected function buildDefinition(Model $model)
177176
);
178177
} elseif (in_array($column->dataType(), ['decimal', 'double', 'float'])) {
179178
$faker = FakerRegistry::fakerData($column->name()) ?? FakerRegistry::fakerDataType($column->dataType());
180-
if ($this->isLaravel8Up()) {
179+
if (Blueprint::isLaravel8OrHigher()) {
181180
$definition .= str_repeat(self::INDENT, 3)."'{$column->name()}' => ";
182181
$definition .= '$this->faker->'.$faker;
183182
} else {
@@ -196,7 +195,7 @@ protected function buildDefinition(Model $model)
196195
);
197196
} elseif (in_array($column->dataType(), ['json', 'jsonb'])) {
198197
$default = $column->defaultValue() ?? "'{}'";
199-
if ($this->isLaravel8Up()) {
198+
if (Blueprint::isLaravel8OrHigher()) {
200199
$definition .= str_repeat(self::INDENT, 3)."'{$column->name()}' => {$default},".PHP_EOL;
201200
} else {
202201
$definition .= str_repeat(self::INDENT, 2)."'{$column->name()}' => {$default},".PHP_EOL;
@@ -205,15 +204,15 @@ protected function buildDefinition(Model $model)
205204
if ($column->isNullable()) {
206205
continue;
207206
}
208-
if ($this->isLaravel8Up()) {
207+
if (Blueprint::isLaravel8OrHigher()) {
209208
$definition .= sprintf('%s%s => $this->faker->%s,%s', str_repeat(self::INDENT, 3), "'{$column->name()}_id'", FakerRegistry::fakerDataType('id'), PHP_EOL);
210209
$definition .= sprintf('%s%s => $this->faker->%s,%s', str_repeat(self::INDENT, 3), "'{$column->name()}_type'", FakerRegistry::fakerDataType('string'), PHP_EOL);
211210
} else {
212211
$definition .= sprintf('%s%s => $faker->%s,%s', str_repeat(self::INDENT, 2), "'{$column->name()}_id'", FakerRegistry::fakerDataType('id'), PHP_EOL);
213212
$definition .= sprintf('%s%s => $faker->%s,%s', str_repeat(self::INDENT, 2), "'{$column->name()}_type'", FakerRegistry::fakerDataType('string'), PHP_EOL);
214213
}
215214
} elseif ($column->dataType() === 'rememberToken') {
216-
if ($this->isLaravel8Up()) {
215+
if (Blueprint::isLaravel8OrHigher()) {
217216
$definition .= str_repeat(self::INDENT, 3)."'{$column->name()}' => ";
218217
} else {
219218
$this->addImport($model, 'Illuminate\Support\Str');
@@ -222,7 +221,7 @@ protected function buildDefinition(Model $model)
222221
$definition .= 'Str::random(10)';
223222
$definition .= ','.PHP_EOL;
224223
} else {
225-
if ($this->isLaravel8Up()) {
224+
if (Blueprint::isLaravel8OrHigher()) {
226225
$definition .= str_repeat(self::INDENT, 3)."'{$column->name()}' => ";
227226
} else {
228227
$definition .= str_repeat(self::INDENT, 2)."'{$column->name()}' => ";
@@ -238,7 +237,7 @@ protected function buildDefinition(Model $model)
238237
$faker = 'word';
239238
}
240239

241-
if ($this->isLaravel8Up()) {
240+
if (Blueprint::isLaravel8OrHigher()) {
242241
$definition .= '$this->faker->'.$faker;
243242
} else {
244243
$definition .= '$faker->'.$faker;
@@ -275,9 +274,4 @@ private function fillableColumns(array $columns): array
275274
return ! in_array('nullable', $column->modifiers());
276275
});
277276
}
278-
279-
protected function isLaravel8Up()
280-
{
281-
return version_compare(App::version(), '8.0.0', '>=');
282-
}
283277
}

src/Generators/ModelGenerator.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Blueprint\Models\Column;
88
use Blueprint\Models\Model;
99
use Blueprint\Tree;
10-
use Illuminate\Support\Facades\App;
1110
use Illuminate\Support\Str;
1211

1312
class ModelGenerator implements Generator
@@ -24,7 +23,7 @@ public function output(Tree $tree): array
2423
{
2524
$output = [];
2625

27-
if ($this->isLaravel8Up()) {
26+
if (Blueprint::isLaravel8OrHigher()) {
2827
$stub = $this->files->stub('model.class.stub');
2928
} else {
3029
$stub = $this->files->stub('model.class.no-factory.stub');
@@ -53,7 +52,7 @@ public function types(): array
5352

5453
protected function populateStub(string $stub, Model $model)
5554
{
56-
if ($this->isLaravel8Up()) {
55+
if (Blueprint::isLaravel8OrHigher()) {
5756
$stub = str_replace('{{ namespace }}', $model->fullyQualifiedNamespace(), $stub);
5857
$stub = str_replace(PHP_EOL.'class {{ class }}', $this->buildClassPhpDoc($model).PHP_EOL.'class {{ class }}', $stub);
5958
$stub = str_replace('{{ class }}', $model->name(), $stub);
@@ -232,7 +231,7 @@ protected function addTraits(Model $model, $stub)
232231
}
233232

234233
$stub = str_replace('use Illuminate\\Database\\Eloquent\\Model;', 'use Illuminate\\Database\\Eloquent\\Model;'.PHP_EOL.'use Illuminate\\Database\\Eloquent\\SoftDeletes;', $stub);
235-
if ($this->isLaravel8Up()) {
234+
if (Blueprint::isLaravel8OrHigher()) {
236235
$stub = Str::replaceFirst('use HasFactory', 'use HasFactory, SoftDeletes', $stub);
237236
} else {
238237
$stub = Str::replaceFirst('{', '{'.PHP_EOL.' use SoftDeletes;'.PHP_EOL, $stub);
@@ -307,11 +306,6 @@ private function castForColumn(Column $column)
307306
}
308307
}
309308

310-
protected function isLaravel8Up()
311-
{
312-
return version_compare(App::version(), '8.0.0', '>=');
313-
}
314-
315309
private function pretty_print_array(array $data, $assoc = true)
316310
{
317311
$output = var_export($data, true);

src/Generators/SeederGenerator.php

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

33
namespace Blueprint\Generators;
44

5+
use Blueprint\Blueprint;
56
use Blueprint\Contracts\Generator;
67
use Blueprint\Tree;
7-
use Illuminate\Support\Facades\App;
88

99
class SeederGenerator implements Generator
1010
{
@@ -31,7 +31,7 @@ public function output(Tree $tree): array
3131

3232
$output = [];
3333

34-
if ($this->isLaravel8OrHigher()) {
34+
if (Blueprint::isLaravel8OrHigher()) {
3535
$stub = $this->files->stub('seeder.stub');
3636
} else {
3737
$stub = $this->files->stub('seeder.no-factory.stub');
@@ -55,7 +55,7 @@ public function types(): array
5555
protected function populateStub(string $stub, string $model)
5656
{
5757
$stub = str_replace('{{ class }}', $this->getClassName($model), $stub);
58-
if ($this->isLaravel8OrHigher()) {
58+
if (Blueprint::isLaravel8OrHigher()) {
5959
$this->addImport($model, 'Illuminate\Database\Seeder');
6060

6161
$stub = str_replace('//', $this->build($model), $stub);
@@ -73,7 +73,7 @@ protected function getClassName(string $model)
7373

7474
protected function build(string $model)
7575
{
76-
if ($this->isLaravel8OrHigher()) {
76+
if (Blueprint::isLaravel8OrHigher()) {
7777
$this->addImport($model, $this->tree->fqcnForContext($model));
7878
return sprintf('%s::factory()->times(5)->create();', class_basename($this->tree->fqcnForContext($model)));
7979
}
@@ -97,15 +97,10 @@ private function addImport(string $model, $class)
9797

9898
private function getPath($model)
9999
{
100-
if ($this->isLaravel8OrHigher()) {
100+
if (Blueprint::isLaravel8OrHigher()) {
101101
return 'database/seeders/'.$model.'Seeder.php';
102102
}
103103

104104
return 'database/seeds/'.$model.'Seeder.php';
105105
}
106-
107-
protected function isLaravel8OrHigher()
108-
{
109-
return version_compare(App::version(), '8.0.0', '>=');
110-
}
111106
}

0 commit comments

Comments
 (0)