Skip to content

Help getArgument/getOption in Symfony Console InputInterface #20

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
Jean85 opened this issue Nov 16, 2018 · 3 comments · Fixed by #45
Closed

Help getArgument/getOption in Symfony Console InputInterface #20

Jean85 opened this issue Nov 16, 2018 · 3 comments · Fixed by #45
Labels
help wanted Extra attention is needed

Comments

@Jean85
Copy link

Jean85 commented Nov 16, 2018

Symfony had a few recent PRs to the Symfony console that impacted my PHPStan analysis due to PHPDoc changes:

symfony/symfony#28448
symfony/symfony#28374
symfony/symfony#28647

Bottom line:

  • InputInterface::getArgument now returns string|string[]|null
  • InputInterface::getOption returns string|string[]|bool|null.

In reality, they can return string[] only if the relative argument/option is set as InputArgument::IS_ARRAY/InputOption::VALUE_IS_ARRAY.

Is it possible to add an aid in this package for this case? The minimum repro case is this one:

class PhpstanCommand extends \Symfony\Component\Console\Command\Command
{
    protected function configure()
    {
        $this->addArgument('argument', InputArgument::REQUIRED);
        $this->addOption('option', null, InputOption::VALUE_REQUIRED);
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        // Cannot cast array<string>|string|null to string. 
        $argument = (string) $input->getArgument('argument');
        // Cannot cast array<string>|bool|string|null to string. 
        $option = (string) $input->getOption('option');
    }
}
@lookyman
Copy link
Collaborator

I am not sure this is possible, since the options are set in a different method on different objects. This would probably require a much deeper analysis of the source code then I can imagine right now. But it is a really annoying thing to ignore these errors every time, so I can give it a go. Or you can always send a PR if you have an idea how to solve it..

@vasilvestre
Copy link

I really wonder how to configure it but PHP packages. The container isn't found and wrong types are still detected. Is there any change to do ?

@ondrejmirtes
Copy link
Member

@vasilvestre You don't need a DI container to achieve this, you just need to return a configured Application instance from the console_application_loader file. Feel free to open a new issue if you have a specific problem with this.

@phpstan phpstan locked as resolved and limited conversation to collaborators Mar 28, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants