From dd4ae33189ef4f2d520ae92e7255d884137f25bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B1=90?= <1578804454@qq.com> Date: Sun, 25 Oct 2026 23:54:08 +0800 Subject: [PATCH 1/3] Return null for existing null-valued paths --- src/Data.php | 2 +- src/DataInterface.php | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Data.php b/src/Data.php index 3409b8e..f07fce1 100644 --- a/src/Data.php +++ b/src/Data.php @@ -135,7 +135,7 @@ public function get(string $key, $default = null) $currentValue = $currentValue[$currentKey]; } - return $currentValue === null ? $default : $currentValue; + return $currentValue; } /** diff --git a/src/DataInterface.php b/src/DataInterface.php index 5909a8c..085048a 100644 --- a/src/DataInterface.php +++ b/src/DataInterface.php @@ -63,7 +63,8 @@ public function remove(string $key): void; * Get the raw value for a key * * If the key does not exist, an optional default value can be returned instead. - * If no default is provided then an exception will be thrown instead. + * If the key exists and its value is null, null is returned. + * If the key does not exist and no default is provided, an exception is thrown. * * @param string $key * @param mixed $default From 8fa12d9a95b93e33f9774f2b3fb0d265293d735e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B1=90?= <1578804454@qq.com> Date: Sun, 25 Oct 2026 23:54:47 +0800 Subject: [PATCH 2/3] Cover null-valued and missing paths with explicit defaults --- tests/DataTest.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/DataTest.php b/tests/DataTest.php index 3777a3d..0f27dc3 100644 --- a/tests/DataTest.php +++ b/tests/DataTest.php @@ -202,6 +202,21 @@ public function testGetWhenValueDoesNotExist() $data->get('foo.bar'); } + public function testGetReturnsNullForExistingNullValueWhenDefaultIsGiven() + { + $data = new Data([ + 'setting' => null, + 'nested' => [ + 'setting' => null, + ], + ]); + + $this->assertTrue($data->has('setting')); + $this->assertNull($data->get('setting', 'fallback')); + $this->assertNull($data->get('nested.setting', 'fallback')); + $this->assertSame('fallback', $data->get('missing', 'fallback')); + } + public function testHas() { $data = new Data($this->getSampleData()); From 5ad5246ea205a6deaca0595ed6f683b9ce8ac4f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B1=90?= <1578804454@qq.com> Date: Sun, 25 Oct 2026 23:55:10 +0800 Subject: [PATCH 3/3] Document defaults as applying only to missing paths --- CHANGELOG.md | 4 ++++ README.md | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b8b468d..d916430 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + + - `Data::get()` now returns `null` for existing null-valued keys instead of the supplied default. + ## [3.0.3] - 2024-07-08 ### Fixed diff --git a/README.md b/README.md index 775fbdf..025652c 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,10 @@ $data->get('some.path.that.does.not.exist', 'some-default-value'); // throws a MissingPathException because no default was given $data->get('some.path.that.does.not.exist'); + +// null (the default is only used when the path does not exist) +$data = new Data(['setting' => null]); +$data->get('setting', 'fallback'); ``` A more concrete example: