Skip to content

[Command] getOption return types #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mcfedr opened this issue Feb 14, 2019 · 3 comments · Fixed by #45
Closed

[Command] getOption return types #36

mcfedr opened this issue Feb 14, 2019 · 3 comments · Fixed by #45

Comments

@mcfedr
Copy link
Contributor

mcfedr commented Feb 14, 2019

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?

@lookyman
Copy link
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
Copy link
Member

ondrejmirtes commented Feb 14, 2019 via email

@github-actions
Copy link

github-actions bot commented May 1, 2021

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 1, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants