Description
Description
With the advent of modern static analysis tools such as PHPStan and Psalm, the mixed
return type from InputInterface
's getArgument
and getOption
has become somewhat tedious to interact with. I generally know at development time what the type of those return values will be based on the flags provided during the command's configuration.
It would be a nice DX improvement if there were some type-safe way to get CLI input values. This would allow removing a lot of inline assertions and streamline console command implementations a decent amount.
IMO, the most straightforward approach would be adding a set of getArgumentAsString
(etc) methods. I don't think implementation would be too challenging, and it could probably be applied easily with a packaged trait. But it's somewhat tedious and potentially a serious BC break for any (I assume rare) custom implementations.
I could also see, potentially, having an optional second parameter to the existing getArgument
and getOption
methods. This, theoretically, is a bit cleaner from a BC perspective. However, for this to actually address the need of reducing inline assertions for type checkers, there would probably need to be some pretty clever generics defined on the methods. I'm not entirely sure these are possible, and they may be tooling-specific.
Thoughts? Thanks for considering!
Example
$stringArgument = $input->getArgumentAsString('name');
// or
$stringArgument = $input->getArgument('name', InputArgument::AS_STRING)