Skip to content

Commit f8e2ad9

Browse files
authored
Merge pull request #1 from pprkut/phpstan
LunrCliParser: Type-safe access to $_SERVER['argv']
2 parents 6393069 + 908a34d commit f8e2ad9

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

src/Lunr/Shadow/LunrCliParser.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
namespace Lunr\Shadow;
1414

15+
use UnexpectedValueException;
16+
1517
/**
1618
* Getopt like command line argument parser. However, it
1719
* does a few things different from getopt. While getopt
@@ -94,7 +96,25 @@ public function __destruct()
9496
*/
9597
public function parse(): array
9698
{
97-
$this->args = $_SERVER['argv'];
99+
if (!is_array($_SERVER['argv']))
100+
{
101+
throw new UnexpectedValueException('Command line arguments are not stored in an array!');
102+
}
103+
104+
if (!array_is_list($_SERVER['argv']))
105+
{
106+
throw new UnexpectedValueException('Command line arguments are not stored as a list!');
107+
}
108+
109+
foreach ($_SERVER['argv'] as $index => $arg)
110+
{
111+
if (!is_string($arg))
112+
{
113+
throw new UnexpectedValueException('Command line argument ' . $index . ' is not a string!');
114+
}
115+
116+
$this->args[$index] = $arg;
117+
}
98118

99119
foreach ($this->args as $index => $arg)
100120
{

src/Lunr/Shadow/Tests/LunrCliParserParseTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
namespace Lunr\Shadow\Tests;
1212

13+
use UnexpectedValueException;
14+
1315
/**
1416
* This class contains test methods for parse() in the LunrCliParser class.
1517
*
@@ -18,6 +20,57 @@
1820
class LunrCliParserParseTest extends LunrCliParserTest
1921
{
2022

23+
/**
24+
* Test that parsing with an invalid argv throws an exception.
25+
*
26+
* @covers Lunr\Shadow\LunrCliParser::parse
27+
*/
28+
public function testParseArgvWithArgvAsString(): void
29+
{
30+
$_SERVER['argv'] = 'script.php';
31+
32+
$this->expectException(UnexpectedValueException::class);
33+
$this->expectExceptionMessage('Command line arguments are not stored in an array!');
34+
35+
$value = $this->class->parse();
36+
37+
$this->assertArrayEmpty($value);
38+
}
39+
40+
/**
41+
* Test that parsing with an invalid argv throws an exception.
42+
*
43+
* @covers Lunr\Shadow\LunrCliParser::parse
44+
*/
45+
public function testParseArgvWithArgvAsAssociativeArray(): void
46+
{
47+
$_SERVER['argv'] = [ 1 => 'script.php' ];
48+
49+
$this->expectException(UnexpectedValueException::class);
50+
$this->expectExceptionMessage('Command line arguments are not stored as a list!');
51+
52+
$value = $this->class->parse();
53+
54+
$this->assertArrayEmpty($value);
55+
}
56+
57+
/**
58+
* Test that parsing with an invalid argv throws an exception.
59+
*
60+
* @covers Lunr\Shadow\LunrCliParser::parse
61+
*/
62+
public function testParseArgvWithArgvAsIntArray(): void
63+
{
64+
$_SERVER['argv'] = [ 1, 'script.php' ];
65+
66+
$this->expectException(UnexpectedValueException::class);
67+
$this->expectExceptionMessage('Command line argument 0 is not a string!');
68+
69+
$value = $this->class->parse();
70+
71+
$this->assertArrayEmpty($value);
72+
}
73+
2174
/**
2275
* Test that parsing no arguments returns an empty array.
2376
*

0 commit comments

Comments
 (0)