From 44c46ce222df3a59b8cc4a41eba2362ee9db3897 Mon Sep 17 00:00:00 2001 From: peter279k Date: Thu, 13 Apr 2023 10:32:44 +0800 Subject: [PATCH] Revert 2bd797f commit & deprecated exception fix --- src/ContinuousOptionParser.php | 6 ++--- src/OptionParser.php | 17 ++++++------- tests/ContinuousOptionParserTest.php | 22 +++++++--------- tests/OptionCollectionTest.php | 16 +++++------- tests/OptionParserTest.php | 38 ++++++++++++---------------- tests/OptionTest.php | 17 +++++-------- 6 files changed, 49 insertions(+), 67 deletions(-) diff --git a/src/ContinuousOptionParser.php b/src/ContinuousOptionParser.php index fa592fd..bdc9f22 100644 --- a/src/ContinuousOptionParser.php +++ b/src/ContinuousOptionParser.php @@ -64,7 +64,7 @@ * * // command arguments * $arguments = array(); - * + * * $argv = explode(' ','-v -d -c subcommand1 -a -b -c subcommand2 -c subcommand3 arg1 arg2 arg3'); * * // parse application options first @@ -139,7 +139,7 @@ public function continueParse() protected function fillDefaultValues(OptionCollection $opts, OptionResult $result) { - // register option result from options with default value + // register option result from options with default value foreach ($opts as $opt) { if ($opt->value === null && $opt->defaultValue !== null) { $opt->setValue($opt->getDefaultValue()); @@ -153,7 +153,7 @@ public function parse(array $argv) { // create new Result object. $result = new OptionResult(); - list($this->argv, $extra) = $this->preprocessingArguments($argv); + $this->argv = $this->preprocessingArguments($argv); $this->length = count($this->argv); // from last parse index diff --git a/src/OptionParser.php b/src/OptionParser.php index da9cba5..a3c11a4 100644 --- a/src/OptionParser.php +++ b/src/OptionParser.php @@ -38,7 +38,7 @@ public function setSpecs(OptionCollection $specs) */ protected function consumeOptionToken(Option $spec, $arg, $next, & $success = false) { - // Check options doesn't require next token before + // Check options doesn't require next token before // all options that require values. if ($spec->isFlag()) { @@ -70,11 +70,11 @@ protected function consumeOptionToken(Option $spec, $arg, $next, & $success = fa $spec->setValue($next->arg); return 1; - } + } return 0; } - /* + /* * push value to multipl value option */ protected function pushOptionValue(Option $spec, $arg, $next) @@ -94,7 +94,6 @@ protected function preprocessingArguments(array $argv) { // preprocessing arguments $newArgv = array(); - $extra = array(); $afterDash = false; foreach ($argv as $arg) { if ($arg === '--') { @@ -102,7 +101,7 @@ protected function preprocessingArguments(array $argv) continue; } if ($afterDash) { - $extra[] = $arg; + $newArgv[] = $arg; continue; } @@ -111,15 +110,15 @@ protected function preprocessingArguments(array $argv) list($opt, $val) = $a->splitAsOption(); array_push($newArgv, $opt, $val); } else { - $newArgv[] = $arg; + array_push($newArgv, $arg); } } - return array($newArgv, $extra); + return $newArgv; } protected function fillDefaultValues(OptionCollection $opts, OptionResult $result) { - // register option result from options with default value + // register option result from options with default value foreach ($opts as $opt) { if ($opt->value === null && $opt->defaultValue !== null) { $opt->setValue($opt->getDefaultValue()); @@ -141,7 +140,7 @@ public function parse(array $argv) { $result = new OptionResult(); - list($argv, $extra) = $this->preprocessingArguments($argv); + $argv = $this->preprocessingArguments($argv); $len = count($argv); diff --git a/tests/ContinuousOptionParserTest.php b/tests/ContinuousOptionParserTest.php index b1c2ad1..2dbd93e 100644 --- a/tests/ContinuousOptionParserTest.php +++ b/tests/ContinuousOptionParserTest.php @@ -46,7 +46,7 @@ public function argumentProvider() ['program','-v', '-c', 'subcommand1', '--as', 99, 'arg1', 'arg2', 'arg3', '--','zz','xx','vv'], [ 'app' => ['verbose' => true], - 'args' => ['arg1', 'arg2', 'arg3'] + 'args' => ['arg1', 'arg2', 'arg3', 'zz', 'xx', 'vv'] ], ], ]; @@ -203,7 +203,7 @@ function testParser4() $this->assertNotNull( $r ); - + $this->assertNotNull( $r->a , 'option a' ); $this->assertNotNull( $r->b , 'option b' ); $this->assertNotNull( $r->c , 'option c' ); @@ -250,7 +250,7 @@ function testParser5() $arguments[] = $parser->advance(); } } - + $this->assertEquals( 'arg1', $arguments[0] ); $this->assertEquals( 'arg2', $arguments[1] ); $this->assertEquals( 'arg3', $arguments[2] ); @@ -261,11 +261,10 @@ function testParser5() $this->assertNotNull( 3, $subcommand_options['subcommand3']->a ); } - /** - * @expectedException GetOptionKit\Exception\InvalidOptionException - */ public function testParseInvalidOptionException() { + $this->expectException(\GetOptionKit\Exception\InvalidOptionException::class); + $parser = new ContinuousOptionParser(new OptionCollection); $parser->parse(array('app','--foo')); $arguments = array(); @@ -307,23 +306,20 @@ public function testIncrementalValue() $this->assertEquals(3, $result->keys["verbose"]->value); } - - /** - * @expectedException GetOptionKit\Exception\InvalidOptionException - */ public function testUnknownOption() { + $this->expectException(\GetOptionKit\Exception\InvalidOptionException::class); + $options = new OptionCollection; $options->add("v|verbose"); $parser = new ContinuousOptionParser($options); $result = $parser->parse(array('app', '-b')); } - /** - * @expectedException LogicException - */ public function testAdvancedOutOfBounds() { + $this->expectException(\LogicException::class); + $options = new OptionCollection; $options->add("v|verbose"); $parser = new ContinuousOptionParser($options); diff --git a/tests/OptionCollectionTest.php b/tests/OptionCollectionTest.php index 542b293..189f6c4 100644 --- a/tests/OptionCollectionTest.php +++ b/tests/OptionCollectionTest.php @@ -12,32 +12,28 @@ public function testAddOption() $this->assertSame($o, $opts->getShortOption('v')); } - - /** - * @expectedException LogicException - */ public function testAddInvalidOption() { + $this->expectException(\LogicException::class); + $opts = new OptionCollection; $opts->add(123); } - /** - * @expectedException GetOptionKit\Exception\OptionConflictException - */ public function testOptionConflictShort() { + $this->expectException(\GetOptionKit\Exception\OptionConflictException::class); + $opts = new OptionCollection; $opts->add('r|repeat'); $opts->add('t|time'); $opts->add('r|regex'); } - /** - * @expectedException GetOptionKit\Exception\OptionConflictException - */ public function testOptionConflictLong() { + $this->expectException(\GetOptionKit\Exception\OptionConflictException::class); + $opts = new OptionCollection; $opts->add('r|repeat'); $opts->add('t|time'); diff --git a/tests/OptionParserTest.php b/tests/OptionParserTest.php index 96196d7..c81ef56 100644 --- a/tests/OptionParserTest.php +++ b/tests/OptionParserTest.php @@ -24,11 +24,10 @@ protected function setUp(): void $this->parser = new OptionParser($this->specs); } - /** - * @expectedException Exception - */ public function testInvalidOption() { + $this->expectException(\Exception::class); + $options = new OptionCollection; $options->addOption(new Option(0)); } @@ -229,17 +228,16 @@ public function testParseIncrementalOption() $parser = new OptionParser($opts); $result = $parser->parse(explode(' ','app -vvv arg1 arg2')); - $this->assertInstanceOf('GetOptionKit\Option',$result['verbose']); + $this->assertInstanceOf('GetOptionKit\Option',$result['verbose']); $this->assertNotNull($result['verbose']); $this->assertEquals(3, $result['verbose']->value); } - /** - * @expectedException Exception - */ public function testIntegerTypeNonNumeric() { + $this->expectException(\Exception::class); + $opt = new OptionCollection; $opt->add( 'b|bar:=number' , 'option with integer type' ); @@ -350,13 +348,13 @@ public function optionTestProvider() [['a','--foo','a', 'b', 'c']] ), array( 'f|foo', 'simple boolean option', 'foo', true, - [['a','--foo'], ['a','-f']] + [['a','--foo'], ['a','-f']] ), array( 'f|foo:=string', 'string option', 'foo', 'xxx', - [['a','--foo','xxx'], ['a','-f', 'xxx']] + [['a','--foo','xxx'], ['a','-f', 'xxx']] ), array( 'f|foo:=string', 'string option', 'foo', 'xxx', - [['a','b', 'c', '--foo','xxx'], ['a', 'a', 'b', 'c', '-f', 'xxx']] + [['a','b', 'c', '--foo','xxx'], ['a', 'a', 'b', 'c', '-f', 'xxx']] ), ); } @@ -375,29 +373,26 @@ public function test($specString, $desc, $key, $expectedValue, array $argvList) } } - /** - * @expectedException Exception - */ public function testParseWithoutProgramName() { + $this->expectException(\Exception::class); + $parser = new OptionParser(new OptionCollection); $parser->parse(array('--foo')); } - /** - * @expectedException GetOptionKit\Exception\InvalidOptionException - */ public function testParseInvalidOptionException() { + $this->expectException(\GetOptionKit\Exception\InvalidOptionException::class); + $parser = new OptionParser(new OptionCollection); $parser->parse(array('app','--foo')); } - /** - * @expectedException GetOptionKit\Exception\RequireValueException - */ public function testParseOptionRequireValueException() { + $this->expectException(\GetOptionKit\Exception\RequireValueException::class); + $options = new OptionCollection; $options->add('name:=string', 'name'); @@ -461,11 +456,10 @@ public function testParseAcceptsValidOption() $this->assertArrayHasKey('f', $result); } - /** - * @expectedException GetOptionKit\Exception\InvalidOptionValueException - */ public function testParseThrowsExceptionOnInvalidOption() { + $this->expectException(\GetOptionKit\Exception\InvalidOptionValueException::class); + $this->specs ->add('f:foo', 'test option') ->validator(function($value) { diff --git a/tests/OptionTest.php b/tests/OptionTest.php index 7b06f1d..eb45b68 100644 --- a/tests/OptionTest.php +++ b/tests/OptionTest.php @@ -27,11 +27,10 @@ public function testOptionSpec($spec) $this->assertNotNull($opt); } - /** - * @expectedException Exception - */ public function testInvalidOptionSpec() { + $this->expectException(\Exception::class); + new Option('....'); } @@ -87,21 +86,19 @@ public function testValidator($cb) $this->assertEquals('--scope', $opt->renderReadableSpec(true)); } - /** - * @expectedException Exception - */ public function testInvalidTypeClass() { + $this->expectException(\Exception::class); + $opt = new Option('scope'); $opt->isa('SomethingElse'); $class = $opt->getTypeClass(); } - /** - * @expectedException InvalidArgumentException - */ public function testValidatorReturnValue() { + $this->expectException(\InvalidArgumentException::class); + $opt = new Option('scope'); $opt->validator(function($val) { return 123454; @@ -216,7 +213,7 @@ public function testValidValues() public function testFilter() { $opt = new Option('scope'); - $opt->filter(function($val) { + $opt->filter(function($val) { return preg_replace('#a#', 'x', $val); }) ;