-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
Removed non-default call of $app-get()
in favor of using $app->has()
#14185
Removed non-default call of $app-get()
in favor of using $app->has()
#14185
Conversation
return $user && !$user->isGuest ? $user->id : null; | ||
if ($this->value === null && Yii::$app->has('user')) { | ||
$user = Yii::$app->get('user'); | ||
return !$user->isGuest ? $user->id : null; |
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.
this equal:
return $user->getId();
https://github.com/yiisoft/yii2/blob/master/framework/web/User.php#L338
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.
Nice, even better :)
I think this can be merged. |
if ($this->value === null) { | ||
$user = Yii::$app->get('user', false); | ||
return $user && !$user->isGuest ? $user->id : null; | ||
if ($this->value === null && Yii::$app->has('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.
!$user->isGuest
check is missing. Because of that behavior is not the same as it was.
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.
Actually this is already part of the getter:
public function getId()
{
$identity = $this->getIdentity();
return $identity !== null ? $identity->getId() : null;
}
public function getIsGuest()
{
return $this->getIdentity() === null;
}
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.
Changed code doesn't look like equivalent of the previous code.
why do you think it is better to call two methods instead of one? Or what exactly is the improvement? I see no reason to change it. |
@cebe Depending on the implementation there could be large differences. If the API offers a Why even offer |
|
@SamMousa how about #14185 (comment)? |
@samdark See the comment I just added. If your code does this:
It doesn't matter what you use. But if then, 2 years later you don't need the 3rd line and you remove it:
Now you're instantiating a component you no longer need; you just need to know it exists. |
I am fine with changing it to |
But who says it is actually @cebe making the change? The point I was trying to make is that the code is not descriptive. |
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.
it's better readable, although it is less obvious that guests return null
Merged. Thanks! |
This is a small code quality improvement. Related to #14184