Refactor Services and CLI commands to use DTOs#175
Conversation
📝 WalkthroughWalkthroughCommands and services were refactored to use new DTO classes for Posts, Comments, and Users. Commands construct DTOs from inputs and pass them to services; service method signatures were updated to accept DTOs and use their serialized data for model operations. Tests and helpers were updated accordingly. Changes
Sequence Diagram(s)mermaid Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (2)
shared/Commands/CommentCreateCommand.php (1)
80-84: Normalize CLIcontentbefore DTO creation for consistency.
CommentDTO::fromRequest()trims content, but the CLI path passes raw content. Aligning both paths avoids inconsistent stored values.Proposed tweak
$commentDto = new CommentDTO( $this->getArgument('post_uuid'), $this->getArgument('user_uuid'), - $this->getArgument('content') + trim((string)$this->getArgument('content')) );🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@shared/Commands/CommentCreateCommand.php` around lines 80 - 84, The CLI path creates CommentDTO with raw content causing inconsistency with CommentDTO::fromRequest() which trims content; update the construction in CommentCreateCommand (the new CommentDTO(...) call) to normalize the content argument (e.g., apply trim on $this->getArgument('content') or otherwise replicate the same normalization used by CommentDTO::fromRequest()) so CLI-created DTOs match request-created DTOs.shared/Commands/PostCreateCommand.php (1)
57-63: Unuseduuidargument can be removed.The
uuidargument (line 61) is no longer used sincePostService::addPost()now generates the UUID internally viauuid_ordered(). This dead code can be cleaned up.Suggested fix
protected array $args = [ ['title', 'required', 'Post title'], ['description', 'required', 'Post description'], ['user_uuid', 'required', 'Author uuid'], - ['uuid', 'optional', 'Post uuid'], ['image', 'optional', 'Post image'], ];🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@shared/Commands/PostCreateCommand.php` around lines 57 - 63, The args array in PostCreateCommand still declares a 'uuid' entry even though PostService::addPost() now generates UUIDs internally via uuid_ordered(); remove the unused ['uuid', 'optional', 'Post uuid'] element from the protected array $args in the PostCreateCommand class to eliminate dead code and keep the command signature consistent with PostService::addPost().
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@shared/Commands/UserCreateCommand.php`:
- Line 102: The command is breaking the DTO boundary by calling
service(AuthService::class)->add($userDto->toArray()); — change the
AuthService::add signature to accept the UserDTO object (e.g., UserDTO $userDto)
and update this call-site in UserCreateCommand to pass $userDto (not
->toArray()); then move the array mapping/normalization logic into
AuthService::add (or a private mapper method) so all mapping from UserDTO to
persistence/input arrays happens inside AuthService; also update any other
callers of AuthService::add to use the new UserDTO parameter.
In `@shared/DTOs/UserDTO.php`:
- Around line 146-156: The toArray() method in UserDTO is currently removing
keys with empty strings (''), which drops fields like role and image; update the
array_filter callback in UserDTO::toArray to only filter out null values (i.e.,
return $value !== null) so empty strings are preserved and keys remain present
in the serialized output, ensuring persisted defaults and response parity are
not altered.
---
Nitpick comments:
In `@shared/Commands/CommentCreateCommand.php`:
- Around line 80-84: The CLI path creates CommentDTO with raw content causing
inconsistency with CommentDTO::fromRequest() which trims content; update the
construction in CommentCreateCommand (the new CommentDTO(...) call) to normalize
the content argument (e.g., apply trim on $this->getArgument('content') or
otherwise replicate the same normalization used by CommentDTO::fromRequest()) so
CLI-created DTOs match request-created DTOs.
In `@shared/Commands/PostCreateCommand.php`:
- Around line 57-63: The args array in PostCreateCommand still declares a 'uuid'
entry even though PostService::addPost() now generates UUIDs internally via
uuid_ordered(); remove the unused ['uuid', 'optional', 'Post uuid'] element from
the protected array $args in the PostCreateCommand class to eliminate dead code
and keep the command signature consistent with PostService::addPost().
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: b1b6124f-94e7-4649-8d96-813a60bcbfe3
📒 Files selected for processing (12)
shared/Commands/CommentCreateCommand.phpshared/Commands/PostCreateCommand.phpshared/Commands/PostUpdateCommand.phpshared/Commands/UserCreateCommand.phpshared/DTOs/CommentDTO.phpshared/DTOs/PostDTO.phpshared/DTOs/UserDTO.phpshared/Services/CommentService.phpshared/Services/PostService.phptests/Helpers/functions.phptests/Unit/shared/Services/CommentServiceTest.phptests/Unit/shared/Services/PostServiceTest.php
… restore uuid pass-through in PostDTO/PostCreateCommand
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
shared/Commands/PostCreateCommand.php (1)
83-89: Validateuser_uuid/uuidbefore creatingPostDTO.The command validates title/content, but identifier fields used in the DTO are not validated in
validationRules(). Add UUID-format checks before instantiation to keep bad IDs out of service/model layers.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@shared/Commands/PostCreateCommand.php` around lines 83 - 89, The command constructs a PostDTO with identifier fields that aren’t validated; update PostCreateCommand::validationRules() to include UUID-format validators for both 'user_uuid' and 'uuid' (the inputs passed into the PostDTO), run the validation and return/throw on failure before the PostDTO instantiation, and ensure PostCreateCommand uses the validated values when calling new PostDTO so malformed IDs never reach the service/model layers.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@shared/Services/PostService.php`:
- Around line 183-186: In updatePost (PostService::updatePost), prevent UUID
mutation by not copying uuid from the incoming PostDto into the model: when
converting $postDto to an array before calling $post->fill(...) remove or
exclude the 'uuid' key (or explicitly only map allowed mutable fields) so the
model's $post->uuid remains unchanged; then save and return via
$this->getPost($post->uuid) as before.
---
Nitpick comments:
In `@shared/Commands/PostCreateCommand.php`:
- Around line 83-89: The command constructs a PostDTO with identifier fields
that aren’t validated; update PostCreateCommand::validationRules() to include
UUID-format validators for both 'user_uuid' and 'uuid' (the inputs passed into
the PostDTO), run the validation and return/throw on failure before the PostDTO
instantiation, and ensure PostCreateCommand uses the validated values when
calling new PostDTO so malformed IDs never reach the service/model layers.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 4ea8d72a-03f4-4afc-8473-d20f9fb39215
📒 Files selected for processing (5)
shared/Commands/CommentCreateCommand.phpshared/Commands/PostCreateCommand.phpshared/DTOs/PostDTO.phpshared/DTOs/UserDTO.phpshared/Services/PostService.php
✅ Files skipped from review due to trivial changes (1)
- shared/Commands/CommentCreateCommand.php
🚧 Files skipped from review as they are similar to previous changes (2)
- shared/DTOs/UserDTO.php
- shared/DTOs/PostDTO.php
| $post->fill($postDto->toArray()); | ||
| $post->save(); | ||
|
|
||
| return $this->getPost($post->uuid); |
There was a problem hiding this comment.
Prevent UUID mutation in updatePost.
$postDto->toArray() may include uuid, which allows changing the record identity during update. Keep UUID immutable in updates and fill only mutable fields.
Suggested fix
public function updatePost(string $uuid, PostDTO $postDto): Post
{
$post = $this->model->findOneBy('uuid', $uuid);
- $post->fill($postDto->toArray());
+ $payload = $postDto->toArray();
+ unset($payload['uuid']);
+ $post->fill($payload);
$post->save();
- return $this->getPost($post->uuid);
+ return $this->getPost($uuid);
}🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@shared/Services/PostService.php` around lines 183 - 186, In updatePost
(PostService::updatePost), prevent UUID mutation by not copying uuid from the
incoming PostDto into the model: when converting $postDto to an array before
calling $post->fill(...) remove or exclude the 'uuid' key (or explicitly only
map allowed mutable fields) so the model's $post->uuid remains unchanged; then
save and return via $this->getPost($post->uuid) as before.
Closes #171
Summary by CodeRabbit
Refactor
Tests