Skip to content

[Console] Fix PHP 8.1 deprecation in ChoiceQuestion #45181

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

Merged
merged 1 commit into from
Jan 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Symfony/Component/Console/Question/ChoiceQuestion.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,18 +131,18 @@ private function getDefaultValidator(): callable
return function ($selected) use ($choices, $errorMessage, $multiselect, $isAssoc) {
if ($multiselect) {
// Check for a separated comma values
if (!preg_match('/^[^,]+(?:,[^,]+)*$/', $selected, $matches)) {
if (!preg_match('/^[^,]+(?:,[^,]+)*$/', (string) $selected, $matches)) {
throw new InvalidArgumentException(sprintf($errorMessage, $selected));
}

$selectedChoices = explode(',', $selected);
$selectedChoices = explode(',', (string) $selected);
} else {
$selectedChoices = [$selected];
}

if ($this->isTrimmable()) {
foreach ($selectedChoices as $k => $v) {
$selectedChoices[$k] = trim($v);
$selectedChoices[$k] = trim((string) $v);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ class ChoiceQuestionTest extends TestCase
/**
* @dataProvider selectUseCases
*/
public function testSelectUseCases($multiSelect, $answers, $expected, $message)
public function testSelectUseCases($multiSelect, $answers, $expected, $message, $default = null)
{
$question = new ChoiceQuestion('A question', [
'First response',
'Second response',
'Third response',
'Fourth response',
]);
null,
], $default);

$question->setMultiselect($multiSelect);

Expand Down Expand Up @@ -59,6 +60,19 @@ public function selectUseCases()
['First response', 'Second response'],
'When passed multiple answers on MultiSelect, the defaultValidator must return these answers as an array',
],
[
false,
[null],
null,
'When used null as default single answer on singleSelect, the defaultValidator must return this answer as null',
],
[
false,
['First response'],
'First response',
'When used a string as default single answer on singleSelect, the defaultValidator must return this answer as a string',
'First response',
],
];
}

Expand Down