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

Skip to content

Commit 06b5e7f

Browse files
committed
Fix empty strings handling
1 parent 5a6dddc commit 06b5e7f

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,12 @@ private function parseLongOption($token)
146146

147147
if (false !== $pos = strpos($name, '=')) {
148148
if (0 === strlen($value = substr($name, $pos + 1))) {
149-
array_unshift($this->parsed, null);
149+
// if no value after "=" then substr() returns "" since php7 only, false before
150+
// see http://php.net/manual/fr/migration70.incompatible.php#119151
151+
if (PHP_VERSION_ID < 70000 && false === $value) {
152+
$value = '';
153+
}
154+
array_unshift($this->parsed, $value);
150155
}
151156
$this->addLongOption(substr($name, 0, $pos), $value);
152157
} else {
@@ -219,23 +224,16 @@ private function addLongOption($name, $value)
219224

220225
$option = $this->definition->getOption($name);
221226

222-
// Convert empty values to null
223-
if (!isset($value[0])) {
224-
$value = null;
225-
}
226-
227227
if (null !== $value && !$option->acceptValue()) {
228228
throw new \RuntimeException(sprintf('The "--%s" option does not accept a value.', $name));
229229
}
230230

231-
if (null === $value && $option->acceptValue() && count($this->parsed)) {
231+
if (in_array($value, array('', null), true) && $option->acceptValue() && count($this->parsed)) {
232232
// if option accepts an optional or mandatory argument
233233
// let's see if there is one provided
234234
$next = array_shift($this->parsed);
235-
if (isset($next[0]) && '-' !== $next[0]) {
235+
if ((isset($next[0]) && '-' !== $next[0]) || in_array($next, array('', null), true)) {
236236
$value = $next;
237-
} elseif (empty($next)) {
238-
$value = null;
239237
} else {
240238
array_unshift($this->parsed, $next);
241239
}

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function testParseOptions($input, $options, $expectedOptions, $message)
4747
$input = new ArgvInput($input);
4848
$input->bind(new InputDefinition($options));
4949

50-
$this->assertEquals($expectedOptions, $input->getOptions(), $message);
50+
$this->assertSame($expectedOptions, $input->getOptions(), $message);
5151
}
5252

5353
public function provideOptions()
@@ -74,20 +74,32 @@ public function provideOptions()
7474
array(
7575
array('cli.php', '--foo='),
7676
array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL)),
77-
array('foo' => null),
78-
'->parse() parses long options with optional value which is empty (with a = separator) as null',
77+
array('foo' => ''),
78+
'->parse() parses long options with optional value without value specified (with a = separator) as empty string',
7979
),
8080
array(
8181
array('cli.php', '--foo=', 'bar'),
8282
array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputArgument('name', InputArgument::REQUIRED)),
83+
array('foo' => ''),
84+
'->parse() parses long options with optional value without value specified or an empty string (with a = separator) followed by an argument as empty string',
85+
),
86+
array(
87+
array('cli.php', 'bar', '--foo'),
88+
array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputArgument('name', InputArgument::REQUIRED)),
8389
array('foo' => null),
84-
'->parse() parses long options with optional value which is empty (with a = separator) followed by an argument',
90+
'->parse() parses long options with optional value which is empty (with a = separator) preceded by an argument',
91+
),
92+
array(
93+
array('cli.php', '--foo', '', 'bar'),
94+
array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputArgument('name', InputArgument::REQUIRED)),
95+
array('foo' => ''),
96+
'->parse() parses long options with optional value which is empty as empty string even followed by an argument',
8597
),
8698
array(
8799
array('cli.php', '--foo'),
88100
array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL)),
89101
array('foo' => null),
90-
'->parse() parses long options with optional value which is empty as null',
102+
'->parse() parses long options with optional value specified with no separator and no value as null',
91103
),
92104
array(
93105
array('cli.php', '-f'),
@@ -252,7 +264,7 @@ public function testParseArrayOption()
252264

253265
$input = new ArgvInput(array('cli.php', '--name=foo', '--name=bar', '--name='));
254266
$input->bind(new InputDefinition(array(new InputOption('name', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY))));
255-
$this->assertSame(array('name' => array('foo', 'bar', null)), $input->getOptions(), '->parse() parses empty array options as null ("--option=value" syntax)');
267+
$this->assertSame(array('name' => array('foo', 'bar', '')), $input->getOptions(), '->parse() parses empty array options as null ("--option=value" syntax)');
256268

257269
$input = new ArgvInput(array('cli.php', '--name', 'foo', '--name', 'bar', '--name', '--anotherOption'));
258270
$input->bind(new InputDefinition(array(

0 commit comments

Comments
 (0)