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

Wrong return type #1903

Closed
nospor opened this issue Feb 15, 2019 · 8 comments
Closed

Wrong return type #1903

nospor opened this issue Feb 15, 2019 · 8 comments
Labels
Milestone

Comments

@nospor
Copy link

nospor commented Feb 15, 2019

Hi, I have a function which returns an array but phpstan says, it returns null

Method Test::test() should return array but returns null. 

This is the code:

class Test {
    private $answersOrder = [];
    public function test(string $qId): array
    {
        if (null !== $this->answersOrder[$qId]) {
            return $this->answersOrder[$qId];
        }


        $this->answersOrder[$qId] = 5;

        return $this->answersOrder[$qId];
    }

}

As you can see it always returns an array. I think that first IF block confuses phpstan somehow

Expected output

There shouldnt be any error for that class

@ondrejmirtes
Copy link
Member

phpstan.org reproduction (please always include it): https://phpstan.org/r/8e8c10ec-47bf-4868-b8d4-3d5446d9950e

BTW: Your code is wrong, the right expectation is:

Method Test::doFoo() should return array but returns int.

Not "no errors".

@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Feb 15, 2019
@nospor
Copy link
Author

nospor commented Feb 15, 2019

Yes, of course. All you said is right. I will remember next time about reproduction link and yes for that example expectation error should be what you said. I just modified my original big file and set INT instead of []. Still, problem with null shouldnt appear :) Ta

@adaamz
Copy link
Contributor

adaamz commented Feb 18, 2019

And maybe you should not compare to null, but check if key existence via isset/array_key_exists.

I added expected phpdoc of property and there is output:
https://phpstan.org/r/d2fa450d-6c9c-496d-8109-f833ccde31c1

@nospor
Copy link
Author

nospor commented Feb 18, 2019

@CzechBoy yes, in this current simple example you could have a right but I just cutted it from bigger context where there was null value

@adaamz
Copy link
Contributor

adaamz commented Feb 18, 2019

@nospor
Copy link
Author

nospor commented Feb 18, 2019

Ok, let me show you full class without any cutting
https://phpstan.org/r/2564889d-74bc-4172-b7f6-7a3f1da9485c

The last error
Method CalculationSorter::getSortedAnswers() should return array but returns null.
I tried to put your docblock there with different variations but it didnt fix the error.

This is not my class, I just run phpstan on one of our projects and I found that problem. But now when I check once more that class I do not understand why there is comparing to null. There should be as you said just checking isset. When I replace it to checking isset then that problem do not appear so thank you for that.

but still I think there is some problem with phpstan analysing for my original example from first post, dont you think?

@ondrejmirtes
Copy link
Member

Yes, definitely, the original example is sufficient to reproduce this, thanks.

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 29, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants