From 6a35895c4b1ecbd848d0f38c307151d2fd8e0172 Mon Sep 17 00:00:00 2001 From: Tom Kay Date: Thu, 22 Oct 2026 12:49:37 +0100 Subject: [PATCH] Treat null keys and section names as empty strings on PHP 8.5 PHP 8.5 deprecates null as an array offset, and 8.1 deprecates passing null to string parameters. Config used caller-supplied keys, section names, INI paths and INI strings directly, so a null from the caller raised a deprecation, which is an exception where deprecations are converted. Each is now read as '', which is what PHP did implicitly before, so stored keys and thrown exceptions are unchanged. The tests convert deprecations to exceptions so CI catches new ones. Co-Authored-By: Claude Opus 5.5 --- .gitignore | 1 + phpunit.xml | 1 + src/Provider/AbstractConfigProvider.php | 14 ++- src/Provider/ConfigSection.php | 11 ++- .../Ini/AbstractIniConfigProvider.php | 1 + src/Provider/Ini/IniConfigProvider.php | 1 + tests/NullKeyTest.php | 97 +++++++++++++++++++ 7 files changed, 116 insertions(+), 10 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/phpunit.xml b/phpunit.xml index 564f4ce..f975ade 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -7,6 +7,7 @@ convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" + convertDeprecationsToExceptions="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"> diff --git a/src/Provider/AbstractConfigProvider.php b/src/Provider/AbstractConfigProvider.php index 5b4b615..f53a826 100644 --- a/src/Provider/AbstractConfigProvider.php +++ b/src/Provider/AbstractConfigProvider.php @@ -24,13 +24,14 @@ abstract class AbstractConfigProvider implements ConfigProviderInterface */ public function addItem($section, $item, $value) { + $section = $section ?? ''; if(isset($this->_sections[$section])) { $this->_sections[$section]->addItem($item, $value); } else { - $this->addSection(new ConfigSection($section, [$item => $value])); + $this->addSection(new ConfigSection($section, [$item ?? '' => $value])); } return $this; @@ -46,6 +47,7 @@ public function addItem($section, $item, $value) */ public function removeItem($section, $item) { + $section = $section ?? ''; if(isset($this->_sections[$section])) { $this->_sections[$section]->removeItem($item); @@ -113,6 +115,7 @@ public function getSections() */ public function getSection($name, $throw = true) { + $name = $name ?? ''; if(isset($this->_sections[$name])) { return $this->_sections[$name]; @@ -135,7 +138,7 @@ public function getSection($name, $throw = true) */ public function sectionExists($name) { - return isset($this->_sections[$name]); + return isset($this->_sections[$name ?? '']); } /** @@ -147,7 +150,7 @@ public function sectionExists($name) */ public function has($name) { - return isset($this->_sections[$name]); + return isset($this->_sections[$name ?? '']); } /** @@ -165,7 +168,7 @@ public function addSection(ConfigSectionInterface $section) ); } - $this->_sections[$section->getName()] = $section; + $this->_sections[$section->getName() ?? ''] = $section; return $this; } @@ -178,7 +181,7 @@ public function addSection(ConfigSectionInterface $section) */ public function setSection(ConfigSectionInterface $section) { - $this->_sections[$section->getName()] = $section; + $this->_sections[$section->getName() ?? ''] = $section; return $this; } @@ -204,6 +207,7 @@ public function removeSection(ConfigSectionInterface $section) */ public function removeSectionByName($sectionName) { + $sectionName = $sectionName ?? ''; if(isset($this->_sections[$sectionName])) { unset($this->_sections[$sectionName]); diff --git a/src/Provider/ConfigSection.php b/src/Provider/ConfigSection.php index 7529f65..44943a1 100644 --- a/src/Provider/ConfigSection.php +++ b/src/Provider/ConfigSection.php @@ -55,7 +55,7 @@ public function getName() */ public function has($key) { - return isset($this->_items[$key]); + return isset($this->_items[$key ?? '']); } /** @@ -70,6 +70,7 @@ public function has($key) */ public function getItem($key, $default = null) { + $key = $key ?? ''; if(isset($this->_items[$key])) { return $this->_items[$key]; @@ -104,7 +105,7 @@ public function getItems() */ public function addItem($key, $value) { - $this->_items[$key] = $value; + $this->_items[$key ?? ''] = $value; return $this; } @@ -133,7 +134,7 @@ public function addItems(array $keyValueItems) */ public function removeItem($key) { - unset($this->_items[$key]); + unset($this->_items[$key ?? '']); return $this; } @@ -154,7 +155,7 @@ public function removeItem($key) */ public function offsetExists($offset): bool { - return isset($this->_items[$offset]); + return isset($this->_items[$offset ?? '']); } /** @@ -209,6 +210,6 @@ public function offsetSet($offset, $value): void */ public function offsetUnset($offset): void { - unset($this->_items[$offset]); + unset($this->_items[$offset ?? '']); } } diff --git a/src/Provider/Ini/AbstractIniConfigProvider.php b/src/Provider/Ini/AbstractIniConfigProvider.php index bfed470..cf5ce23 100644 --- a/src/Provider/Ini/AbstractIniConfigProvider.php +++ b/src/Provider/Ini/AbstractIniConfigProvider.php @@ -15,6 +15,7 @@ abstract class AbstractIniConfigProvider extends AbstractConfigProvider */ protected function _loadString($iniString, $parseEnv) { + $iniString = $iniString ?? ''; if($parseEnv) { $iniString = $this->_parseEnvVars($iniString); diff --git a/src/Provider/Ini/IniConfigProvider.php b/src/Provider/Ini/IniConfigProvider.php index 2e02b17..f2061ee 100644 --- a/src/Provider/Ini/IniConfigProvider.php +++ b/src/Provider/Ini/IniConfigProvider.php @@ -28,6 +28,7 @@ public function __construct($file = null, $parseEnv = false) */ public function loadFile($fullPath, $parseEnv = false) { + $fullPath = $fullPath ?? ''; if(!file_exists($fullPath)) { throw new RuntimeException("Config file '$fullPath' could not be found"); diff --git a/tests/NullKeyTest.php b/tests/NullKeyTest.php new file mode 100644 index 0000000..fa24be7 --- /dev/null +++ b/tests/NullKeyTest.php @@ -0,0 +1,97 @@ +addItem(null, 'value'); + + $this->assertSame(['' => 'value'], $section->getItems()); + $this->assertSame('value', $section->getItem(null)); + $this->assertTrue($section->has(null)); + + $section->removeItem(null); + $this->assertFalse($section->has('')); + $this->assertSame('default', $section->getItem(null, 'default')); + } + + public function testSectionArrayAccessNullOffset() + { + $section = new ConfigSection('db'); + $section[] = 'appended'; + + $this->assertSame('appended', $section->getItem('')); + $this->assertTrue(isset($section[null])); + $this->assertSame('appended', $section[null]); + + unset($section[null]); + $this->assertFalse(isset($section[''])); + } + + public function testProviderNullSectionAndKey() + { + $provider = new ConfigProvider(); + $provider->addItem(null, 'key', 'value'); + $provider->addItem('db', null, 'empty'); + + $this->assertTrue($provider->sectionExists(null)); + $this->assertTrue($provider->has(null)); + $this->assertSame('value', $provider->getItem(null, 'key')); + $this->assertSame('empty', $provider->getItem('db', null)); + $this->assertTrue($provider->hasItem('db', null)); + $this->assertSame('value', $provider->getSection(null)->getItem('key')); + + $provider->removeItem(null, 'key'); + $this->assertFalse($provider->hasItem('', 'key')); + + $provider->removeSectionByName(null); + $this->assertFalse($provider->sectionExists('')); + } + + public function testProviderNullSectionName() + { + $provider = new ConfigProvider(); + $provider->addSection(new ConfigSection(null, ['a' => 'b'])); + $this->assertSame('b', $provider->getItem('', 'a')); + + $provider->setSection(new ConfigSection(null, ['a' => 'c'])); + $this->assertSame('c', $provider->getItem('', 'a')); + + $provider->removeSection(new ConfigSection(null)); + $this->assertFalse($provider->sectionExists('')); + $this->assertSame('missing', $provider->getSection(null, false)->getItem('x', 'missing')); + } + + public function testIniNullFile() + { + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage("Config file '' could not be found"); + (new IniConfigProvider())->loadFile(null); + } + + public function testIniNullString() + { + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage("The ini string passed is corrupt or invalid"); + (new IniConfigProvider())->loadString(null); + } + + public function testIniNullStringWithEnv() + { + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage("The ini string passed is corrupt or invalid"); + (new IniConfigProvider())->loadString(null, true); + } +}