From 9b5e4b4615c1c6bd71e1bd02114cfccd1ac49828 Mon Sep 17 00:00:00 2001 From: Ni Nelli Date: Thu, 16 Oct 2025 15:29:51 +0900 Subject: [PATCH 1/8] feat: add model relations property annotations refs: https://github.com/RonasIT/laravel-entity-generator/issues/203 --- src/Generators/ModelGenerator.php | 18 ++++++++++++++++++ stubs/model.blade.php | 3 +++ .../fixtures/ModelGeneratorTest/new_model.php | 3 +++ .../new_model_with_many_relations.php | 3 ++- .../new_model_with_subfolers_relations.php | 2 ++ .../new_subfolders_model.php | 3 +++ 6 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index d25f17ee..16d6345f 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -51,6 +51,7 @@ protected function getNewModelContent(): string 'importRelations' => $this->getImportedRelations(), 'anotationProperties' => $this->generateAnnotationProperties($this->fields), 'hasCarbonField' => !empty($this->fields['timestamp']) || !empty($this->fields['timestamp-required']), + 'hasCollectionType' => !empty($this->relations->hasMany) || !empty($this->relations->belongsToMany), ]); } @@ -205,6 +206,14 @@ protected function generateAnnotationProperties(array $fields): array } } + foreach ($this->relations as $type => $relations) { + foreach ($relations as $relation) { + $relation = class_basename($relation); + + $result[$this->getRelationName($relation, $type)] = $this->getRelationType($relation, $type); + } + } + return $result; } @@ -244,4 +253,13 @@ protected function isRequired(string $typeName): bool { return Str::endsWith($typeName, 'required'); } + + protected function getRelationType(string $relation, string $type): string + { + if (in_array($type, self::PLURAL_NUMBER_REQUIRED)) { + return "Collection|$relation"; + } + + return "{$relation}|null"; + } } diff --git a/stubs/model.blade.php b/stubs/model.blade.php index 7384db39..84439a53 100644 --- a/stubs/model.blade.php +++ b/stubs/model.blade.php @@ -8,6 +8,9 @@ @if($hasCarbonField) use Carbon\Carbon; @endif +@if($hasCollectionType) +use Illuminate\Database\Eloquent\Collection; +@endif @if(!empty($anotationProperties)) /** diff --git a/tests/fixtures/ModelGeneratorTest/new_model.php b/tests/fixtures/ModelGeneratorTest/new_model.php index 739fb44a..37b2f15e 100644 --- a/tests/fixtures/ModelGeneratorTest/new_model.php +++ b/tests/fixtures/ModelGeneratorTest/new_model.php @@ -5,6 +5,7 @@ use Illuminate\Database\Eloquent\Model; use RonasIT\Support\Traits\ModelTrait; use Carbon\Carbon; +use Illuminate\Database\Eloquent\Collection; /** * @property int|null $priority @@ -20,6 +21,8 @@ * @property Carbon|null $updated_at * @property Carbon $published_at * @property array $meta + * @property Comment|null $comment + * @property Collection|User $users */ class Post extends Model { diff --git a/tests/fixtures/ModelGeneratorTest/new_model_with_many_relations.php b/tests/fixtures/ModelGeneratorTest/new_model_with_many_relations.php index d7cf00e6..7245202c 100644 --- a/tests/fixtures/ModelGeneratorTest/new_model_with_many_relations.php +++ b/tests/fixtures/ModelGeneratorTest/new_model_with_many_relations.php @@ -5,9 +5,10 @@ use Illuminate\Database\Eloquent\Model; use RonasIT\Support\Traits\ModelTrait; use App\Models\User; +use Illuminate\Database\Eloquent\Collection; -//TODO: add @property annotation for each model's field /** + * @property Collection|User $users */ class Post extends Model { diff --git a/tests/fixtures/ModelGeneratorTest/new_model_with_subfolers_relations.php b/tests/fixtures/ModelGeneratorTest/new_model_with_subfolers_relations.php index 8205d5bf..60e38459 100644 --- a/tests/fixtures/ModelGeneratorTest/new_model_with_subfolers_relations.php +++ b/tests/fixtures/ModelGeneratorTest/new_model_with_subfolers_relations.php @@ -5,9 +5,11 @@ use Illuminate\Database\Eloquent\Model; use RonasIT\Support\Traits\ModelTrait; use App\Models\Forum\Author; +use Illuminate\Database\Eloquent\Collection; /** * @property string $title + * @property Collection|Author $authors */ class Post extends Model { diff --git a/tests/fixtures/ModelGeneratorTest/new_subfolders_model.php b/tests/fixtures/ModelGeneratorTest/new_subfolders_model.php index 5a21ccd0..bf7f280a 100644 --- a/tests/fixtures/ModelGeneratorTest/new_subfolders_model.php +++ b/tests/fixtures/ModelGeneratorTest/new_subfolders_model.php @@ -7,6 +7,7 @@ use App\Models\Comment; use App\Models\User; use Carbon\Carbon; +use Illuminate\Database\Eloquent\Collection; /** * @property int|null $priority @@ -22,6 +23,8 @@ * @property Carbon|null $updated_at * @property Carbon $published_at * @property array $meta + * @property Comment|null $comment + * @property Collection|User $users */ class Post extends Model { From 4f8a534ebcfdbf4982ffd6df57e6f70c8fee6b9a Mon Sep 17 00:00:00 2001 From: Ni Nelli Date: Fri, 17 Oct 2025 17:58:33 +0900 Subject: [PATCH 2/8] refactor: add related models property annotations, remove duplicate code refs: https://github.com/RonasIT/laravel-entity-generator/issues/203 --- src/Generators/ModelGenerator.php | 45 +++++++++++++------ stubs/model.blade.php | 6 +-- tests/ModelGeneratorTest.php | 26 ++++++++++- tests/Support/Model/ModelMockTrait.php | 28 +++++------- .../comment_relation_model.php | 3 ++ .../fixtures/ModelGeneratorTest/new_model.php | 2 +- .../new_model_with_many_relations.php | 2 +- .../new_model_with_subfolers_relations.php | 2 +- .../new_subfolders_model.php | 2 +- .../related_model_with_property.php | 26 +++++++++++ 10 files changed, 102 insertions(+), 40 deletions(-) mode change 100644 => 100755 tests/ModelGeneratorTest.php mode change 100644 => 100755 tests/Support/Model/ModelMockTrait.php mode change 100644 => 100755 tests/fixtures/ModelGeneratorTest/comment_relation_model.php mode change 100644 => 100755 tests/fixtures/ModelGeneratorTest/new_model.php mode change 100644 => 100755 tests/fixtures/ModelGeneratorTest/new_model_with_many_relations.php mode change 100644 => 100755 tests/fixtures/ModelGeneratorTest/new_model_with_subfolers_relations.php mode change 100644 => 100755 tests/fixtures/ModelGeneratorTest/new_subfolders_model.php create mode 100755 tests/fixtures/ModelGeneratorTest/related_model_with_property.php diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index 16d6345f..11ec759a 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -42,14 +42,16 @@ protected function hasRelations(): bool protected function getNewModelContent(): string { + $relations = $this->prepareRelations(); + return $this->getStub('model', [ 'entity' => $this->model, 'fields' => Arr::collapse($this->fields), - 'relations' => $this->prepareRelations(), + 'relations' => $relations, 'casts' => $this->getCasts($this->fields), 'namespace' => $this->getNamespace('models', $this->modelSubFolder), 'importRelations' => $this->getImportedRelations(), - 'anotationProperties' => $this->generateAnnotationProperties($this->fields), + 'annotationProperties' => $this->generateAnnotationProperties($this->fields, $relations), 'hasCarbonField' => !empty($this->fields['timestamp']) || !empty($this->fields['timestamp-required']), 'hasCollectionType' => !empty($this->relations->hasMany) || !empty($this->relations->belongsToMany), ]); @@ -82,14 +84,18 @@ public function prepareRelatedModels(): void $this->insertImport($content, $namespace); } + $relationName = $this->getRelationName($this->model, $types[$type]); + $newRelation = $this->getStub('relation', [ - 'name' => $this->getRelationName($this->model, $types[$type]), + 'name' => $relationName, 'type' => $types[$type], 'entity' => $this->model, ]); $fixedContent = preg_replace('/\}$/', "\n {$newRelation}\n}", $content); + $this->insertPropertyAnnotation($fixedContent, $this->getRelationType($this->model, $types[$type]), $relationName); + $this->saveClass('models', $relation, $fixedContent); } } @@ -196,7 +202,7 @@ protected function generateClassNamespace(string $className, ?string $folder = n return "{$path}\\{$psrPath}"; } - protected function generateAnnotationProperties(array $fields): array + protected function generateAnnotationProperties(array $fields, array $relations): array { $result = []; @@ -206,12 +212,8 @@ protected function generateAnnotationProperties(array $fields): array } } - foreach ($this->relations as $type => $relations) { - foreach ($relations as $relation) { - $relation = class_basename($relation); - - $result[$this->getRelationName($relation, $type)] = $this->getRelationType($relation, $type); - } + foreach ($relations as $relation) { + $result[$relation['name']] = $this->getRelationType($relation['entity'], $relation['type']); } return $result; @@ -254,12 +256,27 @@ protected function isRequired(string $typeName): bool return Str::endsWith($typeName, 'required'); } - protected function getRelationType(string $relation, string $type): string + protected function getRelationType(string $model, string $relation): string { - if (in_array($type, self::PLURAL_NUMBER_REQUIRED)) { - return "Collection|$relation"; + if (in_array($relation, self::PLURAL_NUMBER_REQUIRED)) { + return "Collection"; + } + + return "{$model}|null"; + } + + protected function insertPropertyAnnotation(string &$content, string $propertyDataType, string $propertyName): void + { + $annotation = "* @property {$propertyDataType} \${$propertyName}"; + + if (!Str::contains($content, '/**')) { + $content = preg_replace('/^\s*class.*\n/m', "\n/**\n {$annotation}\n */$0", $content); + } else { + $content = preg_replace('/\*\/\n/', "{$annotation}\n $0", $content); } - return "{$relation}|null"; + if (Str::contains($propertyDataType, 'Collection')) { + $this->insertImport($content, 'Illuminate\Database\Eloquent\Collection'); + } } } diff --git a/stubs/model.blade.php b/stubs/model.blade.php index 84439a53..844f420d 100644 --- a/stubs/model.blade.php +++ b/stubs/model.blade.php @@ -12,10 +12,10 @@ use Illuminate\Database\Eloquent\Collection; @endif -@if(!empty($anotationProperties)) +@if(!empty($annotationProperties)) /** -@foreach($anotationProperties as $key => $value) - * @property {{ $value }} ${{ $key }} +@foreach($annotationProperties as $key => $value) + * @property {!! $value !!} ${{ $key }} @endforeach */ @else diff --git a/tests/ModelGeneratorTest.php b/tests/ModelGeneratorTest.php old mode 100644 new mode 100755 index 41eaddf6..f22c7d5a --- a/tests/ModelGeneratorTest.php +++ b/tests/ModelGeneratorTest.php @@ -41,7 +41,9 @@ className: ResourceAlreadyExistsException::class, public function testRelationModelMissing() { - $this->mockFileSystemWithoutCommentModel(); + $this->mockFilesystem([ + 'User.php' => file_get_contents(getcwd() . '/tests/Support/Models/WelcomeBonus.php'), + ]); $this->assertExceptionThrew( className: ClassNotExistsException::class, @@ -244,4 +246,26 @@ className: WarningEvent::class, message: 'Generation of model has been skipped cause the view incorrect_stub from the config entity-generator.stubs.relation is not exists. Please check that config has the correct view name value.', ); } + + public function testAddPropertyAnnotationToRelatedModel() + { + $this->mockFilesystem([ + 'Post.php' => $this->getFixture('new_model_without_fields.php'), + ]); + + app(ModelGenerator::class) + ->setModel('Category') + ->setFields([]) + ->setRelations(new RelationsDTO( + belongsToMany: ['Post'], + )) + ->generate(); + + $this->assertGeneratedFileEquals('related_model_with_property.php', 'app/Models/Post.php'); + + $this->assertEventPushed( + className: SuccessCreateMessage::class, + message: 'Created a new Model: Category', + ); + } } diff --git a/tests/Support/Model/ModelMockTrait.php b/tests/Support/Model/ModelMockTrait.php old mode 100644 new mode 100755 index f65f83a2..cc15a253 --- a/tests/Support/Model/ModelMockTrait.php +++ b/tests/Support/Model/ModelMockTrait.php @@ -9,27 +9,19 @@ trait ModelMockTrait { use GeneratorMockTrait; - - public function mockFileSystemWithoutCommentModel(): void - { - $fileSystemMock = new FileSystemMock(); - - $fileSystemMock->models = [ - 'User.php' => file_get_contents(getcwd() . '/tests/Support/Models/WelcomeBonus.php'), - ]; - - $fileSystemMock->setStructure(); - } - - public function mockFilesystem(): void + public function mockFilesystem(array $models = []): void { $fileSystemMock = new FileSystemMock; - $fileSystemMock->models = [ - 'Comment.php' => file_get_contents(getcwd() . '/tests/Support/Models/WelcomeBonus.php'), - 'User.php' => file_get_contents(getcwd() . '/tests/Support/Models/WelcomeBonus.php'), - 'Forum/Author.php' => file_get_contents(getcwd() . '/tests/Support/Models/WelcomeBonus.php'), - ]; + if (!empty ($models)) { + $fileSystemMock->models = $models; + } else { + $fileSystemMock->models = [ + 'Comment.php' => file_get_contents(getcwd() . '/tests/Support/Models/WelcomeBonus.php'), + 'User.php' => file_get_contents(getcwd() . '/tests/Support/Models/WelcomeBonus.php'), + 'Forum/Author.php' => file_get_contents(getcwd() . '/tests/Support/Models/WelcomeBonus.php'), + ]; + } $fileSystemMock->setStructure(); } diff --git a/tests/fixtures/ModelGeneratorTest/comment_relation_model.php b/tests/fixtures/ModelGeneratorTest/comment_relation_model.php old mode 100644 new mode 100755 index 1e42f1d6..446326cf --- a/tests/fixtures/ModelGeneratorTest/comment_relation_model.php +++ b/tests/fixtures/ModelGeneratorTest/comment_relation_model.php @@ -5,6 +5,9 @@ use Illuminate\Database\Eloquent\Model; use RonasIT\Support\Traits\ModelTrait; +/** + * @property Post|null $post + */ class WelcomeBonus extends Model { use ModelTrait; diff --git a/tests/fixtures/ModelGeneratorTest/new_model.php b/tests/fixtures/ModelGeneratorTest/new_model.php old mode 100644 new mode 100755 index 37b2f15e..0ddd9154 --- a/tests/fixtures/ModelGeneratorTest/new_model.php +++ b/tests/fixtures/ModelGeneratorTest/new_model.php @@ -22,7 +22,7 @@ * @property Carbon $published_at * @property array $meta * @property Comment|null $comment - * @property Collection|User $users + * @property Collection $users */ class Post extends Model { diff --git a/tests/fixtures/ModelGeneratorTest/new_model_with_many_relations.php b/tests/fixtures/ModelGeneratorTest/new_model_with_many_relations.php old mode 100644 new mode 100755 index 7245202c..126ccdd3 --- a/tests/fixtures/ModelGeneratorTest/new_model_with_many_relations.php +++ b/tests/fixtures/ModelGeneratorTest/new_model_with_many_relations.php @@ -8,7 +8,7 @@ use Illuminate\Database\Eloquent\Collection; /** - * @property Collection|User $users + * @property Collection $users */ class Post extends Model { diff --git a/tests/fixtures/ModelGeneratorTest/new_model_with_subfolers_relations.php b/tests/fixtures/ModelGeneratorTest/new_model_with_subfolers_relations.php old mode 100644 new mode 100755 index 60e38459..6bc06b67 --- a/tests/fixtures/ModelGeneratorTest/new_model_with_subfolers_relations.php +++ b/tests/fixtures/ModelGeneratorTest/new_model_with_subfolers_relations.php @@ -9,7 +9,7 @@ /** * @property string $title - * @property Collection|Author $authors + * @property Collection $authors */ class Post extends Model { diff --git a/tests/fixtures/ModelGeneratorTest/new_subfolders_model.php b/tests/fixtures/ModelGeneratorTest/new_subfolders_model.php old mode 100644 new mode 100755 index bf7f280a..243e0193 --- a/tests/fixtures/ModelGeneratorTest/new_subfolders_model.php +++ b/tests/fixtures/ModelGeneratorTest/new_subfolders_model.php @@ -24,7 +24,7 @@ * @property Carbon $published_at * @property array $meta * @property Comment|null $comment - * @property Collection|User $users + * @property Collection $users */ class Post extends Model { diff --git a/tests/fixtures/ModelGeneratorTest/related_model_with_property.php b/tests/fixtures/ModelGeneratorTest/related_model_with_property.php new file mode 100755 index 00000000..0e8c2263 --- /dev/null +++ b/tests/fixtures/ModelGeneratorTest/related_model_with_property.php @@ -0,0 +1,26 @@ + $categories + */ +class Post extends Model +{ + use ModelTrait; + + protected $fillable = [ + ]; + + protected $hidden = ['pivot']; + + public function categories() + { + return $this->belongsToMany(Category::class); + } +} From 48d2b8ce72ddb7502e89c6f9c58ad1b9d4fe97ba Mon Sep 17 00:00:00 2001 From: Ni Nelli Date: Mon, 20 Oct 2025 14:15:25 +0900 Subject: [PATCH 3/8] chore: add TODO refs: https://github.com/RonasIT/laravel-entity-generator/issues/203 --- src/Generators/ModelGenerator.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index 11ec759a..cbe0fc9b 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -92,6 +92,7 @@ public function prepareRelatedModels(): void 'entity' => $this->model, ]); +// TODO: use ronasit/larabuilder instead $fixedContent = preg_replace('/\}$/', "\n {$newRelation}\n}", $content); $this->insertPropertyAnnotation($fixedContent, $this->getRelationType($this->model, $types[$type]), $relationName); @@ -113,6 +114,7 @@ protected function insertImport(string &$classContent, string $import): void $import = "use {$import};"; if (!Str::contains($classContent, $import)) { +// TODO: use ronasit/larabuilder instead $classContent = preg_replace('/(namespace\s+[^;]+;\s*)/', "$1{$import}\n", $classContent, 1); } } @@ -269,6 +271,7 @@ protected function insertPropertyAnnotation(string &$content, string $propertyDa { $annotation = "* @property {$propertyDataType} \${$propertyName}"; +// TODO: use ronasit/larabuilder instead if (!Str::contains($content, '/**')) { $content = preg_replace('/^\s*class.*\n/m', "\n/**\n {$annotation}\n */$0", $content); } else { From 5e293d2df361523669da02fee94d83d5049d9614 Mon Sep 17 00:00:00 2001 From: Ni Nelli Date: Thu, 23 Oct 2025 13:51:26 +0900 Subject: [PATCH 4/8] style: minor fixes refs: https://github.com/RonasIT/laravel-entity-generator/issues/203 --- src/Generators/ModelGenerator.php | 6 +++--- tests/Support/Model/ModelMockTrait.php | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index cbe0fc9b..53f20064 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -92,7 +92,7 @@ public function prepareRelatedModels(): void 'entity' => $this->model, ]); -// TODO: use ronasit/larabuilder instead + // TODO: use ronasit/larabuilder instead regexp $fixedContent = preg_replace('/\}$/', "\n {$newRelation}\n}", $content); $this->insertPropertyAnnotation($fixedContent, $this->getRelationType($this->model, $types[$type]), $relationName); @@ -114,7 +114,7 @@ protected function insertImport(string &$classContent, string $import): void $import = "use {$import};"; if (!Str::contains($classContent, $import)) { -// TODO: use ronasit/larabuilder instead + // TODO: use ronasit/larabuilder instead regexp $classContent = preg_replace('/(namespace\s+[^;]+;\s*)/', "$1{$import}\n", $classContent, 1); } } @@ -271,7 +271,7 @@ protected function insertPropertyAnnotation(string &$content, string $propertyDa { $annotation = "* @property {$propertyDataType} \${$propertyName}"; -// TODO: use ronasit/larabuilder instead + // TODO: use ronasit/larabuilder instead regexp if (!Str::contains($content, '/**')) { $content = preg_replace('/^\s*class.*\n/m', "\n/**\n {$annotation}\n */$0", $content); } else { diff --git a/tests/Support/Model/ModelMockTrait.php b/tests/Support/Model/ModelMockTrait.php index cc15a253..f03c6793 100755 --- a/tests/Support/Model/ModelMockTrait.php +++ b/tests/Support/Model/ModelMockTrait.php @@ -13,7 +13,7 @@ public function mockFilesystem(array $models = []): void { $fileSystemMock = new FileSystemMock; - if (!empty ($models)) { + if (!empty($models)) { $fileSystemMock->models = $models; } else { $fileSystemMock->models = [ From caf1c8c9eb89fce447f16492b659fb6adb9f5ab3 Mon Sep 17 00:00:00 2001 From: Ni Nelli Date: Fri, 24 Oct 2025 13:47:10 +0900 Subject: [PATCH 5/8] refactor: fix model relation type, separate methods in ModelMockTrait refs: https://github.com/RonasIT/laravel-entity-generator/issues/203 --- src/Generators/ModelGenerator.php | 2 +- tests/ModelGeneratorTest.php | 10 +++----- tests/Support/Model/ModelMockTrait.php | 23 +++++++++++-------- .../fixtures/ModelGeneratorTest/new_model.php | 2 +- .../new_model_with_many_relations.php | 2 +- .../new_model_with_subfolers_relations.php | 2 +- .../new_subfolders_model.php | 2 +- .../related_model_with_property.php | 20 +++++++++++----- 8 files changed, 36 insertions(+), 27 deletions(-) diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index 53f20064..a9762742 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -261,7 +261,7 @@ protected function isRequired(string $typeName): bool protected function getRelationType(string $model, string $relation): string { if (in_array($relation, self::PLURAL_NUMBER_REQUIRED)) { - return "Collection"; + return "Collection<{$model}>"; } return "{$model}|null"; diff --git a/tests/ModelGeneratorTest.php b/tests/ModelGeneratorTest.php index 07c85c73..a919b289 100755 --- a/tests/ModelGeneratorTest.php +++ b/tests/ModelGeneratorTest.php @@ -19,7 +19,7 @@ public function setUp(): void { parent::setUp(); - $this->mockFilesystem(); + $this->mockDefaultFilesystem(); } public function testModelAlreadyExists() @@ -249,19 +249,15 @@ className: WarningEvent::class, public function testAddPropertyAnnotationToRelatedModel() { - $this->mockFilesystem([ - 'Post.php' => $this->getFixture('new_model_without_fields.php'), - ]); - app(ModelGenerator::class) ->setModel('Category') ->setFields([]) ->setRelations(new RelationsDTO( - belongsToMany: ['Post'], + belongsToMany: ['User'], )) ->generate(); - $this->assertGeneratedFileEquals('related_model_with_property.php', 'app/Models/Post.php'); + $this->assertGeneratedFileEquals('related_model_with_property.php', 'app/Models/User.php'); $this->assertEventPushed( className: SuccessCreateMessage::class, diff --git a/tests/Support/Model/ModelMockTrait.php b/tests/Support/Model/ModelMockTrait.php index f03c6793..a08c4c1d 100755 --- a/tests/Support/Model/ModelMockTrait.php +++ b/tests/Support/Model/ModelMockTrait.php @@ -9,19 +9,24 @@ trait ModelMockTrait { use GeneratorMockTrait; + public function mockDefaultFilesystem(): void + { + $fileSystemMock = new FileSystemMock; + + $fileSystemMock->models = [ + 'Comment.php' => file_get_contents(getcwd() . '/tests/Support/Models/WelcomeBonus.php'), + 'User.php' => file_get_contents(getcwd() . '/tests/Support/Models/WelcomeBonus.php'), + 'Forum/Author.php' => file_get_contents(getcwd() . '/tests/Support/Models/WelcomeBonus.php'), + ]; + + $fileSystemMock->setStructure(); + } + public function mockFilesystem(array $models = []): void { $fileSystemMock = new FileSystemMock; - if (!empty($models)) { - $fileSystemMock->models = $models; - } else { - $fileSystemMock->models = [ - 'Comment.php' => file_get_contents(getcwd() . '/tests/Support/Models/WelcomeBonus.php'), - 'User.php' => file_get_contents(getcwd() . '/tests/Support/Models/WelcomeBonus.php'), - 'Forum/Author.php' => file_get_contents(getcwd() . '/tests/Support/Models/WelcomeBonus.php'), - ]; - } + $fileSystemMock->models = $models; $fileSystemMock->setStructure(); } diff --git a/tests/fixtures/ModelGeneratorTest/new_model.php b/tests/fixtures/ModelGeneratorTest/new_model.php index 0ddd9154..fcf29337 100755 --- a/tests/fixtures/ModelGeneratorTest/new_model.php +++ b/tests/fixtures/ModelGeneratorTest/new_model.php @@ -22,7 +22,7 @@ * @property Carbon $published_at * @property array $meta * @property Comment|null $comment - * @property Collection $users + * @property Collection $users */ class Post extends Model { diff --git a/tests/fixtures/ModelGeneratorTest/new_model_with_many_relations.php b/tests/fixtures/ModelGeneratorTest/new_model_with_many_relations.php index 126ccdd3..62dfd92d 100755 --- a/tests/fixtures/ModelGeneratorTest/new_model_with_many_relations.php +++ b/tests/fixtures/ModelGeneratorTest/new_model_with_many_relations.php @@ -8,7 +8,7 @@ use Illuminate\Database\Eloquent\Collection; /** - * @property Collection $users + * @property Collection $users */ class Post extends Model { diff --git a/tests/fixtures/ModelGeneratorTest/new_model_with_subfolers_relations.php b/tests/fixtures/ModelGeneratorTest/new_model_with_subfolers_relations.php index 6bc06b67..65726408 100755 --- a/tests/fixtures/ModelGeneratorTest/new_model_with_subfolers_relations.php +++ b/tests/fixtures/ModelGeneratorTest/new_model_with_subfolers_relations.php @@ -9,7 +9,7 @@ /** * @property string $title - * @property Collection $authors + * @property Collection $authors */ class Post extends Model { diff --git a/tests/fixtures/ModelGeneratorTest/new_subfolders_model.php b/tests/fixtures/ModelGeneratorTest/new_subfolders_model.php index 243e0193..633820fb 100755 --- a/tests/fixtures/ModelGeneratorTest/new_subfolders_model.php +++ b/tests/fixtures/ModelGeneratorTest/new_subfolders_model.php @@ -24,7 +24,7 @@ * @property Carbon $published_at * @property array $meta * @property Comment|null $comment - * @property Collection $users + * @property Collection $users */ class Post extends Model { diff --git a/tests/fixtures/ModelGeneratorTest/related_model_with_property.php b/tests/fixtures/ModelGeneratorTest/related_model_with_property.php index 0e8c2263..d90fe6c0 100755 --- a/tests/fixtures/ModelGeneratorTest/related_model_with_property.php +++ b/tests/fixtures/ModelGeneratorTest/related_model_with_property.php @@ -1,26 +1,34 @@ $categories + * @property Collection $categories */ -class Post extends Model +class WelcomeBonus extends Model { use ModelTrait; + public function getConnectionName(): string + { + return 'pgsql'; + } + protected $fillable = [ + 'title', + 'name', ]; - protected $hidden = ['pivot']; + public function some_relation() + { + } public function categories() { return $this->belongsToMany(Category::class); } -} +} \ No newline at end of file From 21dd8aa31e732d7a41d1cb732f92878676d095b9 Mon Sep 17 00:00:00 2001 From: Ni Nelli Date: Tue, 28 Oct 2025 14:11:35 +0900 Subject: [PATCH 6/8] refactor: style fixes, remove redundant code refs: https://github.com/RonasIT/laravel-entity-generator/issues/203 --- tests/Support/Model/ModelMockTrait.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Support/Model/ModelMockTrait.php b/tests/Support/Model/ModelMockTrait.php index a08c4c1d..744569e7 100755 --- a/tests/Support/Model/ModelMockTrait.php +++ b/tests/Support/Model/ModelMockTrait.php @@ -11,7 +11,7 @@ trait ModelMockTrait public function mockDefaultFilesystem(): void { - $fileSystemMock = new FileSystemMock; + $fileSystemMock = new FileSystemMock(); $fileSystemMock->models = [ 'Comment.php' => file_get_contents(getcwd() . '/tests/Support/Models/WelcomeBonus.php'), @@ -22,9 +22,9 @@ public function mockDefaultFilesystem(): void $fileSystemMock->setStructure(); } - public function mockFilesystem(array $models = []): void + public function mockFilesystem(array $models): void { - $fileSystemMock = new FileSystemMock; + $fileSystemMock = new FileSystemMock(); $fileSystemMock->models = $models; From 21ef6ff6842bb214cbfeea8de1c2124b5915fb98 Mon Sep 17 00:00:00 2001 From: DenTray Date: Mon, 3 Nov 2025 21:35:06 +0600 Subject: [PATCH 7/8] Apply suggestion from @Copilot Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/Generators/ModelGenerator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index 253ec1fd..2a95e3ba 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -268,7 +268,7 @@ protected function insertPropertyAnnotation(string &$content, string $propertyDa // TODO: use ronasit/larabuilder instead regexp if (!Str::contains($content, '/**')) { - $content = preg_replace('/^\s*class.*\n/m', "\n/**\n {$annotation}\n */$0", $content); + $content = preg_replace('/^\s*class[\s\S]+?\{/m', "\n/**\n {$annotation}\n */$0", $content); } else { $content = preg_replace('/\*\/\n/', "{$annotation}\n $0", $content); } From 69628d2a56f51e28966c0f2047b0ef686b5028ab Mon Sep 17 00:00:00 2001 From: DenTray Date: Mon, 3 Nov 2025 21:35:47 +0600 Subject: [PATCH 8/8] Apply suggestion from @Copilot Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/Generators/ModelGenerator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index 2a95e3ba..86651d61 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -270,7 +270,7 @@ protected function insertPropertyAnnotation(string &$content, string $propertyDa if (!Str::contains($content, '/**')) { $content = preg_replace('/^\s*class[\s\S]+?\{/m', "\n/**\n {$annotation}\n */$0", $content); } else { - $content = preg_replace('/\*\/\n/', "{$annotation}\n $0", $content); + $content = preg_replace('/\*\//m', "{$annotation}\n $0", $content); } if (Str::contains($propertyDataType, 'Collection')) {