Support from Acquia helps fund testing for Drupal Acquia logo

Comments

heykarthikwithu created an issue. See original summary.

heykarthikwithu’s picture

Status: Active » Needs review
FileSize
573 bytes
heykarthikwithu’s picture

Priority: Normal » Minor
heykarthikwithu’s picture

Issue tags: +rc eligible
rakesh.gectcr’s picture

rakesh.gectcr’s picture

Studiographene’s picture

rakesh.gectcr’s picture

@Studiographene

I am not finding any difference between the two patches. Both seems to be equal. See the attached interdiff file

Studiographene’s picture

HI #rakesh.gectcr,

According to the Bug i just remove the $key variable from the file file.field.inc ,"Unused local variable $key (at line 70), 'file' module, 'file.field.inc' file."

What else we suppose to do to fixed this Bug,

Thanks
Studiographene

heykarthikwithu’s picture

royal121’s picture

The patch applies correctly. Reviewed.

longwave’s picture

Status: Needs review » Reviewed & tested by the community
alexpott’s picture

Status: Reviewed & tested by the community » Needs review

I'm not sure that this issue meets the guidelines for issue scope. There are plenty of other instances of foreach ($something as $key => $value) { where $key is unused. See https://www.drupal.org/core/scope for guidelines and examples for Drupal core issue scope.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Kumar Kundan’s picture

It works fine.

Kumar Kundan’s picture

@alexpott: i go through this link, which very nice. but when we see title of this issue it can be meets the guidelines for issue scope. this is also true that , There are plenty of other instances of foreach ($something as $key => $value) { where $key is unused.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

paulocs’s picture

Hello @alexpott,

As you said in comment #54 from issue #2002650: [meta, no patch] improve maintainability by removing unused local variables, we should look each unused variable on its own merit. So is this issue in the scope?

If yes, here is a new patch for 8.9.x-dev. The $key variable is really not used in the foreach at line 49.

The $key variable was unused when issue #853926: Wrong order of multi-value file fields when form rebuilt (preview, failed validation) was fixed.

Cheers,
Paulo.

paulocs’s picture

Title: Unused local variable $key (at line 70), 'file' module, 'file.field.inc' file. » Unused local variable $key (at line 50), 'file' module, 'file.field.inc' file.
Issue summary: View changes
paulocs’s picture

Title: Unused local variable $key (at line 50), 'file' module, 'file.field.inc' file. » Unused local variable $key (at line 49), 'file' module, 'file.field.inc' file.
Issue summary: View changes
longwave’s picture

Status: Needs review » Reviewed & tested by the community

Back to RTBC on this one, now we are considering them case by case this seems ok.

alexpott’s picture

Status: Reviewed & tested by the community » Needs review

We've avoided doing foreach key fixes in the other issues and I don't think they should be done on their own we can do these as part of the automated fix in #3106216: Remove unused variables from core there's an argument that these variables are not unused - PHPStorm, for example, does not mark them as unused. I think this is the exception to the rule that each one deserves it's own scope because it's not the same as an issue where we have to work why something is longer used.

andypost’s picture

Version: 8.9.x-dev » 9.2.x-dev
Abhijith S’s picture

FileSize
39.58 KB

Applied patch #24 on 9.2.x and it removes the unwanted $key variable from it.

after

RTBC +1

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

quietone’s picture

Title: Unused local variable $key (at line 49), 'file' module, 'file.field.inc' file. » Unused local variable $key (at line 49) in file.field.inc

@Abhijith S, Thank you for your interest in this issue. Adding screenshots of a file with the patch applied does not advance the issue, removing credit. The contributor guide on Drupal.org has information about how to contribute, particularly the process to Review a patch or merge request. There are also guidelines for How is credit granted for Drupal core issues..

Simplified the title, removed quotes.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

borisson_’s picture

I have no idea why this is not found with the rule added in #3106216: Remove unused variables from core. I think it probably has something to do with the fact that there are several $key variables in the function but changing those to another variable doesn't seem to change the outcome here.
I'm also not sure if phpcs + that rules is the right tool to fix this issue, I think phpstan would make more sense. In any case, As a part of #2571965: [meta] Fix PHP coding standards in core, I think we should find something that actually finds this unused variable?

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Needs Review Queue Initiative

Running for 10.1 but don't expect a failure.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

If we're going to remove this I think we should take the opportunity to make the code inside more consistent. In the loop we have

    foreach (Element::children($widget) as $sub_key) {
      if (isset($widget[$sub_key]['#type']) && $widget[$sub_key]['#type'] == 'submit') {
        hide($widget[$sub_key]);
        $operations_elements[] = &$widget[$sub_key];
      }
    }

and

    foreach (Element::children($operations_elements) as $key) {
      show($operations_elements[$key]);
    }

I think now we're removing the $key from the containing loop we should be changing the first internal Element::children() to use $key instead of $sub_key because now sub_ is pretty meaningless.

quietone’s picture

Rishabh Vishwakarma’s picture

Status: Needs work » Needs review
FileSize
1.74 KB
1.2 KB

Addressed the points mentioned in #38

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

foreach (Element::children($operations_elements) as $sub_key) {

Will let the committer decide this one but not sure if this should be sub_key but since we used $key in the loop above it maybe it's fine.

borisson_’s picture

Status: Reviewed & tested by the community » Needs work

No, making this sub key is super confusing, back to needs work imho.

Rassoni’s picture

Status: Needs work » Needs review
FileSize
1.5 KB
2.46 KB

Addressed the points mentioned in #38 in Patch #40, Currently, Parent loops is not using $key So used $key instead of $sub_key and that loop ended in line no 65. IMO line number 91 changes not required as it starts with different loop. Please review.

Rassoni’s picture

FileSize
1.39 KB

Sorry wrong interdiff.

aziza_a’s picture

FileSize
823.02 KB

Checked the patch given on #43 , it apply's cleanly and have also addressed the previous comments

+RTBC

borisson_’s picture

Status: Needs review » Reviewed & tested by the community

@aziza_a, adding a screenshot that code applies is not helpful, the bots tell us that. Please don't do that.

The patch in #43 has made the changes I requested in #42, looks good to me now.

  • catch committed 80b3daf9 on 10.1.x
    Issue #2602326 by Rassoni, Rishabh Vishwakarma, heykarthikwithu, paulocs...
catch’s picture

Status: Reviewed & tested by the community » Fixed

In general I don't think we should treat array keys in foreach loops as unused but in this case it's improving readability overall so seems like a good change. Committed/pushed to 10.1.x, thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.