Skip to content

Incorrect type for Enum $this when calling generic method #7211

@ndench

Description

@ndench

Bug report

Code snippet that reproduces the problem

<?php declare(strict_types = 1);

enum Foo {
	case Bar;
	case Baz;
	case Gar;
	case Gaz;
	
	public function startsWithB(): bool
	{
		return inArray($this, [static::Baz, static::Bar]);
	}
}

/**
* @template    T
* @psalm-param T $needle
* @psalm-param array<array-key, T> $haystack
*/
function inArray(mixed $needle, array $haystack): bool
{
	return \in_array($needle, $haystack, true);
}

Phpstan gives an error in the Foo::startsWithB function:

Parameter #2 $haystack of function inArray expects array<$this(Foo)>, array<int, Foo::Bar|Foo::Baz> given.

It seems to that $this(Foo) does not match Foo::Bar|Foo::Baz, whereas I think that it should.

https://phpstan.org/r/102df9d0-30af-4810-a9de-d3e4ba87fe5f

Expected output

I don't think that it should be an error at all.

As a side note, Psalm doesn't get an error: https://psalm.dev/r/87ee1f1051

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

I love generics provided by both phpstan and psalm. It allows me to write much more clean and succinct code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions