Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[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

Merged
merged 1 commit into from
Jul 13, 2018
Merged

[HttpFoundation] Suppress side effects in 'get' and 'has' methods of NamespacedAttributeBag #27927

merged 1 commit into from
Jul 13, 2018

Conversation

webnet-fr
Copy link
Contributor

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]]).

if (!$writeContext) {
$null = null;

return $null;
Copy link
Contributor

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

Copy link
Contributor Author

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).

Copy link
Contributor

@linaori linaori Jul 11, 2018

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?

Copy link
Contributor Author

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

@fabpot fabpot changed the base branch from 3.4 to 2.8 July 13, 2018 11:19
@fabpot
Copy link
Member

fabpot commented Jul 13, 2018

Thank you @webnet-fr.

@fabpot fabpot merged commit 5f59ad4 into symfony:2.8 Jul 13, 2018
fabpot added a commit that referenced this pull request Jul 13, 2018
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants