- Issue created by @fnalb2
- 🇮🇳India sidharth_soman Bangalore
I've made the required change. Please review.
- last update
over 1 year ago 29,946 pass
When a reference is used in a foreach loop instead of using a simple variable, the reference remains assigned and keeps its "value" which is a reference, even after the foreach execution. Most of the time, this is not what the developer is expecting and the reference may be used wrongly in the rest of the code. For this reason, it is recommended to always unset a reference that is used in a foreach to avoid any unexpected side effects. Make sure that the referenced value variable is unset after the loop.
File: core/modules/file/tests/src/Functional/FileListingTest.php
line: 108
foreach ($nodes as &$node) {
$this->drupalGet('node/' . $node->id() . '/edit');
$file = $this->getTestFile('image');
$edit = [
'files[file_0]' => \Drupal::service('file_system')->realpath($file->getFileUri()),
];
$this->submitForm($edit, 'Save');
$node = Node::load($node->id());
}
Testprogramm:
<?php
$help = ['A' => 'b', 'C' => 'd'];
print_r($help);
foreach ($help as $id => &$help_info) {
$help_info = [ 'id' => $id ];
}
print_r($help);
$i=0;
foreach ($help as $id => $help_info) {
$i++;
}
print_r($help);
?>
The output is as follows:
Array
(
[A] => b
[C] => d
)
Array
(
[A] => Array
(
[id] => A
)
[C] => Array
(
[id] => C
)
)
Array
(
[A] => Array
(
[id] => A
)
[C] => Array
(
[id] => A <===
)
)
line: 108
foreach ($nodes as &$node) {
$this->drupalGet('node/' . $node->id() . '/edit');
$file = $this->getTestFile('image');
$edit = [
'files[file_0]' => \Drupal::service('file_system')->realpath($file->getFileUri()),
];
$this->submitForm($edit, 'Save');
$node = Node::load($node->id());
}
unset($node);
no
no
no
no
Active
11.0 🔥
I've made the required change. Please review.