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

Class constants where value starts with an operand are not recognized #8307

Closed
bitwise-operators opened this issue Jul 22, 2022 · 4 comments
Closed

Comments

@bitwise-operators
Copy link
Contributor

If the value for a class constant starts with an operand, like ! or ~, Psalm doesn't register it, and throws an UndefinedConstant error.

https://psalm.dev/r/b100090d84

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/b100090d84
<?php
class test {
	public const BITFLAG_ALL = ~0;
	public const BOOL_TRUE = !false;
}

print_r(test::BITFLAG_ALL);
print_r(test::BOOL_TRUE ? 'true' : 'false');
Psalm output (using commit f6fb715):

ERROR: UndefinedConstant - 7:9 - Constant test::BITFLAG_ALL is not defined

ERROR: UndefinedConstant - 8:9 - Constant test::BOOL_TRUE is not defined

@bitwise-operators
Copy link
Contributor Author

As an aside, I've had a quick look through the code to see if I could fix this, but I'm having a hard time figuring out what part of the code this is likely to be in. If someone could give a pointer where this is likely to originate, I'd be happy to give it another go.

@AndrolGenhald
Copy link
Collaborator

AndrolGenhald commented Jul 22, 2022

My first guess would be it's a parsing issue in either ParseTreeCreator or TypeParser, but it's possible the issue is somewhere else as well (and the fact that there's no parse error makes me doubt that guess). Still, if you add a breakpoint to see when the type is parsed, maybe you can follow it through and see why it's not being set properly.

Second guess would be ClassLikeNodeScanner::visitClassConstDeclaration, that's probably where I'd start. I bet SimpleTypeInferer::infer needs to be improved to support more unary ops.

@weirdan
Copy link
Collaborator

weirdan commented Nov 10, 2022

Fixed in #8360

@weirdan weirdan closed this as completed Nov 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants