From c116711054a8e9ce0330c7a66b69dd6af2027eba Mon Sep 17 00:00:00 2001 From: Heinz Wiesinger Date: Tue, 10 Dec 2024 13:28:21 +0100 Subject: [PATCH] LunrCliParser: Type-safe access to $_SERVER['argv'] --- src/Lunr/Shadow/LunrCliParser.php | 22 +++++++- .../Shadow/Tests/LunrCliParserParseTest.php | 53 +++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/src/Lunr/Shadow/LunrCliParser.php b/src/Lunr/Shadow/LunrCliParser.php index 5939963..06df0b9 100644 --- a/src/Lunr/Shadow/LunrCliParser.php +++ b/src/Lunr/Shadow/LunrCliParser.php @@ -12,6 +12,8 @@ namespace Lunr\Shadow; +use UnexpectedValueException; + /** * Getopt like command line argument parser. However, it * does a few things different from getopt. While getopt @@ -94,7 +96,25 @@ public function __destruct() */ public function parse(): array { - $this->args = $_SERVER['argv']; + if (!is_array($_SERVER['argv'])) + { + throw new UnexpectedValueException('Command line arguments are not stored in an array!'); + } + + if (!array_is_list($_SERVER['argv'])) + { + throw new UnexpectedValueException('Command line arguments are not stored as a list!'); + } + + foreach ($_SERVER['argv'] as $index => $arg) + { + if (!is_string($arg)) + { + throw new UnexpectedValueException('Command line argument is not a string!'); + } + + $this->args[$index] = $arg; + } foreach ($this->args as $index => $arg) { diff --git a/src/Lunr/Shadow/Tests/LunrCliParserParseTest.php b/src/Lunr/Shadow/Tests/LunrCliParserParseTest.php index a49554d..61be688 100644 --- a/src/Lunr/Shadow/Tests/LunrCliParserParseTest.php +++ b/src/Lunr/Shadow/Tests/LunrCliParserParseTest.php @@ -10,6 +10,8 @@ namespace Lunr\Shadow\Tests; +use UnexpectedValueException; + /** * This class contains test methods for parse() in the LunrCliParser class. * @@ -18,6 +20,57 @@ class LunrCliParserParseTest extends LunrCliParserTest { + /** + * Test that parsing with an invalid argv throws an exception. + * + * @covers Lunr\Shadow\LunrCliParser::parse + */ + public function testParseArgvWithArgvAsString(): void + { + $_SERVER['argv'] = 'script.php'; + + $this->expectException(UnexpectedValueException::class); + $this->expectExceptionMessage('Command line arguments are not stored in an array!'); + + $value = $this->class->parse(); + + $this->assertArrayEmpty($value); + } + + /** + * Test that parsing with an invalid argv throws an exception. + * + * @covers Lunr\Shadow\LunrCliParser::parse + */ + public function testParseArgvWithArgvAsAssociativeArray(): void + { + $_SERVER['argv'] = [ 1 => 'script.php' ]; + + $this->expectException(UnexpectedValueException::class); + $this->expectExceptionMessage('Command line arguments are not stored as a list!'); + + $value = $this->class->parse(); + + $this->assertArrayEmpty($value); + } + + /** + * Test that parsing with an invalid argv throws an exception. + * + * @covers Lunr\Shadow\LunrCliParser::parse + */ + public function testParseArgvWithArgvAsIntArray(): void + { + $_SERVER['argv'] = [ 1, 'script.php' ]; + + $this->expectException(UnexpectedValueException::class); + $this->expectExceptionMessage('Command line argument is not a string!'); + + $value = $this->class->parse(); + + $this->assertArrayEmpty($value); + } + /** * Test that parsing no arguments returns an empty array. *