Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
vendor/
composer.lock
/.phpunit.result.cache
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down
14 changes: 9 additions & 5 deletions src/Provider/AbstractConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -113,6 +115,7 @@ public function getSections()
*/
public function getSection($name, $throw = true)
{
$name = $name ?? '';
if(isset($this->_sections[$name]))
{
return $this->_sections[$name];
Expand All @@ -135,7 +138,7 @@ public function getSection($name, $throw = true)
*/
public function sectionExists($name)
{
return isset($this->_sections[$name]);
return isset($this->_sections[$name ?? '']);
}

/**
Expand All @@ -147,7 +150,7 @@ public function sectionExists($name)
*/
public function has($name)
{
return isset($this->_sections[$name]);
return isset($this->_sections[$name ?? '']);
}

/**
Expand All @@ -165,7 +168,7 @@ public function addSection(ConfigSectionInterface $section)
);
}

$this->_sections[$section->getName()] = $section;
$this->_sections[$section->getName() ?? ''] = $section;
return $this;
}

Expand All @@ -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;
}

Expand All @@ -204,6 +207,7 @@ public function removeSection(ConfigSectionInterface $section)
*/
public function removeSectionByName($sectionName)
{
$sectionName = $sectionName ?? '';
if(isset($this->_sections[$sectionName]))
{
unset($this->_sections[$sectionName]);
Expand Down
11 changes: 6 additions & 5 deletions src/Provider/ConfigSection.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function getName()
*/
public function has($key)
{
return isset($this->_items[$key]);
return isset($this->_items[$key ?? '']);
}

/**
Expand All @@ -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];
Expand Down Expand Up @@ -104,7 +105,7 @@ public function getItems()
*/
public function addItem($key, $value)
{
$this->_items[$key] = $value;
$this->_items[$key ?? ''] = $value;
return $this;
}

Expand Down Expand Up @@ -133,7 +134,7 @@ public function addItems(array $keyValueItems)
*/
public function removeItem($key)
{
unset($this->_items[$key]);
unset($this->_items[$key ?? '']);
return $this;
}

Expand All @@ -154,7 +155,7 @@ public function removeItem($key)
*/
public function offsetExists($offset): bool
{
return isset($this->_items[$offset]);
return isset($this->_items[$offset ?? '']);
}

/**
Expand Down Expand Up @@ -209,6 +210,6 @@ public function offsetSet($offset, $value): void
*/
public function offsetUnset($offset): void
{
unset($this->_items[$offset]);
unset($this->_items[$offset ?? '']);
}
}
1 change: 1 addition & 0 deletions src/Provider/Ini/AbstractIniConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ abstract class AbstractIniConfigProvider extends AbstractConfigProvider
*/
protected function _loadString($iniString, $parseEnv)
{
$iniString = $iniString ?? '';
if($parseEnv)
{
$iniString = $this->_parseEnvVars($iniString);
Expand Down
1 change: 1 addition & 0 deletions src/Provider/Ini/IniConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
97 changes: 97 additions & 0 deletions tests/NullKeyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

namespace Packaged\Config\Test;

use Packaged\Config\Provider\ConfigProvider;
use Packaged\Config\Provider\ConfigSection;
use Packaged\Config\Provider\Ini\IniConfigProvider;
use PHPUnit\Framework\TestCase;

/**
* A null key or section name is stored under the empty string key, as PHP
* did implicitly before 8.5 deprecated null array offsets
*/
class NullKeyTest extends TestCase
{
public function testSectionNullKey()
{
$section = new ConfigSection('db');
$section->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);
}
}
Loading