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

Method [...] should return array but returns array #5861

Closed
acelaya opened this issue Nov 1, 2021 · 6 comments
Closed

Method [...] should return array but returns array #5861

acelaya opened this issue Nov 1, 2021 · 6 comments
Labels

Comments

@acelaya
Copy link
Sponsor

acelaya commented Nov 1, 2021

Bug report

I have just updated to PHPStan 1.0, and wanted to try the new level 9 on a small library.

I have a method set to return array, which basically wraps a call to json_decode. On first execution it threw Method [...] should return array but returns mixed, so I added a casting to array to explicitly comply with the type (even though the second param, associative, is true in my implementation).

After running phpstan again, then it throws Method [...] should return array but returns array.

Code snippet that reproduces the problem

class Foo {
    public function parse(string $rawJson): array
    {
        return (array) json_decode($rawJson, true, 512, JSON_THROW_ON_ERROR);
    }
}

Link reproducing it: https://phpstan.org/r/b82ca237-abc3-48ed-83a2-7d64551ea493

Expected output

PHPStan should not complain about that.

Did PHPStan help you today? Did it make you happy in any way?

PHPStan is just amazing, I have been using it for years (almost since it was first released).

It has added so much value to so many projects I have worked on, that I cannot express with words how greatful I am.

This is just a small bug that one would expect on a major release like this one.

Congratulations on the project, and keep up the good work.

@mergeable
Copy link

mergeable bot commented Nov 1, 2021

This bug report is missing a link to reproduction on phpstan.org.

It will most likely be closed after manual review.

@acelaya
Copy link
Sponsor Author

acelaya commented Nov 1, 2021

This bug report is missing a link to reproduction on phpstan.org.

It will most likely be closed after manual review.

Just added it

@dktapps
Copy link
Contributor

dktapps commented Nov 1, 2021

Seems like this might be something to do with the (array) cast itself. Similar issue: #5872

This sample works as expected: https://phpstan.org/r/655f3f3d-dde4-44fc-9558-18bce188ca16

@phpstan-bot
Copy link
Contributor

@acelaya After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-4: Method Foo::parse() return type has no value type specified in iterable type array.
-6: Method Foo::parse() should return array but returns array.
+4: Method Foo::parse() return type has no value type specified in iterable type array.
Full report
Line Error
4 Method Foo::parse() return type has no value type specified in iterable type array.

@stof
Copy link
Contributor

stof commented Nov 3, 2021

so I added a casting to array to explicitly comply with the type (even though the second param, associative, is true in my implementation).

associative: true guarantees you won't get a stdclass object. It does not guarantee you will get an array (the return type in such case is still int|float|string|bool|null|array)

MidnightDesign pushed a commit to MidnightDesign/phpstan-src that referenced this issue Nov 30, 2021
@github-actions
Copy link

github-actions bot commented Dec 5, 2021

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 Dec 5, 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

5 participants