From 4e9b1b2351fe245e1477635741fdde5ad73994d2 Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Fri, 15 Dec 2017 12:34:02 +0100 Subject: [PATCH] Backport fix for #2472 (this does not mean that PHPUnit 5.7 is supported on PHP 7.2) --- ChangeLog-5.7.md | 1 + src/Util/Getopt.php | 39 +++++++++++++++++++++++---------------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/ChangeLog-5.7.md b/ChangeLog-5.7.md index 6d6e6536d96..f5c5229734a 100644 --- a/ChangeLog-5.7.md +++ b/ChangeLog-5.7.md @@ -6,6 +6,7 @@ All notable changes of the PHPUnit 5.7 release series are documented in this fil ### Fixed +* Fixed [#2472](https://github.com/sebastianbergmann/phpunit/issues/2472): `PHPUnit\Util\Getopt` uses deprecated `each()` function * Fixed [#2833](https://github.com/sebastianbergmann/phpunit/issues/2833): Test class loaded during data provider execution is not discovered ## [5.7.25] - 2017-11-14 diff --git a/src/Util/Getopt.php b/src/Util/Getopt.php index dc3af7c4772..894778d4849 100644 --- a/src/Util/Getopt.php +++ b/src/Util/Getopt.php @@ -32,18 +32,24 @@ public static function getopt(array $args, $short_options, $long_options = null) reset($args); - while (list($i, $arg) = @each($args)) { + $args = array_map('trim', $args); + + while (false !== $arg = current($args)) { + $i = key($args); + next($args); if ($arg == '') { continue; } if ($arg == '--') { $non_opts = array_merge($non_opts, array_slice($args, $i + 1)); + break; } if ($arg[0] != '-' || (strlen($arg) > 1 && $arg[1] == '-' && !$long_options)) { $non_opts[] = $args[$i]; + continue; } elseif (strlen($arg) > 1 && $arg[1] == '-') { self::parseLongOption( @@ -80,21 +86,18 @@ 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[] = [$opt, substr($arg, $i + 1)]; - break; - } - } else { - if ($i + 1 < $argLen) { - $opts[] = [$opt, substr($arg, $i + 1)]; - break; - } elseif (list(, $opt_arg) = @each($args)) { - } else { + if ($i + 1 < $argLen) { + $opts[] = [$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); } } @@ -134,10 +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) { @@ -155,3 +161,4 @@ protected static function parseLongOption($arg, $long_options, &$opts, &$args) throw new PHPUnit_Framework_Exception("unrecognized option --$opt"); } } +