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

Skip to content

[Console] PHP deprecation when question value is NULL #61122

@willemverspyck

Description

@willemverspyck

Symfony version(s) affected

6.4

Description

I get this deprecation notice, when I start typing in the console:

deprecation.INFO: Deprecated: str_starts_with(): Passing null to parameter #1 ($haystack) of type string is deprecated {"exception":"[object] (ErrorException(code: 0): Deprecated: str_starts_with(): Passing null to parameter #1 ($haystack) of type string is deprecated at /data/vendor/symfony/console/Helper/QuestionHelper.php:357)"} {"token":null}

The value of the ChoiceQuestion array should not be NULL, but I can happen. When I initate the command with "bin/console app:task", and press for example 2, I will get the deprecation notice (before pressing enter).

How to reproduce

#[AsCommand(name: 'app:task', description: 'Execute task for module')]
final class TaskCommand extends Command
{
    protected function configure(): void
    {
        $this
            ->addOption('moduleId', null, InputOption::VALUE_REQUIRED, 'The "id" of the module?');
    }

    protected function interact(InputInterface $input, OutputInterface $output): void
    {
        $moduleId = $input->getOption('moduleId');

        if (null !== $moduleId) {
            return;
        }

        $data = [
            1 => 'Foo',
            2 => 'Bar',
            3 => null,
        ];

        $question = new ChoiceQuestion('Please select a module:', $data);
        $question->setMaxAttempts(2);
        $question->setValidator(function (string $answer): string {
            if (0 === preg_match('/^\d+$/', $answer)) {
                throw new RuntimeException('Unknown module');
            }

            return $answer;
        });

        $answer = $this->getHelper('question')->ask($input, $output, $question);

        $input->setOption('moduleId', $answer);
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        return Command::SUCCESS;
    }
}

Possible Solution

Add nullsafe

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions