Skip to content

get() still treats null values as missing keys despite v3.0.0 fix #55

@ruttydm

Description

@ruttydm

Bug

Data::get() returns the default value when a key exists but has a null value, contradicting both the interface documentation and the v3.0.0 changelog which states this was fixed.

Version: 3.0.3

Code

Data.php line 138:

return $currentValue === null ? $default : $currentValue;

The traversal loop (lines 126–136) correctly uses array_key_exists() to distinguish missing keys from null values. But line 138 throws that distinction away by treating null as "not found."

Reproduction

$data = new Data(['setting' => null]);

$data->has('setting');              // true ✓ — key exists
$data->get('setting', 'fallback'); // 'fallback' ✗ — should return null
$data->get('setting');             // null — correct by accident ($default is null)

Inconsistencies

  1. Interface contract (DataInterface.php line 65): "If the key does not exist, an optional default value can be returned instead." — specifies missing keys, not null values.

  2. CHANGELOG.md v3.0.0: "Fixed get() method behaving as if keys with null values didn't exist" — this exact bug was claimed to be fixed.

  3. has() vs get(): has('key') correctly returns true for null-valued keys (uses array_key_exists), but get('key', 'default') returns 'default' as if the key doesn't exist.

  4. ArrayAccess: isset($data['key']) returns true (via has()), but the underlying get() would return the default instead of null.

Suggested fix

return $currentValue;

Simply return $currentValue as-is. The missing-key case is already handled by the loop above (lines 127–133), which either returns $default or throws MissingPathException.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions