Skip to content
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

Merged
merged 2 commits into from
Jul 4, 2017

Conversation

SamMousa
Copy link
Contributor

Q A
Is bugfix? no
New feature? no
Breaks BC? no
Tests pass? yes

This is a small code quality improvement. Related to #14184

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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, even better :)

@samdark samdark added this to the 2.0.13 milestone May 22, 2017
@SamMousa
Copy link
Contributor Author

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')) {
Copy link
Member

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.

Copy link
Contributor Author

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;
    }

Copy link
Member

@samdark samdark left a 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.

@samdark samdark self-assigned this Jun 20, 2017
@cebe
Copy link
Member

cebe commented Jul 3, 2017

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.

@SamMousa
Copy link
Contributor Author

SamMousa commented Jul 3, 2017

@cebe Depending on the implementation there could be large differences.

If the API offers a has and a get then for checking it makes sense to use has. There could be an implementation where checking is a lot cheaper than actually retrieving.

Why even offer has if it is better to just check the result of get?

@samdark
Copy link
Member

samdark commented Jul 3, 2017

has() is there for cases when you don't need the value but actually need to check if there's something or not.

@samdark
Copy link
Member

samdark commented Jul 3, 2017

@SamMousa how about #14185 (comment)?

@SamMousa
Copy link
Contributor Author

SamMousa commented Jul 3, 2017

@samdark See the comment I just added.
@cebe Using $app->has does not instantiate the component; $app->get() does.

If your code does this:

if ($app->get('test', false) === null) {
    // Some stuff here.
    $app->get('test')->someFunc();
    $app->get('someOtherComponent')->test();
}

It doesn't matter what you use. But if then, 2 years later you don't need the 3rd line and you remove it:

if ($app->get('test', false) === null) {
    // Some stuff here.
    $app->get('someOtherComponent')->test();
}

Now you're instantiating a component you no longer need; you just need to know it exists.

@cebe
Copy link
Member

cebe commented Jul 3, 2017

But if then, 2 years later you don't need the 3rd line

I am fine with changing it to has() two years later then ;)

@SamMousa
Copy link
Contributor Author

SamMousa commented Jul 3, 2017

But who says it is actually @cebe making the change? The point I was trying to make is that the code is not descriptive.
You're not getting a component you're checking if the application knows about a component.
It doesn't matter if you get it or not 2 lines later, in the guard of the if statement you are just checking existence.

Copy link
Member

@dynasource dynasource left a 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

@samdark samdark merged commit e34505c into yiisoft:master Jul 4, 2017
@samdark
Copy link
Member

samdark commented Jul 4, 2017

Merged. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants