Using sonarqube to review out code we have this "crital" warning:
"Make sure that the referenced value variable is unset after the loop"
in views/plugins/views_wizard/views_ui_base_views_wizard.class.php
foreach ($path_fields as &$field) {
$field['id'] = view::generate_item_id($field['id'], $display_options['default']['fields']);
$display_options['default']['fields'][$field['id']] = $field;
}
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.
Noncompliant Code Example
$arr = array(1, 2, 3);
foreach ($arr as &$value) { // Noncompliant; $value is still alive after the loop and references the last item of the array: $arr[2]
$value = $value * 2;
}
$value = 'x';
Compliant Solution
$arr = array(1, 2, 3);
foreach ($arr as &$value) { // Compliant; there is no risk to use by mistake the content of $value pointing to $arr[2]
$value = $value * 2;
}
unset($value);
$value = 'x';
| Comment | File | Size | Author |
|---|---|---|---|
| #7 | views-n3376114-7.patch | 8.79 KB | damienmckenna |
| #7 | views-n3376114-7.interdiff.txt | 8.42 KB | damienmckenna |
Issue fork views-3376114
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #3
carlitus commentedComment #4
damienmckennaThat looks like a useful tiny improvement, thank you for putting it together.
There's a tiny issue with the change - the indenting is incorrect, please re-save the change using two spaces for each indent so that it lines up correctly? Thank you.
Comment #5
carlitus commentedSorry for the wrong indenting, i think was a nano thing. Now i've made it with phpstorm and it seems right.
Comment #6
carlitus commentedComment #7
damienmckennaLet's extend this to fix similar issues in other parts of the codebase:
Comment #8
damienmckenna@Carlitus: can you please review the new patch and let me know what you think? Thank you.
Comment #9
carlitus commentedHi,
I have checked it and I see everything is fine
The only thing is that I have seen some unused instances in the foreach keys, but maybe this is another minor issue.
comment.views.inc
line 708
theme.inc
Comment #10
damienmckennaI think it'd be better to do that cleanup separately.
Comment #12
damienmckennaCommitted.
Comment #13
damienmckenna