Skip to content

Commit 236a0e6

Browse files
author
Nathan Esayeas
authored
fix: correct newline character for windows (#394)
1 parent fd0b646 commit 236a0e6

File tree

1 file changed

+58
-54
lines changed

1 file changed

+58
-54
lines changed

src/Generators/FactoryGenerator.php

Lines changed: 58 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ public function output(Tree $tree): array
3636

3737
/** @var \Blueprint\Models\Model $model */
3838
foreach ($tree->models() as $model) {
39-
if (! Blueprint::isLaravel8OrHigher()) {
39+
if (!Blueprint::isLaravel8OrHigher()) {
4040
$this->addImport($model, 'Faker\Generator as Faker');
4141
}
4242
$this->addImport($model, $model->fullyQualifiedClassName());
4343

4444
$path = $this->getPath($model);
4545

46-
if (! $this->files->exists(dirname($path))) {
46+
if (!$this->files->exists(dirname($path))) {
4747
$this->files->makeDirectory(dirname($path), 0755, true);
4848
}
4949

@@ -64,22 +64,26 @@ protected function getPath(Model $model)
6464
{
6565
$path = $model->name();
6666
if ($model->namespace()) {
67-
$path = str_replace('\\', '/', $model->namespace()).'/'.$path;
67+
$path = str_replace('\\', '/', $model->namespace()) . '/' . $path;
6868
}
6969

70-
return 'database/factories/'.$path.'Factory.php';
70+
return 'database/factories/' . $path . 'Factory.php';
7171
}
7272

7373
protected function populateStub(string $stub, Model $model)
7474
{
7575
$stub = str_replace('{{ model }}', $model->name(), $stub);
7676
$stub = str_replace('//', $this->buildDefinition($model), $stub);
77-
if (Blueprint::isLaravel8OrHigher()) {
78-
$stub = str_replace('use {{ namespacedModel }};', $this->buildImports($model), $stub);
79-
} else {
80-
$stub = str_replace('use Faker\Generator as Faker;'.PHP_EOL.'use {{ namespacedModel }};', $this->buildImports($model), $stub);
77+
78+
if (!Blueprint::isLaravel8OrHigher()) {
79+
$stub = str_replace([
80+
"use Faker\Generator as Faker;\r\nuse",
81+
"use Faker\Generator as Faker;\nuse"
82+
], "use", $stub);
8183
}
8284

85+
$stub = str_replace('use {{ namespacedModel }};', $this->buildImports($model), $stub);
86+
8387
return $stub;
8488
}
8589

@@ -109,66 +113,66 @@ protected function buildDefinition(Model $model)
109113
} elseif ($foreign !== 'foreign') {
110114
$table = $foreign;
111115

112-
if (Str::startsWith($column->name(), $foreign.'_')) {
113-
$key = Str::after($column->name(), $foreign.'_');
114-
} elseif (Str::startsWith($column->name(), Str::snake(Str::singular($foreign)).'_')) {
115-
$key = Str::after($column->name(), Str::snake(Str::singular($foreign)).'_');
116-
} elseif (! Str::endsWith($column->name(), '_id')) {
116+
if (Str::startsWith($column->name(), $foreign . '_')) {
117+
$key = Str::after($column->name(), $foreign . '_');
118+
} elseif (Str::startsWith($column->name(), Str::snake(Str::singular($foreign)) . '_')) {
119+
$key = Str::after($column->name(), Str::snake(Str::singular($foreign)) . '_');
120+
} elseif (!Str::endsWith($column->name(), '_id')) {
117121
$key = $column->name();
118122
}
119123
}
120124

121125
$class = Str::studly(Str::singular($table));
122126

123127
if (Blueprint::isLaravel8OrHigher()) {
124-
$this->addImport($model, $model->fullyQualifiedNamespace().'\\'.$class);
128+
$this->addImport($model, $model->fullyQualifiedNamespace() . '\\' . $class);
125129
}
126130
if ($key === 'id') {
127131
if (Blueprint::isLaravel8OrHigher()) {
128-
$definition .= str_repeat(self::INDENT, 3)."'{$column->name()}' => ";
132+
$definition .= str_repeat(self::INDENT, 3) . "'{$column->name()}' => ";
129133
$definition .= sprintf('%s::factory()', $class);
130-
$definition .= ','.PHP_EOL;
134+
$definition .= ',' . PHP_EOL;
131135
} else {
132-
$definition .= str_repeat(self::INDENT, 2)."'{$column->name()}' => ";
133-
$definition .= sprintf('factory(%s::class)', '\\'.$model->fullyQualifiedNamespace().'\\'.$class);
134-
$definition .= ','.PHP_EOL;
136+
$definition .= str_repeat(self::INDENT, 2) . "'{$column->name()}' => ";
137+
$definition .= sprintf('factory(%s::class)', '\\' . $model->fullyQualifiedNamespace() . '\\' . $class);
138+
$definition .= ',' . PHP_EOL;
135139
}
136140
} else {
137141
if (Blueprint::isLaravel8OrHigher()) {
138-
$definition .= str_repeat(self::INDENT, 3)."'{$column->name()}' => ";
142+
$definition .= str_repeat(self::INDENT, 3) . "'{$column->name()}' => ";
139143
$definition .= sprintf('%s::factory()->create()->%s', $class, $key);
140-
$definition .= ','.PHP_EOL;
144+
$definition .= ',' . PHP_EOL;
141145
} else {
142-
$definition .= str_repeat(self::INDENT, 2)."'{$column->name()}' => function () {";
146+
$definition .= str_repeat(self::INDENT, 2) . "'{$column->name()}' => function () {";
143147
$definition .= PHP_EOL;
144-
$definition .= str_repeat(self::INDENT, 3).sprintf('return factory(%s::class)->create()->%s;', '\\'.$model->fullyQualifiedNamespace().'\\'.$class, $key);
148+
$definition .= str_repeat(self::INDENT, 3) . sprintf('return factory(%s::class)->create()->%s;', '\\' . $model->fullyQualifiedNamespace() . '\\' . $class, $key);
145149
$definition .= PHP_EOL;
146-
$definition .= str_repeat(self::INDENT, 2).'},'.PHP_EOL;
150+
$definition .= str_repeat(self::INDENT, 2) . '},' . PHP_EOL;
147151
}
148152
}
149153
} elseif ($column->dataType() === 'id' || ($column->dataType() === 'uuid' && Str::endsWith($column->name(), '_id'))) {
150154
$name = Str::beforeLast($column->name(), '_id');
151155
$class = Str::studly($column->attributes()[0] ?? $name);
152156

153157
if (Blueprint::isLaravel8OrHigher()) {
154-
$this->addImport($model, $model->fullyQualifiedNamespace().'\\'.$class);
155-
$definition .= str_repeat(self::INDENT, 3)."'{$column->name()}' => ";
158+
$this->addImport($model, $model->fullyQualifiedNamespace() . '\\' . $class);
159+
$definition .= str_repeat(self::INDENT, 3) . "'{$column->name()}' => ";
156160
$definition .= sprintf('%s::factory()', $class);
157161
} else {
158-
$definition .= str_repeat(self::INDENT, 2)."'{$column->name()}' => ";
159-
$definition .= sprintf('factory(%s::class)', '\\'.$model->fullyQualifiedNamespace().'\\'.$class);
162+
$definition .= str_repeat(self::INDENT, 2) . "'{$column->name()}' => ";
163+
$definition .= sprintf('factory(%s::class)', '\\' . $model->fullyQualifiedNamespace() . '\\' . $class);
160164
}
161-
$definition .= ','.PHP_EOL;
162-
} elseif (in_array($column->dataType(), ['enum', 'set']) && ! empty($column->attributes())) {
165+
$definition .= ',' . PHP_EOL;
166+
} elseif (in_array($column->dataType(), ['enum', 'set']) && !empty($column->attributes())) {
163167
$faker = FakerRegistry::fakerData($column->name()) ?? FakerRegistry::fakerDataType($column->dataType());
164168
if (Blueprint::isLaravel8OrHigher()) {
165-
$definition .= str_repeat(self::INDENT, 3)."'{$column->name()}' => ";
166-
$definition .= '$this->faker->'.$faker;
169+
$definition .= str_repeat(self::INDENT, 3) . "'{$column->name()}' => ";
170+
$definition .= '$this->faker->' . $faker;
167171
} else {
168-
$definition .= str_repeat(self::INDENT, 2)."'{$column->name()}' => ";
169-
$definition .= '$faker->'.$faker;
172+
$definition .= str_repeat(self::INDENT, 2) . "'{$column->name()}' => ";
173+
$definition .= '$faker->' . $faker;
170174
}
171-
$definition .= ','.PHP_EOL;
175+
$definition .= ',' . PHP_EOL;
172176
$definition = str_replace(
173177
"/** {$column->dataType()}_attributes **/",
174178
json_encode($column->attributes()),
@@ -177,28 +181,28 @@ protected function buildDefinition(Model $model)
177181
} elseif (in_array($column->dataType(), ['decimal', 'double', 'float'])) {
178182
$faker = FakerRegistry::fakerData($column->name()) ?? FakerRegistry::fakerDataType($column->dataType());
179183
if (Blueprint::isLaravel8OrHigher()) {
180-
$definition .= str_repeat(self::INDENT, 3)."'{$column->name()}' => ";
181-
$definition .= '$this->faker->'.$faker;
184+
$definition .= str_repeat(self::INDENT, 3) . "'{$column->name()}' => ";
185+
$definition .= '$this->faker->' . $faker;
182186
} else {
183-
$definition .= str_repeat(self::INDENT, 2)."'{$column->name()}' => ";
184-
$definition .= '$faker->'.$faker;
187+
$definition .= str_repeat(self::INDENT, 2) . "'{$column->name()}' => ";
188+
$definition .= '$faker->' . $faker;
185189
}
186-
$definition .= ','.PHP_EOL;
190+
$definition .= ',' . PHP_EOL;
187191

188192
$precision = min([65, intval($column->attributes()[0] ?? 10)]);
189193
$scale = min([30, max([0, intval($column->attributes()[1] ?? 0)])]);
190194

191195
$definition = str_replace(
192196
"/** {$column->dataType()}_attributes **/",
193-
implode(', ', [$scale, 0, (str_repeat(9, $precision - $scale).'.'.str_repeat(9, $scale))]),
197+
implode(', ', [$scale, 0, (str_repeat(9, $precision - $scale) . '.' . str_repeat(9, $scale))]),
194198
$definition
195199
);
196200
} elseif (in_array($column->dataType(), ['json', 'jsonb'])) {
197201
$default = $column->defaultValue() ?? "'{}'";
198202
if (Blueprint::isLaravel8OrHigher()) {
199-
$definition .= str_repeat(self::INDENT, 3)."'{$column->name()}' => {$default},".PHP_EOL;
203+
$definition .= str_repeat(self::INDENT, 3) . "'{$column->name()}' => {$default}," . PHP_EOL;
200204
} else {
201-
$definition .= str_repeat(self::INDENT, 2)."'{$column->name()}' => {$default},".PHP_EOL;
205+
$definition .= str_repeat(self::INDENT, 2) . "'{$column->name()}' => {$default}," . PHP_EOL;
202206
}
203207
} elseif ($column->dataType() === 'morphs') {
204208
if ($column->isNullable()) {
@@ -213,22 +217,22 @@ protected function buildDefinition(Model $model)
213217
}
214218
} elseif ($column->dataType() === 'rememberToken') {
215219
if (Blueprint::isLaravel8OrHigher()) {
216-
$definition .= str_repeat(self::INDENT, 3)."'{$column->name()}' => ";
220+
$definition .= str_repeat(self::INDENT, 3) . "'{$column->name()}' => ";
217221
} else {
218222
$this->addImport($model, 'Illuminate\Support\Str');
219-
$definition .= str_repeat(self::INDENT, 2)."'{$column->name()}' => ";
223+
$definition .= str_repeat(self::INDENT, 2) . "'{$column->name()}' => ";
220224
}
221225
$definition .= 'Str::random(10)';
222-
$definition .= ','.PHP_EOL;
226+
$definition .= ',' . PHP_EOL;
223227
} else {
224228
if (Blueprint::isLaravel8OrHigher()) {
225-
$definition .= str_repeat(self::INDENT, 3)."'{$column->name()}' => ";
229+
$definition .= str_repeat(self::INDENT, 3) . "'{$column->name()}' => ";
226230
} else {
227-
$definition .= str_repeat(self::INDENT, 2)."'{$column->name()}' => ";
231+
$definition .= str_repeat(self::INDENT, 2) . "'{$column->name()}' => ";
228232
}
229233
$type = $column->dataType();
230234
if ($column->isUnsigned()) {
231-
$type = 'unsigned'.$type;
235+
$type = 'unsigned' . $type;
232236
}
233237

234238
$faker = FakerRegistry::fakerData($column->name()) ?? (FakerRegistry::fakerDataType($type) ?? FakerRegistry::fakerDataType($column->dataType()));
@@ -238,11 +242,11 @@ protected function buildDefinition(Model $model)
238242
}
239243

240244
if (Blueprint::isLaravel8OrHigher()) {
241-
$definition .= '$this->faker->'.$faker;
245+
$definition .= '$this->faker->' . $faker;
242246
} else {
243-
$definition .= '$faker->'.$faker;
247+
$definition .= '$faker->' . $faker;
244248
}
245-
$definition .= ','.PHP_EOL;
249+
$definition .= ',' . PHP_EOL;
246250
}
247251
}
248252

@@ -255,7 +259,7 @@ protected function buildImports(Model $model)
255259
sort($imports);
256260

257261
return implode(PHP_EOL, array_map(function ($class) {
258-
return 'use '.$class.';';
262+
return 'use ' . $class . ';';
259263
}, $imports));
260264
}
261265

@@ -271,7 +275,7 @@ private function fillableColumns(array $columns): array
271275
}
272276

273277
return array_filter($columns, function (Column $column) {
274-
return ! in_array('nullable', $column->modifiers());
278+
return !in_array('nullable', $column->modifiers());
275279
});
276280
}
277281
}

0 commit comments

Comments
 (0)