Skip to content

Conversation

@rodrigopedra
Copy link
Contributor

PR #57570 introduced the data_has() helper to aid fixing the EnumerateValues::value() implementation.

The new helper works for the purposes of that PR, but is misaligned with other sibilings helpers, such as data_get().

For example, using the current implementation, all these assertions fail when then should pass:

$this->assertTrue(data_has($object, 'users.name.0'));
$this->assertTrue(data_has($array, '0.users.0.name'));
$this->assertTrue(data_has($dottedArray, ['users', 'first.name']));
$this->assertTrue(data_has($dottedArray, ['users', 'middle.name']));
$this->assertFalse(data_has($sameKeyMultiLevel, 'foo.name'));

This assertions were adapted from the data_get() helper tests.

The last one fails due to the current implementation using a while loop to search for a property segment existence.

This PR:

  • Refactor the unreleased data_has() helper to behave more aligned with other sibling helpers.
    • Actually the logic is basically the same to traverse the data_get() helper\
    • The difference is that data_has() uses property_exists() and data_get() uses isset() to verify a object's property
  • Add tests for the data_has() helper with assertions that would fail with the current implementation

@taylorotwell taylorotwell merged commit 47de756 into laravel:12.x Oct 30, 2025
68 checks passed
@taylorotwell
Copy link
Member

Thank you!

@decadence
Copy link
Contributor

This function returns false for valid 0 key or I'm missing something? For example:

$array = [1, 2, 3];

data_has($array, "0"); // wrong false

@rodrigopedra rodrigopedra deleted the fix-data_has branch October 30, 2025 09:33
@rodrigopedra
Copy link
Contributor Author

@decadence sent PR #57586

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants