Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
See meta #2002650: [meta, no patch] improve maintainability by removing unused local variables
core/includes/common.inc
- Unused local variable $options (line 4254)
Comment | File | Size | Author |
---|---|---|---|
#17 | 2002708-17-remove_unused_variables_common_inc.patch | 496 bytes | rahul.shinde |
#8 | 2002708_unused-local-var_8.patch | 525 bytes | somepal |
#5 | 2002708-5-remove_unused_variables_common_inc.patch | 525 bytes | kerasai |
#3 | core-remove-variable-2002708.patch | 525 bytes | arknoll |
Comments
Comment #1
arknoll CreditAttribution: arknoll commentedWorking on this.
Comment #2
longwave@arknoll: You might want to use the "assigned" field to indicate this.
Comment #3
arknoll CreditAttribution: arknoll commentedRemoved variable.
Comment #5
kerasai CreditAttribution: kerasai commentedLooks like patch in #3 was created backwards, as it was trying to add the line instead of remove it. Let's give this a shot.
Comment #6
aspilicious CreditAttribution: aspilicious commentedGood to go
Comment #7
catchThis looks like a bug to me rather than just an unused variable - isn't it trying to ensure that $options is an array in the return value?
So it should write options back to the array (or act on a reference in the foreach()) rather than just discarding.
Comment #8
somepal CreditAttribution: somepal commentedif (is_scalar($options))
checks if its scalar and intends to make
$options
available as array (for the next foreach pass maybe) if its the one. But initializing in this manner at the end of the foreach() pass, wouldn't serve that purpose. its not used further down the line as well. hence removed in the patch.Comment #9
kerasai CreditAttribution: kerasai commented@catch Not sure how much sense it makes to set $options to an empty array just to assign it a value on the next operation as it iterates through the foreach. On last pass $options remain set, but not used anywhere in the remainder of the function. Sure seems unused to me.
#5 kept testbot happy and #8 is identical, I think this is RTBC. Since I've contributed code on this one, I'll let you all hash it out.
Comment #10
longwaveInstead of
do we need
To ensure the original $file key is unset by the end of the foreach?
Comment #11
somepal CreditAttribution: somepal commented@longwave sorry could not agree that it was an unset attempt and I think we wouldn't need
unset($module_libraries[$key]['js'][$file]);
there, as
$module_libraries[$key]['js'][$file]['version']
is referred again as -
$module_libraries[$key]['js'][$file]['version'] = $module_libraries[$key]['version'];
At the same time, feels that it is not an attempt to initialize the variable to an array, as it wouldn't work that way, foreach() only works on the copy of the array unless passed as reference.
So the conclusion we should delete the line
$options = array();
safely and got ahead with patch #5 2002708-5-remove_unused_variables_common_inc.patch or #8 2002708_unused-local-var_8.patch
Comment #12
somepal CreditAttribution: somepal commentedComment #13
longwaveThe comment above this block says "The JavaScript or CSS file has been specified in shorthand format, without an array of options. In this case $options is the filename". Firstly this is wrong because the loop explicitly looks at JavaScript only, not CSS, and secondly this "shorthand format" does not appear to be documented anywhere. The source of data for this loop is hook_library_info(), and the documentation there for the 'js' key says "An array of JavaScript elements; each element's key is used as $data argument, each element's value is used as $options array for drupal_add_js()". So, perhaps we should remove this check for the "shorthand format" entirely?
Comment #13.0
longwaveUpdated issue summary.
Comment #14
jibran8: 2002708_unused-local-var_8.patch queued for re-testing.
Comment #16
rahul.shindeComment #17
rahul.shindeTried re-rolling patch but failed. So created altogether new patch and submitting.
Comment #18
xjmThe performance impact of this is negligible, so retitling.
Comment #19
elgordogrande CreditAttribution: elgordogrande commentedThe $options variable may have been used previously in this scope, but appears to no longer be the case, so it is safe to remove.
Comment #20
webchickCommitted and pushed to 8.x. Thanks!