Skip to content

Commit c6029aa

Browse files
committed
bug symfony#13630 [Console] fixed ArrayInput, if array contains 0 key. (arima-ryunosuke)
This PR was submitted for the 2.6 branch but it was merged into the 2.3 branch instead (closes symfony#13630). Discussion ---------- [Console] fixed ArrayInput, if array contains 0 key. | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | ```php $input = new ArrayInput(array('Fabien', '--foo' => 'bar')); var_dump($input->getParameterOption('--foo')); // this is 'Fabien'. ``` Because `in_array` third argument's default is `false`. `in_array(0, $values)` returns `true` in many cases. Commits ------- a642e4b [Console] fixed ArrayInput, if array contains 0 key.
2 parents 917067e + a642e4b commit c6029aa

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/Symfony/Component/Console/Input/ArrayInput.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,10 @@ public function getParameterOption($values, $default = false)
100100
$values = (array) $values;
101101

102102
foreach ($this->parameters as $k => $v) {
103-
if (is_int($k) && in_array($v, $values)) {
104-
return true;
103+
if (is_int($k)) {
104+
if (in_array($v, $values)) {
105+
return true;
106+
}
105107
} elseif (in_array($k, $values)) {
106108
return $v;
107109
}

src/Symfony/Component/Console/Tests/Input/ArrayInputTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ public function testHasParameterOption()
3838
$this->assertTrue($input->hasParameterOption('--foo'), '->hasParameterOption() returns true if an option is present in the passed parameters');
3939
}
4040

41+
public function testGetParameterOption()
42+
{
43+
$input = new ArrayInput(array('name' => 'Fabien', '--foo' => 'bar'));
44+
$this->assertEquals('bar', $input->getParameterOption('--foo'), '->getParameterOption() returns the option of specified name');
45+
46+
$input = new ArrayInput(array('Fabien', '--foo' => 'bar'));
47+
$this->assertEquals('bar', $input->getParameterOption('--foo'), '->getParameterOption() returns the option of specified name');
48+
}
49+
4150
public function testParseArguments()
4251
{
4352
$input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name'))));

0 commit comments

Comments
 (0)