I'm trying - yet again - DevSense PHP Tools to see if I can work with it.
And I'm hitting a barrier with this.
There still some times where the type isn't properly detected. We can 'help' the IDE then by using /** @var */ or by using a statement like if ($foobar instanceof FooBar) { }
.
But the goal is to get rid of all ugly docblock where possible, and to prevent a runtime check just to tell your IDE something while coding.
So from what I gather, the way that is most popular now (but this might be my bubble) is to use an assesrt()
statement.
A line like assert($foobar instanceof FooBar);
gets recognized by PHPstan and Intelephense, so from that moment it recognizes $foobar as an instance of that type.
The default php settings will have this be 'compiled away' by the interpreter in production settings, while you have the option during development to trigger an error if the assert() doesn't match. Seems like the best of both worlds.
Unfortunately, PHPTools doesn't recognize it. Worse, if I start typing /**
to start a one-liner to cast with @var, it starts a multiline docblock comment, indented and all right there in the function?? It's quite annoying to have to undo that scaffolding again.
There also seems to be no object autocomplete while typing in a @var line.
I expect to end up with something like this:
But, as I said, better yet: No @var at all, without having to do an if ($a instanceof B)
check and throwing an exception that will never be triggered.