-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[HttpFoundation] Suppress side effects in 'get' and 'has' methods of NamespacedAttributeBag #27927
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
[HttpFoundation] Suppress side effects in 'get' and 'has' methods of NamespacedAttributeBag #27927
Conversation
if (!$writeContext) { | ||
$null = null; | ||
|
||
return $null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can do return null;
instead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The trick is that function returns by reference protected function &resolveAttributePath($name, $writeContext = false)
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're returning $null
here, which does not exist before the assignment here, so it's not referencing to anything.
Edit: is this a requirement within php because the reference has to point to a variable, and thus it's basically a hack to make it work?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exactly, in php you cannot return immediate values when funciton returns reference:
function &f() {
return 'value'; // Notice: Only variable references should be returned by reference
}
So, a trick is required:
function &f() {
$dummy = 'value';
return $dummy; // Fine
}
Anyway when we need to return null we can shorten the hack to:
function &f() {
return $null; // Fine
}
because the result is exactly null
:
true === is_null(f());
Cheers
Thank you @webnet-fr. |
…methods of NamespacedAttributeBag (webnet-fr) This PR was submitted for the 3.4 branch but it was merged into the 2.8 branch instead (closes #27927). Discussion ---------- [HttpFoundation] Suppress side effects in 'get' and 'has' methods of NamespacedAttributeBag | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #27912 | License | MIT | Doc PR | - As @Gemineye reported there was a bug in `get` and `has` methods of NamespacedAttributeBag. These methods accept composite names as an argument (like 'foo/bar' or 'foo/bar/baz') to reach the elements of stored arrays. Up to now these methods erroneously created entries (`->get('foo/bar')` created `['foo' => null]`, `->get('foo/bar/baz')` created `['foo' => ['bar' => null]]`). Commits ------- 5f59ad4 suppress side effects in 'get' or 'has' methods of NamespacedAttributeBag
As @Gemineye reported there was a bug in
get
andhas
methods of NamespacedAttributeBag. These methods accept composite names as an argument (like 'foo/bar' or 'foo/bar/baz') to reach the elements of stored arrays. Up to now these methods erroneously created entries (->get('foo/bar')
created['foo' => null]
,->get('foo/bar/baz')
created['foo' => ['bar' => null]]
).