Skip to content

[Console] Allow '0' as a $shortcut in InputOption.php #53516

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 21, 2024
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/Input/InputOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function __construct(string $name, $shortcut = null, int $mode = null, st
throw new InvalidArgumentException('An option name cannot be empty.');
}

if (empty($shortcut)) {
if ('' === $shortcut || [] === $shortcut) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$shortcut === false is not handled

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please send a PR on branch 5.4 to fix this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documented acceptable types for $shortcut are string, array, and null, and the previously-existing test cases only covered those types, so in that sense passing false here is an incorrect use of the API in the first place.

But passing false happened to accidentally work before the change, so I guess it is a breaking change (to undocumented behavior).

A fix that restores the handling of false should probably also update the types in the docstring and add a test case, so that the next person who has reason to modify this code knows that behavior should be preserved.

$shortcut = null;
}

Expand All @@ -78,10 +78,10 @@ public function __construct(string $name, $shortcut = null, int $mode = null, st
$shortcut = implode('|', $shortcut);
}
$shortcuts = preg_split('{(\|)-?}', ltrim($shortcut, '-'));
$shortcuts = array_filter($shortcuts);
$shortcuts = array_filter($shortcuts, 'strlen');
$shortcut = implode('|', $shortcuts);

if (empty($shortcut)) {
if ('' === $shortcut) {
throw new InvalidArgumentException('An option shortcut cannot be empty.');
}
}
Expand Down
14 changes: 14 additions & 0 deletions src/Symfony/Component/Console/Tests/Input/InputOptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,20 @@ public function testShortcut()
$this->assertEquals('f|ff|fff', $option->getShortcut(), '__construct() removes the leading - of the shortcuts');
$option = new InputOption('foo');
$this->assertNull($option->getShortcut(), '__construct() makes the shortcut null by default');
$option = new InputOption('foo', '');
$this->assertNull($option->getShortcut(), '__construct() makes the shortcut null when given an empty string');
$option = new InputOption('foo', []);
$this->assertNull($option->getShortcut(), '__construct() makes the shortcut null when given an empty array');
$option = new InputOption('foo', ['f', '', 'fff']);
$this->assertEquals('f|fff', $option->getShortcut(), '__construct() removes empty shortcuts');
$option = new InputOption('foo', 'f||fff');
$this->assertEquals('f|fff', $option->getShortcut(), '__construct() removes empty shortcuts');
$option = new InputOption('foo', '0');
$this->assertEquals('0', $option->getShortcut(), '-0 is an acceptable shortcut value');
$option = new InputOption('foo', ['0', 'z']);
$this->assertEquals('0|z', $option->getShortcut(), '-0 is an acceptable shortcut value when embedded in an array');
$option = new InputOption('foo', '0|z');
$this->assertEquals('0|z', $option->getShortcut(), '-0 is an acceptable shortcut value when embedded in a string-list');
}

public function testModes()
Expand Down