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

Incorrect return type when calling unique on an Eloquent Collection #652

Closed
Daanra opened this issue Sep 3, 2020 · 2 comments
Closed

Incorrect return type when calling unique on an Eloquent Collection #652

Daanra opened this issue Sep 3, 2020 · 2 comments
Labels
bug Something isn't working

Comments

@Daanra
Copy link
Contributor

Daanra commented Sep 3, 2020

  • Larastan Version: 0.6.4
  • PHPStan Version: 0.12.42
  • --level used: max

Description

When calling unique() on an Eloquent Collection (or a custom Collection class), the return type is inferred as Illuminate\\Support\\Collection, however, this is incorrect.

Laravel's PHPDoc seems to be correct, and it doesn't seem like we're overriding it with a stub. So I'm unsure why this is happening.

Laravel code where the issue was found

use Illuminate\Database\Eloquent\Collection as EloquentCollection;

// ...

public function testCollectionUnique(EloquentCollection $collection): EloquentCollection
{
    return $collection->unique();
}

Error output:

Method Tests\\Features\\ReturnTypes\\CustomEloquentCollectionTest::testCollectionUnique() should return Illuminate\\Database\\Eloquent\\Collection but returns Illuminate\\Support\\Collection.
@szepeviktor szepeviktor added the bug Something isn't working label Sep 3, 2020
@canvural
Copy link
Collaborator

canvural commented Sep 4, 2020

I think the return type in Laravel is wrong. It should be just static There is no need for union. Changing it to just static makes your test case pass.

Edit: I think PHPStan normalizes the type static|\Illuminate\Support\Collection to \Illuminate\Support\Collection because it's the parent class.

But I couldn't figure out in which cases can it return Support/Collection instead of Eloquent/Collection Do you have any example?

@Daanra
Copy link
Contributor Author

Daanra commented Sep 7, 2020

Edit: I think PHPStan normalizes the type static|\Illuminate\Support\Collection to \Illuminate\Support\Collection because it's the parent class.

Thanks for pointing me in the right direction, I wasn't aware of this. This is kind of weird though. static|\Illuminate\Support\Collection is a perfectly valid type and is definitely not equivalent to just \Illuminate\Support\Collection. Anyhow, this is more a PHPStan problem.

The unique return type was indeed incorrect, so I fixed it here.

@Daanra Daanra closed this as completed Sep 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants