Skip to content

Commit

Permalink
Return null if key doesn't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
lcharette committed Dec 31, 2023
1 parent 1155571 commit fb1f0fa
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
- Removed Assets
- Update to Laravel 10
- Update to PHPUnit 10
- [Config] Methods `getBool`, `getString`, `getInt` & `getArray` now return `null` if key doesn't exist, to make it on par with parent `get` method.

### Alert
- Messages are now translated at read time ([#1156](https://github.com/userfrosting/UserFrosting/pull/1156), [#811](https://github.com/userfrosting/UserFrosting/issues/811)). Messages will be translated when using `messages` and `getAndClearMessages`. `addMessage` now accept the optional placeholders, which will be stored with the alert message. `addMessageTranslated` is **deprecated**.
Expand Down
24 changes: 12 additions & 12 deletions src/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ class Config extends Repository
* @param string $key
* @param bool|null $default
*
* @return bool
* @return bool|null Returns null if the key is not found
*/
public function getBool(string $key, ?bool $default = null): bool
public function getBool(string $key, ?bool $default = null): ?bool
{
$value = $this->get($key, $default);

if (!is_bool($value)) {
if (!is_bool($value) && !is_null($value)) {
throw new TypeException("Config key '$key' doesn't return bool.");
}

Expand All @@ -44,13 +44,13 @@ public function getBool(string $key, ?bool $default = null): bool
* @param string $key
* @param string|null $default
*
* @return string
* @return string|null Returns null if the key is not found
*/
public function getString(string $key, ?string $default = null): string
public function getString(string $key, ?string $default = null): ?string
{
$value = $this->get($key, $default);

if (!is_string($value)) {
if (!is_string($value) && !is_null($value)) {
throw new TypeException("Config key '$key' doesn't return string.");
}

Expand All @@ -63,13 +63,13 @@ public function getString(string $key, ?string $default = null): string
* @param string $key
* @param int|null $default
*
* @return int
* @return int|null Returns null if the key is not found
*/
public function getInt(string $key, ?int $default = null): int
public function getInt(string $key, ?int $default = null): ?int
{
$value = $this->get($key, $default);

if (!is_int($value)) {
if (!is_int($value) && !is_null($value)) {
throw new TypeException("Config key '$key' doesn't return int.");
}

Expand All @@ -82,13 +82,13 @@ public function getInt(string $key, ?int $default = null): int
* @param string $key
* @param mixed[]|null $default
*
* @return mixed[]
* @return mixed[]|null Returns null if the key is not found
*/
public function getArray(string $key, ?array $default = null): array
public function getArray(string $key, ?array $default = null): ?array
{
$value = $this->get($key, $default);

if (!is_array($value)) {
if (!is_array($value) && !is_null($value)) {
throw new TypeException("Config key '$key' doesn't return array.");
}

Expand Down
16 changes: 14 additions & 2 deletions tests/Config/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@ class ConfigTest extends TestCase
'bool' => true,
'string' => 'foobar',
'int' => 92,
'array' => [],
'array' => ['foo', 'bar'],
];

public function testGetBool(): void
{
$repo = new Config($this->data);

$this->assertSame(true, $repo->getBool('bool'));
$this->assertSame(false, $repo->getBool('missing', false));
$this->assertSame(null, $repo->getBool('missing'));
$this->assertSame($repo->get('missing'), $repo->getBool('missing')); // Same default behavior as "get"
$this->expectException(TypeException::class);
$repo->getBool('string');
}
Expand All @@ -36,6 +39,9 @@ public function testGetString(): void
$repo = new Config($this->data);

$this->assertSame('foobar', $repo->getString('string'));
$this->assertSame('barfoo', $repo->getString('missing', 'barfoo'));
$this->assertSame(null, $repo->getString('missing'));
$this->assertSame($repo->get('missing'), $repo->getString('missing')); // Same default behavior as "get"
$this->expectException(TypeException::class);
$repo->getString('bool');
}
Expand All @@ -45,6 +51,9 @@ public function testGetInt(): void
$repo = new Config($this->data);

$this->assertSame(92, $repo->getInt('int'));
$this->assertSame(29, $repo->getInt('missing', 29));
$this->assertSame(null, $repo->getInt('missing'));
$this->assertSame($repo->get('missing'), $repo->getInt('missing')); // Same default behavior as "get"
$this->expectException(TypeException::class);
$repo->getInt('string');
}
Expand All @@ -53,7 +62,10 @@ public function testGetArray(): void
{
$repo = new Config($this->data);

$this->assertSame([], $repo->getArray('array'));
$this->assertSame(['foo', 'bar'], $repo->getArray('array'));
$this->assertSame(['bar', 'foo'], $repo->getArray('missing', ['bar', 'foo']));
$this->assertSame(null, $repo->getArray('missing'));
$this->assertSame($repo->get('missing'), $repo->getArray('missing')); // Same default behavior as "get"
$this->expectException(TypeException::class);
$repo->getArray('string');
}
Expand Down

0 comments on commit fb1f0fa

Please sign in to comment.