Skip to content

Commit 4de97e8

Browse files
committed
Merge branch '4.0'
* 4.0: fix merge Env var maps to undefined constant. [SecurityBundle] Backport test [Security] fix merge of 2.7 into 2.8 + add test case backport regression test from 3.4 do not mock the container builder or definitions fixed CS [TwigBundle] Register TwigBridge extensions first [WebProfilerBundle] Fix sub request link PhpDocExtractor::getTypes() throws fatal error when type omitted Fix misspelling variable use libsodium to run Argon2i related tests [DI] minor: use a strict comparision in setDecoratedService [HttpKernel] fix FC Follow-on to #25825: Fix edge case in getParameterOption. keep the context when validating forms
2 parents 233eaa6 + 0e873eb commit 4de97e8

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
lines changed

Input/ArgvInput.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,11 @@ public function hasParameterOption($values, $onlyParams = false)
277277
return false;
278278
}
279279
foreach ($values as $value) {
280-
if ($token === $value || 0 === strpos($token, $value.'=')) {
280+
// Options with values:
281+
// For long options, test for '--option=' at beginning
282+
// For short options, test for '-o' at beginning
283+
$leading = 0 === strpos($value, '--') ? $value.'=' : $value;
284+
if ($token === $value || 0 === strpos($token, $leading)) {
281285
return true;
282286
}
283287
}
@@ -301,13 +305,16 @@ public function getParameterOption($values, $default = false, $onlyParams = fals
301305
}
302306

303307
foreach ($values as $value) {
304-
if ($token === $value || 0 === strpos($token, $value.'=')) {
305-
if (false !== $pos = strpos($token, '=')) {
306-
return substr($token, $pos + 1);
307-
}
308-
308+
if ($token === $value) {
309309
return array_shift($tokens);
310310
}
311+
// Options with values:
312+
// For long options, test for '--option=' at beginning
313+
// For short options, test for '-o' at beginning
314+
$leading = 0 === strpos($value, '--') ? $value.'=' : $value;
315+
if (0 === strpos($token, $leading)) {
316+
return substr($token, strlen($leading));
317+
}
311318
}
312319
}
313320

Input/InputInterface.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public function getFirstArgument();
3333
*
3434
* This method is to be used to introspect the input parameters
3535
* before they have been validated. It must be used carefully.
36+
* Does not necessarily return the correct result for short options
37+
* when multiple flags are combined in the same option.
3638
*
3739
* @param string|array $values The values to look for in the raw parameters (can be an array)
3840
* @param bool $onlyParams Only check real parameters, skip those following an end of options (--) signal
@@ -46,6 +48,8 @@ public function hasParameterOption($values, $onlyParams = false);
4648
*
4749
* This method is to be used to introspect the input parameters
4850
* before they have been validated. It must be used carefully.
51+
* Does not necessarily return the correct result for short options
52+
* when multiple flags are combined in the same option.
4953
*
5054
* @param string|array $values The value(s) to look for in the raw parameters (can be an array)
5155
* @param mixed $default The default value to return if no result is found

Tests/Input/ArgvInputTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,10 @@ public function testHasParameterOption()
314314
$input = new ArgvInput(array('cli.php', '-f', 'foo'));
315315
$this->assertTrue($input->hasParameterOption('-f'), '->hasParameterOption() returns true if the given short option is in the raw input');
316316

317+
$input = new ArgvInput(array('cli.php', '-etest'));
318+
$this->assertTrue($input->hasParameterOption('-e'), '->hasParameterOption() returns true if the given short option is in the raw input');
319+
$this->assertFalse($input->hasParameterOption('-s'), '->hasParameterOption() returns true if the given short option is in the raw input');
320+
317321
$input = new ArgvInput(array('cli.php', '--foo', 'foo'));
318322
$this->assertTrue($input->hasParameterOption('--foo'), '->hasParameterOption() returns true if the given short option is in the raw input');
319323

@@ -339,6 +343,33 @@ public function testHasParameterOptionOnlyOptions()
339343
$this->assertFalse($input->hasParameterOption('--foo', true), '->hasParameterOption() returns false if the given option is in the raw input but after an end of options signal');
340344
}
341345

346+
public function testHasParameterOptionEdgeCasesAndLimitations()
347+
{
348+
$input = new ArgvInput(array('cli.php', '-fh'));
349+
// hasParameterOption does not know if the previous short option, -f,
350+
// takes a value or not. If -f takes a value, then -fh does NOT include
351+
// -h; Otherwise it does. Since we do not know which short options take
352+
// values, hasParameterOption does not support this use-case.
353+
$this->assertFalse($input->hasParameterOption('-h'), '->hasParameterOption() returns true if the given short option is in the raw input');
354+
// hasParameterOption does detect that `-fh` contains `-f`, since
355+
// `-f` is the first short option in the set.
356+
$this->assertTrue($input->hasParameterOption('-f'), '->hasParameterOption() returns true if the given short option is in the raw input');
357+
// The test below happens to pass, although it might make more sense
358+
// to disallow it, and require the use of
359+
// $input->hasParameterOption('-f') && $input->hasParameterOption('-h')
360+
// instead.
361+
$this->assertTrue($input->hasParameterOption('-fh'), '->hasParameterOption() returns true if the given short option is in the raw input');
362+
// In theory, if -fh is supported, then -hf should also work.
363+
// However, this is not supported.
364+
$this->assertFalse($input->hasParameterOption('-hf'), '->hasParameterOption() returns true if the given short option is in the raw input');
365+
366+
$input = new ArgvInput(array('cli.php', '-f', '-h'));
367+
// If hasParameterOption('-fh') is supported for 'cli.php -fh', then
368+
// one might also expect that it should also be supported for
369+
// 'cli.php -f -h'. However, this is not supported.
370+
$this->assertFalse($input->hasParameterOption('-fh'), '->hasParameterOption() returns true if the given short option is in the raw input');
371+
}
372+
342373
public function testToString()
343374
{
344375
$input = new ArgvInput(array('cli.php', '-f', 'foo'));

0 commit comments

Comments
 (0)