Open tscni opened 1 year ago
I found these snippets:
Yeah, I remember banging my head on that multiple times :p
the issue with $definite_type is that unless a class is final, it's pretty much impossible to know. (Even if there is no child known for this type, it may be a library that expect people to implement their own)
There's definitely room for improvement anyway!
At least for cases where the type after the "instanceof" is a literal (and not a variable) this is relatively simple to fix via a plugin (via AfterExpressionAnalysisEvent for Instanceof_) so that the negation at least reports a redundant condition (https://psalm.dev/r/4ca7336f15) I guess that can be done similarly in psalm itself directly, just not with the assertion system, since we don't know at that stage if it's a literal or not.
I found these snippets:
Some examples with variable re-definitions: https://psalm.dev/r/2cf106db54 In
a()
,$x
should be'x'
Inb()
,$x
should beX
Inc()
, the instanceof check should not be redundantd()
works correctlyI mainly include the class-string cases because them working (in general) is the cause of the issue. This relates to https://github.com/vimeo/psalm/pull/6739 and https://github.com/vimeo/psalm/pull/6746 Specifically (in the negated case) this part of the code: https://github.com/vimeo/psalm/blob/56d7b3793eca1e61036af64fe00d49ad69e3935b/src/Psalm/Internal/Type/NegatedAssertionReconciler.php#L198-L204
I'm currently trying to figure out how I can differentiate
class-string<X>
fromX
or'X'
, which would be necessary to fix the issue. Maybe I could implement something similar toTNamedObject::$definite_class
, e.g. a$definite_type
which would be true if it's definitelyX
/'X'
, but not if it'sclass-string<X>