Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: dot_array_search() unexpected behavior #5940

Merged
merged 3 commits into from
May 20, 2022

Conversation

kenjis
Copy link
Member

@kenjis kenjis commented Apr 30, 2022

Description
Fixes #5939

Checklist:

  • Securely signed commits
  • [] Component(s) with PHPDoc blocks, only if necessary or adds value
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@kenjis kenjis added bug Verified issues on the current code behavior or pull requests that will fix them breaking change Pull requests that may break existing functionalities labels Apr 30, 2022
@sfadschm
Copy link
Contributor

I think I would prefer an exception to be thrown ("Array key does not exist." or so).
That is because I think null might be a valid return of a successful search.

I think there is two cases where the statement can be reached:

  • An array of post data which could be manipulated by the user/browser. In that case the search should fail rather then return (say as an additional validation).
  • The array to search was built by ourselves. In that case we should know which keys are accessible and it would make sense to me to let the dev know that the error occured instead of failing silently.

@kenjis
Copy link
Member Author

kenjis commented May 1, 2022

I think null might be a valid return of a successful search.

Yes.

But this returns null. Do you want to change to throw an exception?

public function testArrayDotReturnNullMissingValue()
{
$data = [
'foo' => [
'bar' => 23,
],
];
$this->assertNull(dot_array_search('foo.baz', $data));
}

@iRedds
Copy link
Collaborator

iRedds commented May 2, 2022

@sfadschm What if, instead of an exception, add a third argument to the function, which will contain a default value for the case when the key is not found?

@sfadschm
Copy link
Contributor

sfadschm commented May 2, 2022

But this returns null. Do you want to change to throw an exception?

Oh, right. I missed that we already use null as a failure indicator. I'm fine with this solution then!

@iRedds: Sounds like a nice addition, but might be out of scope for this PR. I also think changing the signature would be another BC break and while null might be a valid return value on success, it is still likely to be to most rarely used one.

@sfadschm
Copy link
Contributor

sfadschm commented May 2, 2022

Definitely needs a changelog and/or user guide entry though ☺

@kenjis kenjis force-pushed the fix-dot_array_search branch from 7f5ca5f to 4c3f857 Compare May 4, 2022 02:41
@kenjis
Copy link
Member Author

kenjis commented May 4, 2022

Added the documentation.

@kenjis kenjis force-pushed the fix-dot_array_search branch from 4c3f857 to be1610b Compare May 19, 2022 22:57
@kenjis
Copy link
Member Author

kenjis commented May 19, 2022

Rebased.
Please review.

@kenjis kenjis merged commit ab2b186 into codeigniter4:develop May 20, 2022
@kenjis kenjis deleted the fix-dot_array_search branch May 20, 2022 05:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change Pull requests that may break existing functionalities bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: dot_array_search() unexpected behavior
4 participants