ConfigInspectorManager.php, convertConfigElementToList()

This set of nested for loop accidently works, despite the fact that $element is used in both loops
This is brittle code and will likely break, if there is any attempt to extend it..

  public function convertConfigElementToList($schema) {
    $list = array();
    foreach ($schema as $key => $element) {
      if ($element instanceof Element) {
        $list[$key] = $element;
        foreach ($this->convertConfigElementToList($element) as $sub_key => $element) {
          $list[$key . '.' . $sub_key] = $element;
        }
      }
      else {
        $list[$key] = $element;
      }
    }
    return $list;
  }

It show up in a lint checker, under probably a bug as :-

The 'element' variable is used as a value by a foreach loop is already used in the same way by the outer foreach loop.

So the solution is cosmetic, that is why I have not graded it as a bug...it will only save the sanity of the next coder to ready this!

CommentFileSizeAuthor
element-0.patch693 bytesmartin107

Comments

vijaycs85’s picture

Status: Needs review » Reviewed & tested by the community
+++ b/src/ConfigInspectorManager.php
@@ -124,8 +124,8 @@ class ConfigInspectorManager extends DefaultPluginManager {
-        foreach ($this->convertConfigElementToList($element) as $sub_key => $element) {
...
+        foreach ($this->convertConfigElementToList($element) as $sub_key => $value) {

good catch!

gábor hojtsy’s picture

Status: Reviewed & tested by the community » Fixed

Superb, thanks for finding this :)

  • Gábor Hojtsy committed 534c130 on 8.x-1.x
    Issue #2356907 by martin107: When nesting foreach loops cannot reuse any...

Status: Fixed » Closed (fixed)

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