psalm icon indicating copy to clipboard operation
psalm copied to clipboard

Class constants where value starts with an operand are not recognized

Open bitwise-operators opened this issue 2 years ago • 3 comments

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

bitwise-operators avatar Jul 22 '22 06:07 bitwise-operators

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

psalm-github-bot[bot] avatar Jul 22 '22 06:07 psalm-github-bot[bot]

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.

bitwise-operators avatar Jul 22 '22 08:07 bitwise-operators

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.

AndrolGenhald avatar Jul 22 '22 15:07 AndrolGenhald

Fixed in #8360

weirdan avatar Nov 10 '22 05:11 weirdan