-
-
Notifications
You must be signed in to change notification settings - Fork 863
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
polymorphic string based checks #1330
Comments
Hi, can you express yourself more clearly? Looks like you’re talking about
two different things (there’s no switch in the linked playground) but I’m
not sure.
On Sat, 4 Aug 2018 at 13:28, Scott Dutton ***@***.***> wrote:
I'm not sure if this is an error, as the code should check in a better way
but I have come across the following
https://phpstan.org/r/051b7ab22cbb5241e32200e723a0bf3c
This should use instanceof ideally which resolves the issue, just
wondering if code like this should also verify ?
Using something like
switch ($object->getType()) {
case something::class :
}
vs
switch (true) {
case $object instanceof something :
}
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#1330>, or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAGZuFmpIXQi_S2djqvolM_8GGQSM1mNks5uNYV0gaJpZM4Vu9fa>
.
--
Ondřej Mirtes
|
I think that the idea is that when |
This might be doable with a custom type-specifying extension, you can find out about them in the README and there's plenty of examples in the codebase: function test(contract $c) {
if ($c->getType() === concrete::class) {
concrete($c);
}
} Supporting it in a switch (true) {
case $foo instanceof Foo:
} And: switch (get_class($foo)) {
case Foo::class:
} Waiting for confirmation from @exussum12 that this is solved so I can close this. |
Sorry, reading it back its not too clear https://phpstan.org/r/25cbd9a4d5f1e16972e503079d3819b7 The result of the two loops are really identical, though the top loop checks by comparing strings and bottom loop checks by using instanceof. Not sure if this should be a bug, as although its type safe - knowing that by comparing strings doesn't feel right. This is only an issue with late static bindings. Hopefully this example makes the issue more clear. The code can 100% be improved here, its just as an example |
Yeah, PHPStan has no way of understanding the connection between |
ok so a custom type check would be fine if the switch was converted to a few ifs ? |
I'm now realizing that you can't write an extension for What are you implementing using the |
Im not implementing this is in older code. I have moved this for now to the switch (true) method, I just raised the bug report as technically the code was correct before also. Though I understand why it wouldnt be picked up as being right |
I'm not sure if this is an error, as the code should check in a better way but I have come across the following
https://phpstan.org/r/051b7ab22cbb5241e32200e723a0bf3c
This should use instanceof ideally which resolves the issue, just wondering if code like this should also verify ?
Using something like
vs
The text was updated successfully, but these errors were encountered: