Skip to content

Commit

Permalink
bug #53516 [Console] Allow '0' as a $shortcut in InputOption.php (law…
Browse files Browse the repository at this point in the history
…sonjl-ornl)

This PR was squashed before being merged into the 5.4 branch.

Discussion
----------

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

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | Fix #53514
| License       | MIT

Fixes some comparisons to no longer erroneously disallow `'0'`/`'-0'` as a shortcut value.

Commits
-------

2b6fe56e1a [Console] Allow '0' as a $shortcut in InputOption.php
  • Loading branch information
fabpot committed Jan 21, 2024
2 parents 8c181cf + 923817d commit 0aff198
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
6 changes: 3 additions & 3 deletions 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) {
$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 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

0 comments on commit 0aff198

Please sign in to comment.