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 type for Enum $this when calling generic method #7211

Closed
ndench opened this issue May 10, 2022 · 2 comments
Closed

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

ndench opened this issue May 10, 2022 · 2 comments
Labels
Milestone

Comments

@ndench
Copy link
Contributor

ndench commented May 10, 2022

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.

@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Jun 2, 2022
@phpstan-bot
Copy link
Contributor

@ndench After the latest commit in 1.8.x, PHPStan now reports different result with your code snippet:

@@ @@
-PHP 8.1 (1 error)
+PHP 8.1
 ==========
 
-11: Parameter #2 $haystack of function inArray expects array<$this(Foo)>, array<int, Foo::Bar|Foo::Baz> given.
+No errors
 
 PHP 7.1 – 8.0 (2 errors)
 ==========
Full report

PHP 8.1

No errors

PHP 7.1 – 8.0 (2 errors)

Line Error
3 Syntax error, unexpected T_STRING on line 3
13 Syntax error, unexpected '}', expecting EOF on line 13

@github-actions
Copy link

github-actions bot commented Aug 2, 2022

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 Aug 2, 2022
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