-
Notifications
You must be signed in to change notification settings - Fork 132
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
feat: add UserModel::getReturnType()
and RegisterController::getUserEntity() uses it
#1172
base: develop
Are you sure you want to change the base?
feat: add UserModel::getReturnType()
and RegisterController::getUserEntity() uses it
#1172
Conversation
src/Models/UserModel.php
Outdated
@@ -395,4 +395,9 @@ private function checkReturnType(): void | |||
throw new LogicException('Return type must be a subclass of ' . User::class); | |||
} | |||
} | |||
|
|||
public function getUserEntity(): User |
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.
public function getUserEntity(): User | |
/** | |
* Returns User Entity Type | |
* | |
* @return class-string<User> | |
*/ | |
public function getReturnType(): string |
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.
Is it better to add createNewUser(array $data): User
or something instead of getReturnType(): string
?
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.
That would omit the UserEntity entirely so we would still not be able to use UserEntity during the RegistrationAction?
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.
If we add the following method to UserModel
:
/**
* Returns a new User Entity.
*
* @param array<string, mixed> $data The data passed to the constructor
*/
public function createNewUser(array $data = []): User
{
return new $this->returnType($data);
}
RegisterController
would be like this:
protected function getUserEntity(): User
{
$userProvider = $this->getUserProvider();
return $userProvider->createNewUser();
}
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.
@christianberkman
I think adding a method to create a new user is better than a method that just returns the user type string.
Would you like to make such an update?
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.
Sorry typo. I meant
So do you propose the following? (My last code snippet)
I also read your snippet with createNewUser wrong and actually we are on the same line indeed.
Is it now better to open a fresh pull request since this differs from the original suggestion?
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.
Yes, it would be better to open a fresh PR.
And I propose to add the first parameter:
/**
* Returns a new User Entity.
*
* @param array<string, mixed> $data (Optional) user data
*/
public function createNewUser(array $data = []): User
{
return new $this->returnType($data);
}
Then, a dev can create a complete new User entity.
If a dev want to use Complete Constructor to the custom User entity, s/he needs complete data to construct a new entity.
Also, the CI4 Entity permits an empty instance creation, so we can just call it without arguments.
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.
I personally don't like using mixed
, and I prefer to explicitly define the possible types as much as we can. For any other unexpected values, we can wait for bug reports from users.
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.
If we don't use mixed
, something like this:
array<string, array|bool|int|float|null|object|string>
But it contains array
, and it is not acceptable. Therefore,
array<string, array<array-key, array|bool|int|float|null|object|string>|bool|int|float|null|object|string>
But still contains array
... Give up and use mixed
.
array<string, array<array-key, mixed>|bool|int|float|null|object|string>
Which is better?
@param array<string, mixed> $data (Optional) user data
@param array<string, array<array-key, mixed>|bool|int|float|null|object|string> $data (Optional) user data
or something else?
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.
My choice is the second option because it strikes a balance between overly vague type definitions and overwhelming complexity. While it's not ideal, it offers a reasonable level of flexibility and clarity.
@param array<string, array<array-key, mixed>|bool|int|float|null|object|string> $data (Optional) user data
UserModel::getReturnType()
and RegisterController::getUserEntity() uses it
Co-authored-by: kenjis <[email protected]>
I sent #1196 |
Description
See #1170
Refactor
RegisterController::getUserEntity
to useUserModel::returnType
instead of the hard codednew User()
. This allows for easy use of a custom User Entity without needing to extend theRegisterController
and add the routes.Checklist: