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

Passing array item by reference breaks \DS\Set behaviour #193

Closed
Feolius opened this issue Dec 24, 2022 · 1 comment
Closed

Passing array item by reference breaks \DS\Set behaviour #193

Feolius opened this issue Dec 24, 2022 · 1 comment
Labels

Comments

@Feolius
Copy link

Feolius commented Dec 24, 2022

php -v: PHP 8.1.12 (cli) (built: Oct 28 2022 19:11:07) (NTS)
ds version: 1.4.0

Initial problematic codeblock looks like this.

$data = [
    [
        'id' => 1,
        'data' => 'test',
    ],
    [
         'id' => 2,
         'data' => 'test',
    ]
];
foreach ($data as &$item) {
    unset($item['id']);
}
$s = new \Ds\Set($data);

Please pay attention on foreach cycle where we are passing array item by reference to get rid of unique id field. After that I expect the following code

$res = $s->toArray();
var_dump($res);

prints me an array with single item ['data' => 'test']. However instead I have the following output:

array(2) {
  [0]=>
  &array(1) {
    ["data"]=>
    string(4) "test"
  }
  [1]=>
  &array(1) {
    ["data"]=>
    string(4) "test"
  }
}

So, we get an array of references which are indeed different. But this is not what I would expect to see.
Then I decided to slightly change initial code:

$data = [
    [
        'data' => 'test',
    ],
    [
        'data' => 'test',
    ]
];
foreach ($data as &$item) {
    // Do nothing
}
$s = new \Ds\Set($data);

And result is still the same. But removing '&' from cycle actually makes it work correctly.

It's worth noting that polyfill works correctly in all cases. So, that leads me here for behaviour explanation. I know that passing array element by reference can be potentially dangerous in case of further normal array assignment (as described here https://www.php.net/manual/en/language.references.whatdo.php). However it doesn't make sense how it can be related to these changes in a Set behaviour and why polyfill behaviour is different from extension implementation.

@rtheunissen
Copy link
Member

Almost a year later, I'm finally coming back to this project and will take a look at this bug. 👀

rtheunissen added a commit that referenced this issue Dec 19, 2023
Fix #193: Passing array item by reference breaks \DS\Set behaviour
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants