Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Console] SymfonyStyle throws notice for invalid default choice in choice() method. #34093

@leevigraham

Description

@leevigraham

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'));
    }
}

image

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:

image

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