Skip to content

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

@Firehed

Description

@Firehed

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

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy