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

Inconsistent error on generics #11041

Open
tamiroh opened this issue May 16, 2024 · 0 comments
Open

Inconsistent error on generics #11041

tamiroh opened this issue May 16, 2024 · 0 comments
Labels
Milestone

Comments

@tamiroh
Copy link

tamiroh commented May 16, 2024

Bug report

For the following code with generics,

<?php declare(strict_types = 1);

/**
 * @template TKey
 * @template TValue
 */
class Collection
{
    ...
}

/**
  * @param Collection<int, string|null> $collection
  */
function testFunc(Collection $collection): void
{

}

$collection = new Collection([0 => 'foo', 1 => 'bar', 2 => null, 3 => 'baz']);

PHPStan reports the following inconsistent errors.

Parameter #1 $collection of function testFunc expects Collection<int, string|null>, Collection<int, string|null> given.

From my few attempts, this seems to happen when...

  • The value of the collection is nullable
  • Bleeding edge is enabled
  • PHPStan Level is less than 8

Code snippet that reproduces the problem

https://phpstan.org/r/6cb19ed5-8774-4178-a21b-a7e19d1934f9

Expected output

No errors.

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

Yes, really helpful for finding bugs. Thank you so much! 😸

@ondrejmirtes ondrejmirtes added this to the Generics milestone May 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants