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

Namespaced enum typing on constants behaving inconsistent #10212

Closed
jnoordsij opened this issue Nov 28, 2023 · 3 comments
Closed

Namespaced enum typing on constants behaving inconsistent #10212

jnoordsij opened this issue Nov 28, 2023 · 3 comments

Comments

@jnoordsij
Copy link

jnoordsij commented Nov 28, 2023

Bug report

In PHP 8.3 it is now possible to add types to class constants. However, when using a namespaced enum type, e.g. one imported from another file, the inferred type reported by PHPStan seems to be incorrect when evaluating the type of the constant, causing a mismatch about constants not accepting a value. See also the snippet.

Code snippet that reproduces the problem

https://phpstan.org/r/46a1d296-5f74-44d0-a5de-a2dd4ba1ff7c

<?php declare(strict_types = 1);

namespace X;

enum Foo
{
	case Bar;
}

class HelloWorld
{
	public const string A = 'foo';
	public const X\Foo B = Foo::Bar;
	public const Foo C = Foo::Bar;
}

\PHPStan\dumpType(Foo::Bar);
\PHPStan\dumpType(HelloWorld::C);

outputs

14 | Constant X\HelloWorld::C (Foo) does not accept value X\Foo::Bar.     Non-ignorable

Expected output

No errors.

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

It's great that PHPStan is already so up-to-date with new PHP 8.3 syntax and ready to use for most cases! That makes me feel a lot more secure about code stability after upgrading my applications to the new version. Thanks!

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src@8234dc0

This is a bug in nikic/php-parser, I'm gonna send a patch there, meanwhile I fixed it with a patch here in phpstan-src :)

@jnoordsij
Copy link
Author

Thanks for the quick action!

Copy link

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 30, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants