-
Notifications
You must be signed in to change notification settings - Fork 3
[217]: fill getAvailableRelations with passed relations #220
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
base: master
Are you sure you want to change the base?
Conversation
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.
Pull Request Overview
This PR enhances the request generation functionality to automatically populate available relations in generated request classes. The key improvement is moving the getRelationName method to the base EntityGenerator class for reuse and implementing logic to automatically extract and populate relations in request classes.
- Moved
getRelationNamemethod fromModelGeneratortoEntityGeneratorfor shared use across generators - Added
getAvailableRelationsmethod toRequestsGeneratorthat extracts relation names from the model - Updated request stub to conditionally show TODO comment only when no relations are available
Reviewed Changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Generators/EntityGenerator.php | Added shared getRelationName method to base class |
| src/Generators/ModelGenerator.php | Removed getRelationName method (moved to base class) and constant |
| src/Generators/RequestsGenerator.php | Added getAvailableRelations method and passes relations to stub |
| stubs/request.blade.php | Updated to render relations array and conditionally show TODO comment |
| tests/fixtures/RequestGeneratorTest/search_request.php | Test fixture updated with populated relations |
| tests/fixtures/RequestGeneratorTest/get_request.php | Test fixture updated with populated relations |
| tests/fixtures/CommandTest/*.php | Test fixtures updated with empty array syntax |
| tests/Support/Model/ModelMockTrait.php | Removed extra blank line |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
src/Generators/EntityGenerator.php
Outdated
| return ucwords(Str::replace('/', '\\', $name), '\\'); | ||
| } | ||
|
|
||
| protected function getRelationName(string $relation, string $type): string |
Copilot
AI
Nov 3, 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.
Trailing whitespace at end of line should be removed.
| protected function getRelationName(string $relation, string $type): string | |
| protected function getRelationName(string $relation, string $type): string |
src/Generators/RequestsGenerator.php
Outdated
| $availableRelations = [ | ||
| ...$availableRelations, | ||
| ...Arr::map($entities, fn ($entity) => $this->getRelationName($entity, $type)), | ||
| ]; |
Copilot
AI
Nov 3, 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.
Variable reassignment inside loop is inefficient. The spread operator creates a new array on each iteration. Consider using array_push or collecting values first, then merging once after the loop.
| $availableRelations = [ | |
| ...$availableRelations, | |
| ...Arr::map($entities, fn ($entity) => $this->getRelationName($entity, $type)), | |
| ]; | |
| array_push( | |
| $availableRelations, | |
| ...Arr::map($entities, fn ($entity) => $this->getRelationName($entity, $type)) | |
| ); |
src/Generators/RequestsGenerator.php
Outdated
| $availableRelations = [ | ||
| ...$availableRelations, | ||
| ...Arr::map($entities, fn ($entity) => $this->getRelationName($entity, $type)), | ||
| ]; |
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.
| $availableRelations = [ | |
| ...$availableRelations, | |
| ...Arr::map($entities, fn ($entity) => $this->getRelationName($entity, $type)), | |
| ]; | |
| array_push( | |
| $availableRelations, | |
| ...Arr::map($entities, fn ($entity) => $this->getRelationName($entity, $type)), | |
| ); |
src/Generators/EntityGenerator.php
Outdated
| $pluralRelations = ['belongsToMany', 'hasMany']; | ||
|
|
||
| if (in_array($type, $pluralRelations)) { |
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.
| $pluralRelations = ['belongsToMany', 'hasMany']; | |
| if (in_array($type, $pluralRelations)) { | |
| if (in_array($type, ['belongsToMany', 'hasMany'])) { |
stubs/request.blade.php
Outdated
| @endif | ||
| @if($needToValidateWith) | ||
|
|
||
| @if(empty($availableRelations)) |
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.
the TODO should be added in all cases
src/Generators/ModelGenerator.php
Outdated
| protected function getRelationType(string $model, string $relation): string | ||
| { | ||
| if (in_array($relation, self::PLURAL_NUMBER_REQUIRED)) { | ||
| if (in_array($relation, ['belongsToMany', 'hasMany'])) { |
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.
let's implement the separate helper for it
| if (in_array($relation, ['belongsToMany', 'hasMany'])) { | |
| if ($this->isPluralRelation($relation)) { |
refs: #217