Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Console: Add type-safe getArgument and getOption methods to InputInterface #58406

Copy link
Copy link
Open
@Firehed

Description

@Firehed
Issue body actions

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)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.