Skip to content

Commit e3ad28b

Browse files
authored
Merge pull request #183 from RonasIT/unify-prepare-relations
refactor: unify prepareRelations into a single method
2 parents de8bfff + 6eb7f37 commit e3ad28b

17 files changed

+64
-43
lines changed

src/DTO/RelationsDTO.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,4 @@ public function __construct(
1111
public array $belongsToMany = [],
1212
) {
1313
}
14-
15-
public function toArray(): array
16-
{
17-
return get_object_vars($this);
18-
}
1914
}

src/Generators/EntityGenerator.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ abstract class EntityGenerator
3636

3737
protected $paths = [];
3838
protected $model;
39-
protected $modelSubFolder = null;
39+
protected $modelSubFolder = '';
4040
protected $fields;
4141
protected $relations = [];
4242
protected $crudOptions;
@@ -289,4 +289,15 @@ protected function isStubExists(string $stubName, ?string $generationType = null
289289

290290
return true;
291291
}
292+
293+
protected function prepareRelations(): array
294+
{
295+
$result = [];
296+
297+
foreach ($this->relations as $relationType => $relations) {
298+
$result[$relationType] = array_map(fn ($relation) => class_basename($relation), $relations);
299+
}
300+
301+
return $result;
302+
}
292303
}

src/Generators/MigrationGenerator.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,6 @@ public function generate(): void
3333
event(new SuccessCreateMessage("Created a new Migration: {$entities}_create_table"));
3434
}
3535

36-
protected function prepareRelations(): array
37-
{
38-
$result = [];
39-
40-
foreach ($this->relations->toArray() as $relationType => $relations) {
41-
$result[$relationType] = array_map(fn ($relation) => Str::afterLast($relation, '/'), $relations);
42-
}
43-
44-
return $result;
45-
}
46-
4736
protected function isJson(string $typeName): bool
4837
{
4938
return $typeName === 'json';

src/Generators/ModelGenerator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ protected function insertImport(string &$classContent, string $import): void
113113
}
114114
}
115115

116-
public function prepareRelations(): array
116+
protected function prepareRelations(): array
117117
{
118118
$result = [];
119119

@@ -187,7 +187,7 @@ protected function shouldImportRelation(string $relation): bool
187187
{
188188
$relationNamespace = when(Str::contains($relation, '/'), fn () => Str::beforeLast($relation, '/'), '');
189189

190-
return $relationNamespace != $this->modelSubFolder;
190+
return $relationNamespace !== $this->modelSubFolder;
191191
}
192192

193193
protected function generateClassNamespace(string $className, ?string $folder = null): string

src/Generators/SeederGenerator.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -64,21 +64,6 @@ protected function createEntitySeeder(): void
6464
event(new SuccessCreateMessage($createMessage));
6565
}
6666

67-
protected function prepareRelations(): array
68-
{
69-
$result = [];
70-
71-
foreach ($this->relations as $type => $relationsByType) {
72-
$result[$type] = [];
73-
74-
foreach ($relationsByType as $relation) {
75-
$result[$type][] = class_basename($relation);
76-
}
77-
}
78-
79-
return $result;
80-
}
81-
8267
protected function appendSeederToList(): void
8368
{
8469
$content = file_get_contents($this->databaseSeederPath);

stubs/model.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,4 @@ class {{ $entity }} extends Model
4444
@include(config('entity-generator.stubs.relation'), $relation)
4545

4646
@endforeach
47-
}
47+
}

tests/ModelGeneratorTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,20 @@ className: SuccessCreateMessage::class,
177177
);
178178
}
179179

180+
public function testCreateWithSubFoldersRelations()
181+
{
182+
$this
183+
->artisan('make:entity Post -S title -A Forum/Author')
184+
->assertSuccessful();
185+
186+
$this->assertGeneratedFileEquals('new_model_with_subfolers_relations.php', 'app/Models/Post.php');
187+
188+
$this->assertEventPushed(
189+
className: SuccessCreateMessage::class,
190+
message: 'Created a new Model: Post',
191+
);
192+
}
193+
180194
public function testCreateModelWithoutDateFields()
181195
{
182196
$this

tests/Support/Model/ModelMockTrait.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public function mockFilesystem(): void
2828
$fileSystemMock->models = [
2929
'Comment.php' => file_get_contents(getcwd() . '/tests/Support/Models/WelcomeBonus.php'),
3030
'User.php' => file_get_contents(getcwd() . '/tests/Support/Models/WelcomeBonus.php'),
31+
'Forum/Author.php' => file_get_contents(getcwd() . '/tests/Support/Models/WelcomeBonus.php'),
3132
];
3233

3334
$fileSystemMock->setStructure();

tests/fixtures/CommandTest/model.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ class Post extends Model
1616
];
1717

1818
protected $hidden = ['pivot'];
19-
}
19+
}

tests/fixtures/CommandTest/subfolder/model.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ class Post extends Model
1616
];
1717

1818
protected $hidden = ['pivot'];
19-
}
19+
}

0 commit comments

Comments
 (0)