Skip to content
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

[4.8] Avoid using function each() #2773

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 17 additions & 17 deletions src/Util/Getopt.php
Expand Up @@ -35,7 +35,9 @@ public static function getopt(array $args, $short_options, $long_options = null)
reset($args);
array_map('trim', $args);

while (list($i, $arg) = each($args)) {
while (false !== $arg = current($args)) {
$i = key($args);
next($args);
if ($arg == '') {
continue;
}
Expand Down Expand Up @@ -85,21 +87,17 @@ protected static function parseShortOption($arg, $short_options, &$opts, &$args)
}

if (strlen($spec) > 1 && $spec[1] == ':') {
if (strlen($spec) > 2 && $spec[2] == ':') {
if ($i + 1 < $argLen) {
$opts[] = array($opt, substr($arg, $i + 1));
break;
}
} else {
if ($i + 1 < $argLen) {
$opts[] = array($opt, substr($arg, $i + 1));
break;
} elseif (list(, $opt_arg) = each($args)) {
} else {
if ($i + 1 < $argLen) {
$opts[] = array($opt, substr($arg, $i + 1));
break;
}
if (!(strlen($spec) > 2 && $spec[2] == ':')) {
if (false === $opt_arg = current($args)) {
throw new PHPUnit_Framework_Exception(
"option requires an argument -- $opt"
);
}
next($args);
}
}

Expand Down Expand Up @@ -139,11 +137,13 @@ protected static function parseLongOption($arg, $long_options, &$opts, &$args)

if (substr($long_opt, -1) == '=') {
if (substr($long_opt, -2) != '==') {
if (!strlen($opt_arg) &&
!(list(, $opt_arg) = each($args))) {
throw new PHPUnit_Framework_Exception(
"option --$opt requires an argument"
);
if (!strlen($opt_arg)) {
if (false === $opt_arg = current($args)) {
throw new PHPUnit_Framework_Exception(
"option --$opt requires an argument"
);
}
next($args);
}
}
} elseif ($opt_arg) {
Expand Down
180 changes: 180 additions & 0 deletions tests/Util/GetoptTest.php
Expand Up @@ -59,4 +59,184 @@ public function testItIncludeTheShortOptionsAfterTheArgument()

$this->assertEquals($expected, $actual);
}

public function testShortOptionUnrecognizedException()
{
$args = array(
'command',
'myArgument',
'-v',
);

try {
PHPUnit_Util_Getopt::getopt($args, '');
$exception = null;
} catch (Exception $x) {
$exception = $x;
}
$this->assertNotNull($exception);
$this->assertInstanceOf('PHPUnit_Framework_Exception', $exception);
$this->assertSame('unrecognized option -- v', $exception->getMessage());
}

public function testShortOptionRequiresAnArgumentException()
{
$args = array(
'command',
'myArgument',
'-f',
);

try {
PHPUnit_Util_Getopt::getopt($args, 'f:');
$exception = null;
} catch (Exception $x) {
$exception = $x;
}
$this->assertNotNull($exception);
$this->assertInstanceOf('PHPUnit_Framework_Exception', $exception);
$this->assertSame('option requires an argument -- f', $exception->getMessage());
}

public function testShortOptionHandleAnOptionalValue()
{
$args = array(
'command',
'myArgument',
'-f',
);
$actual = PHPUnit_Util_Getopt::getopt($args, 'f::');
$expected = array(
array(
array(
'f',
null,
),
),
array(
'myArgument',
),
);
$this->assertEquals($expected, $actual);
}

public function testLongOptionIsAmbiguousException()
{
$args = array(
'command',
'--col',
);

try {
PHPUnit_Util_Getopt::getopt($args, '', array('columns', 'colors'));
$exception = null;
} catch (Exception $x) {
$exception = $x;
}
$this->assertNotNull($exception);
$this->assertInstanceOf('PHPUnit_Framework_Exception', $exception);
$this->assertSame('option --col is ambiguous', $exception->getMessage());
}

public function testLongOptionUnrecognizedException()
{
// the exception 'unrecognized option --option' is not thrown
// if the there are not defined extended options
$args = array(
'command',
'--foo',
);

try {
PHPUnit_Util_Getopt::getopt($args, '', array('colors'));
$exception = null;
} catch (Exception $x) {
$exception = $x;
}
$this->assertNotNull($exception);
$this->assertInstanceOf('PHPUnit_Framework_Exception', $exception);
$this->assertSame('unrecognized option --foo', $exception->getMessage());
}

public function testLongOptionRequiresAnArgumentException()
{
$args = array(
'command',
'--foo',
);

try {
PHPUnit_Util_Getopt::getopt($args, '', array('foo='));
$exception = null;
} catch (Exception $x) {
$exception = $x;
}
$this->assertNotNull($exception);
$this->assertInstanceOf('PHPUnit_Framework_Exception', $exception);
$this->assertSame('option --foo requires an argument', $exception->getMessage());
}

public function testLongOptionDoesNotAllowAnArgumentException()
{
$args = array(
'command',
'--foo=bar',
);

try {
PHPUnit_Util_Getopt::getopt($args, '', array('foo'));
$exception = null;
} catch (Exception $x) {
$exception = $x;
}
$this->assertNotNull($exception);
$this->assertInstanceOf('PHPUnit_Framework_Exception', $exception);
$this->assertSame("option --foo doesn't allow an argument", $exception->getMessage());
}

public function testItHandlesLongParametesWithValues()
{
$command = 'command parameter-0 --exec parameter-1 --conf config.xml --optn parameter-2 --optn=content-of-o parameter-n';

Choose a reason for hiding this comment

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

Line exceeds 120 characters; contains 129 characters

Copy link
Author

Choose a reason for hiding this comment

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

Fixed, thanks bot 😉

$args = explode(' ', $command);
unset($args[0]);
$expected = array(
array(
array('--exec', null),
array('--conf', 'config.xml'),
array('--optn', null),
array('--optn', 'content-of-o'),
),
array(
'parameter-0',
'parameter-1',
'parameter-2',
'parameter-n',
),
);
$actual = PHPUnit_Util_Getopt::getopt($args, '', array('exec', 'conf=', 'optn=='));
$this->assertEquals($expected, $actual);
}

public function testItHandlesShortParametesWithValues()
{
$command = 'command parameter-0 -x parameter-1 -c config.xml -o parameter-2 -ocontent-of-o parameter-n';
$args = explode(' ', $command);
unset($args[0]);
$expected = array(
array(
array('x', null),
array('c', 'config.xml'),
array('o', null),
array('o', 'content-of-o'),
),
array(
'parameter-0',
'parameter-1',
'parameter-2',
'parameter-n',
),
);
$actual = PHPUnit_Util_Getopt::getopt($args, 'xc:o::');
$this->assertEquals($expected, $actual);
}
}