8000 [HttpFoundation] Suppress side effects in 'get' and 'has' methods of NamespacedAttributeBag by webnet-fr · Pull Request #27927 · symfony/symfony · GitHub
[go: up one dir, main page]

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,13 @@ protected function &resolveAttributePath($name, $writeContext = false)

foreach ($parts as $part) {
if (null !== $array && !array_key_exists($part, $array)) {
$array[$part] = $writeContext ? array() : 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

}

$array[$part] = array();
}

$array = &$array[$part];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,17 @@ public function testHas($key, $value, $exists)
$this->assertEquals($exists, $this->bag->has($key));
}

/**
* @dataProvider attributesProvider
*/
public function testHasNoSideEffect($key, $value, $expected)
{
$expected = json_encode($this->bag->all());
$this->bag->has($key);

$this->assertEquals($expected, json_encode($this->bag->all()));
}

/**
* @dataProvider attributesProvider
*/
Expand All @@ -96,6 +107,17 @@ public function testGetDefaults()
$this->assertEquals('default', $this->bag->get('user2.login', 'default'));
}

/**
* @dataProvider attributesProvider
*/
public function testGetNoSideEffect($key, $value, $expected)
{
$expected = json_encode($this->bag->all());
$this->bag->get($key);

$this->assertEquals($expected, json_encode($this->bag->all()));
}

/**
* @dataProvider attributesProvider
*/
Expand Down
0