Skip to content

[Command] getOption return types #36

Closed
@mcfedr

Description

@mcfedr
Contributor

This is a question, I'd be interested in implementing this, but not sure if its possible with Phpstan, so a point in the right direction would be useful.

In Symfony commands options (and arguments) are added in configure function, and this affects the return type of $input->getOption later.

class MyCommand {
    
    protected function configure(): void
    {
        $this
            ->addOption('str', null, InputOption::VALUE_REQUIRED, 'This option will be ?string')
            ->addOption('bool', null, InputOption::VALUE_NONE, 'this option will be bool')
            ->addOption('arra', null, InputOption::VALUE_IS_ARRAY, 'this option will be ?(string[])')
        ;
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $str = $input->getOption('str');
    }
}

So can I change the return type of getOption, according to the options that are configured?

Activity

lookyman

lookyman commented on Feb 14, 2019

@lookyman
Collaborator

I though about this before and so far I am not sure how to implement it. For example, you can have the options defined in a parent, and you should still be able to recognize their types. It gets quite complicated. I have vague ideas about using type specifying extension (define some dummy variables in the command's scope, see PHPStan\Type\Symfony\Helper), but nothing concrete at the moment. You can certainly give it a go, I would appreciate it!

Also, #20.

ondrejmirtes

ondrejmirtes commented on Feb 14, 2019

@ondrejmirtes
Member
github-actions

github-actions commented on May 1, 2021

@github-actions

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

locked as resolved and limited conversation to collaborators on May 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @ondrejmirtes@mcfedr@lookyman

      Issue actions

        [Command] getOption return types · Issue #36 · phpstan/phpstan-symfony