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

[2.0] Fix a bug in Registry.php #49

Merged
merged 6 commits into from
Jun 19, 2022

Conversation

Wang-Yu-Chao
Copy link
Contributor

In class Registry, function set() is supposed to return the old value if exists, according to the comment.
But actually return the newly set value, since assignment is right-associated.

src/Registry.php Outdated
@@ -508,11 +508,13 @@ public function set($path, $value, $separator = null)
switch (true)
{
case (is_object($node)):
$result = $node->{$nodes[$i]} = $value;
$result = $node->{$nodes[$i]};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be $result = $node->{$nodes[$i]} ?? null; (similar for the array case), as is it's causing unit test failures because of undefined properties.

src/Registry.php Outdated
@@ -508,11 +508,13 @@ public function set($path, $value, $separator = null)
switch (true)
{
case (is_object($node)):
$result = $node->{$nodes[$i]} = $value;
$result = $node->{$nodes[$i]} ?? $value;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the intent is to return the previously set value, I'd suggest returning null would be the correct behavior. Otherwise now you're in a mixed case where if it wasn't previously set you're getting the newly set value and if it was previously set you're getting the old value and that API inconsistency isn't necessarily a good thing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. But seems that $result = $node->{$nodes[$i]} ?? null cannot pass unit test...strange

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which is why I said this had to be fixed on the 2.0 branch only. Because the current code behavior, and inherently the tests, all return the newly set value and if the intent is to change it to follow the docs and return the old value, the tests need updating to account for that change in behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Thank you for your patience.

@Wang-Yu-Chao
Copy link
Contributor Author

Should we correct the code and unit tests here or change the document instead?
I only find this returned value used in method setParam in class User, and method onAjaxSampledataApplyStep1 in class PlgSampledataBlog. And seems they don't cause any side effect.

@mbabker
Copy link
Contributor

mbabker commented Mar 22, 2018

Personally, I would suggest to change it.

  • AbstractApplication::set() is a wrapper around Registry::set() which returns the previous value, it was coded in a way where it bypasses this API inconsistency
  • The setters in Language all return the previous value
  • Session::set() returns the previous value

So our API design favors setters returning the previous values, with that said though there are some places where our setters have no return as well (i.e. our Uri object). Personally, I favor the return previous value approach, but either way I don't think returning the same value you're setting is correct.

@wilsonge thoughts?

nibra added 2 commits June 19, 2022 12:03
# Conflicts:
#	src/Registry.php
The tests where looking for a different behaviour than documented. Setting a value in a Registry should actually return the previous value (or null, if set for the first time).
@nibra nibra merged commit 2c144f0 into joomla-framework:2.0-dev Jun 19, 2022
@nibra
Copy link
Contributor

nibra commented Jun 19, 2022

Thank you!

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