-
Notifications
You must be signed in to change notification settings - Fork 3
[203]: add model relations property annotations #208
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
9b5e4b4
8b1a21e
4f8a534
48d2b8c
63a88a9
5e293d2
c928e1b
c3c6115
caf1c8c
484b184
21dd8aa
569c284
21ef6ff
69628d2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -42,15 +42,18 @@ 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), | ||||||
| ]); | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -81,14 +84,19 @@ 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, | ||||||
| ]); | ||||||
|
|
||||||
| // TODO: use ronasit/larabuilder instead | ||||||
| $fixedContent = preg_replace('/\}$/', "\n {$newRelation}\n}", $content); | ||||||
|
|
||||||
| $this->insertPropertyAnnotation($fixedContent, $this->getRelationType($this->model, $types[$type]), $relationName); | ||||||
|
|
||||||
| $this->saveClass('models', $relation, $fixedContent); | ||||||
| } | ||||||
| } | ||||||
|
|
@@ -106,6 +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 | |
| // TODO: use ronasit/larabuilder instead |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| return "Collection<int, $model>"; | |
| return "Collection<{$model}>"; |
Outdated
Copilot
AI
Oct 20, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove the extra spaces before the TODO comment. Use consistent indentation with // followed by a single space.
| // TODO: use ronasit/larabuilder instead | |
| // TODO: use ronasit/larabuilder instead |
DenTray marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
DenTray marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,11 +8,14 @@ | |
| @if($hasCarbonField) | ||
| use Carbon\Carbon; | ||
| @endif | ||
| @if($hasCollectionType) | ||
| 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 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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', | ||
| ); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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 = new FileSystemMock; | |
| $fileSystemMock = new FileSystemMock(); |
ref: https://www.php-fig.org/psr/psr-12/#4-classes-properties-and-methods
Outdated
Copilot
AI
Oct 20, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove the space between !empty and the opening parenthesis. The correct syntax is !empty($models).
| if (!empty ($models)) { | |
| if (!empty($models)) { |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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<int, User> $users | ||||||
|
||||||
| * @property Collection<int, User> $users | |
| * @property Collection<User> $users |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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<int, User> $users | ||||||
|
||||||
| * @property Collection<int, User> $users | |
| * @property Collection<User> $users |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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<int, Author> $authors | ||||||
|
||||||
| * @property Collection<int, Author> $authors | |
| * @property Collection<Author> $authors |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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<int, User> $users | ||||||
|
||||||
| * @property Collection<int, User> $users | |
| * @property Collection<User> $users |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,26 @@ | ||||||
| <?php | ||||||
|
|
||||||
| namespace App\Models; | ||||||
|
|
||||||
| use Illuminate\Database\Eloquent\Collection; | ||||||
| use Illuminate\Database\Eloquent\Model; | ||||||
| use RonasIT\Support\Traits\ModelTrait; | ||||||
|
|
||||||
| //TODO: add @property annotation for each model's field | ||||||
| /** | ||||||
| * @property Collection<int, Category> $categories | ||||||
|
||||||
| * @property Collection<int, Category> $categories | |
| * @property Collection<Category> $categories |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove the extra spaces before the TODO comment. Use consistent indentation with
//followed by a single space.