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
Accept ::class as literal string #1692
Conversation
I disagree with this fix. class-string isn't always literal string. |
@craigfrancis didn't give an example of class-string which are not literal-string in phpstan/phpstan-doctrine#332 (comment) But I think we should follow the way it's handled by psalm |
// $s is string
if (class_exists($s)) {
// should $s suddenly be a literal-string?
} |
[I’m out atm, and using phone, will try to check properly later] The source of the variable has to be a literal, so if you got it from |
That's what I thought, so this PR has to be fixed with my suggestion. Thank you. |
26d06aa
to
e71a0dc
Compare
thanks for the input guys. I just committed an update. |
The spurious out of memory error could also be observed on other PRs. I don‘t think it is related to the actual change |
$reflection = $type->getClassReflection(); | ||
if ($reflection !== null && $reflection->isFinalByKeyword()) { | ||
return new ConstantStringType($reflection->getName(), true); | ||
$stringType = new ConstantStringType($reflection->getName(), true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ConstantStringType
is already considered to be a literal string, so intersecting with AccessoryLiteralStringType
will just remove the AccessoryLiteralStringType
again.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
great catch! fixed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd say that the ifs above should also be covered by this PR. Basically any $obj::class
is a literal-string.
So with $obj::class
and $obj
being EnumCaseObjectType, it should also be literal-string. And with $type instanceof TemplateType && !$type instanceof TypeWithClassName
that should work too.
adjusted per feedback. I am thinking whether a /**
* @param object $o
*/
function a($o): void
{
assertType('class-string&literal-string', $o::class);
sayHello($o::class);
} |
Of course :) |
Please rebase and solve the conflict, I'll merge it then. |
Thank you! |
closes phpstan/phpstan#7823
closes phpstan/phpstan-doctrine#332