From 19958e86d1368c8be37be99d14cde907e7a91289 Mon Sep 17 00:00:00 2001 From: Tom Kay Date: Thu, 22 Oct 2026 12:50:57 +0100 Subject: [PATCH] Throw on null offsets in ConfigSection array access isset() and unset() on a ConfigSection used the offset directly, so a null offset raised PHP 8.5's null array offset deprecation instead of the TypeError every other entry point throws for a null key. They now go through has() and removeItem(). Tests cover null for each key, section name, INI path and INI string entry point. Co-Authored-By: Claude Opus 5.5 --- .gitignore | 1 + src/Provider/ConfigSection.php | 4 +- tests/NullKeyTest.php | 67 ++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 tests/NullKeyTest.php diff --git a/.gitignore b/.gitignore index d1502b0..971326f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ vendor/ composer.lock +/.phpunit.result.cache diff --git a/src/Provider/ConfigSection.php b/src/Provider/ConfigSection.php index 07572a8..a77d944 100644 --- a/src/Provider/ConfigSection.php +++ b/src/Provider/ConfigSection.php @@ -81,7 +81,7 @@ public function removeItem(string $key): static #[\Override] public function offsetExists(mixed $offset): bool { - return isset($this->_items[$offset]); + return $this->has($offset); } #[\Override] @@ -99,6 +99,6 @@ public function offsetSet(mixed $offset, mixed $value): void #[\Override] public function offsetUnset(mixed $offset): void { - unset($this->_items[$offset]); + $this->removeItem($offset); } } diff --git a/tests/NullKeyTest.php b/tests/NullKeyTest.php new file mode 100644 index 0000000..065791c --- /dev/null +++ b/tests/NullKeyTest.php @@ -0,0 +1,67 @@ + new ConfigSection('db', ['a' => 1]); + $provider = static fn() => (new ConfigProvider())->addItem('db', 'a', 1); + + return [ + 'section getItem' => [fn() => $section()->getItem(null)], + 'section has' => [fn() => $section()->has(null)], + 'section addItem' => [fn() => $section()->addItem(null, 1)], + 'section removeItem' => [fn() => $section()->removeItem(null)], + 'section isset offset' => [fn() => isset($section()[null])], + 'section get offset' => [fn() => $section()[null]], + 'section append' => [function () use ($section) { $s = $section(); $s[] = 1; }], + 'section unset offset' => [function () use ($section) { $s = $section(); unset($s[null]); }], + 'section name' => [fn() => new ConfigSection(null)], + 'section setName' => [fn() => $section()->setName(null)], + 'provider getItem section' => [fn() => $provider()->getItem(null, 'a')], + 'provider getItem key' => [fn() => $provider()->getItem('db', null)], + 'provider addItem section' => [fn() => $provider()->addItem(null, 'a', 1)], + 'provider addItem key' => [fn() => $provider()->addItem('db', null, 1)], + 'provider getSection' => [fn() => $provider()->getSection(null)], + 'provider sectionExists' => [fn() => $provider()->sectionExists(null)], + 'provider has' => [fn() => $provider()->has(null)], + 'provider hasItem' => [fn() => $provider()->hasItem(null, 'a')], + 'provider removeItem' => [fn() => $provider()->removeItem(null, 'a')], + 'provider removeSection' => [fn() => $provider()->removeSectionByName(null)], + 'ini loadFile' => [fn() => (new IniConfigProvider())->loadFile(null)], + 'ini loadString' => [fn() => (new IniConfigProvider())->loadString(null)], + 'cached ini loadCached' => [fn() => (new CachedIniConfigProvider())->loadCached([sys_get_temp_dir()], null)], + ]; + } + + #[DataProvider('nullCalls')] + public function testNullThrows(Closure $call): void + { + $this->expectException(TypeError::class); + $call(); + } + + public function testIntegerOffsetsStillWork(): void + { + $section = new ConfigSection('db'); + $section[0] = 'zero'; + $this->assertTrue(isset($section[0])); + $this->assertSame('zero', $section[0]); + unset($section[0]); + $this->assertFalse(isset($section[0])); + } +}