fix: do not read an undefined offset when a value argument is last

The argument parser looked up the next command-line token via $argv[$key + 1]
guarded only by count($argv) === 0. The remaining arguments array is not
reindexed after earlier arguments are removed, so a value-taking argument placed
last (after an unconsumed token) read an undefined offset and raised an Undefined
array key warning under PHP 8, and the argument was set to empty instead of
falling back to its default. Check that the next offset exists with isset().
This commit is contained in:
dualfroz committed 2026-09-04 09:21:56 +02:00
1 parent e65a9b267b
commit 94942b3aff
3 files changed
+119 -1

No files matched your search

+24
View File
@@ -87,6 +87,30 @@ class ManagerTest extends TestCase
$this->assertEquals('', $this->manager->get('foo'));
}
public function testItParsesAValueTakingArgumentThatIsLastWithoutAWarning()
{
$this->manager->add([
'name' => ['prefix' => 'n', 'longPrefix' => 'name', 'defaultValue' => 'default-name'],
]);
set_error_handler(static function (int $errno, string $errstr): bool {
throw new \ErrorException($errstr, 0, $errno);
});
try {
// "somefile" is an unconsumed non-prefixed token, so "-n" is the
// last element but not at a reindexed offset; reading the next
// offset must not raise an "Undefined array key" warning.
$this->manager->parse(['command', 'somefile', '-n']);
} finally {
restore_error_handler();
}
// No value was supplied for -n, so it must fall back to its default.
$this->assertFalse($this->manager->provided('name'));
$this->assertEquals('default-name', $this->manager->get('name'));
}
public function testItStoresTrailingInArray()
{
$this->manager->add([