-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Description
Symfony version(s) affected: 4.3.5, 4.4
Description
If an invalid default value is passed to SymfonyStyle choice()
method a notice is output. I believe this is becuase there is no isset
check.
How to reproduce
Create a console command with an option which has an invalid default value. In my case I was accepting user input from an option.
<?php
namespace Newism\DevOps\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
class TestCommand extends Command
{
protected function configure()
{
$this
->setName('test-command')
->addOption(
'option',
null,
InputOption::VALUE_REQUIRED,
'',
'c'
);
}
protected function interact(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input, $output);
$provider = $io->choice('Question…', ['a', 'b'], $input->getOption('option'));
}
}
Possible Solution
Check if the value is isset
or use a null coalesce operator here: https://github.com/symfony/symfony/blob/4.4/src/Symfony/Component/Console/Style/SymfonyStyle.php#L298
This follows the implementation here: https://github.com/symfony/symfony/blob/4.4/src/Symfony/Component/Console/Helper/QuestionHelper.php#L63
Additional Information
Implementing the isset
will correctly return an invalid value error when using non-interaction mode: