From 299938a87c0fd312514817f1711afe283dac30e7 Mon Sep 17 00:00:00 2001 From: John Koster Date: Sat, 10 Apr 2021 17:27:00 -0500 Subject: [PATCH] Protects against null root node, and ensures instance of Array_ --- src/Analyzers/ConfigAnalyzer.php | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/Analyzers/ConfigAnalyzer.php b/src/Analyzers/ConfigAnalyzer.php index bb191ad..4cb7fe3 100644 --- a/src/Analyzers/ConfigAnalyzer.php +++ b/src/Analyzers/ConfigAnalyzer.php @@ -472,7 +472,6 @@ public function removeNode($key) if ($currentNode instanceof ArrayItem) { $parent = $this->getParentKey($key); - if (mb_strlen($parent) > 0 && $this->hasNode($parent)) { $parentNode = $this->nodeMapping[$parent]; $relativeKey = $this->getLastKeySegment($key); @@ -495,21 +494,23 @@ public function removeNode($key) $parentNode->value->items = $newItems; } } else { - $newItems = []; + if ($this->rootNode != null && $this->rootNode instanceof Array_) { + $newItems = []; - /** @var ArrayItem $valueItem */ - foreach ($this->rootNode->items as $valueItem) { - $valueItemKey = $valueItem->key->value; + /** @var ArrayItem $valueItem */ + foreach ($this->rootNode->items as $valueItem) { + $valueItemKey = $valueItem->key->value; - if ($valueItemKey === null || $valueItemKey !== $key) { - $newItems[] = $valueItem; - } else { - $foundNode = true; + if ($valueItemKey === null || $valueItemKey !== $key) { + $newItems[] = $valueItem; + } else { + $foundNode = true; + } } - } - // Reassign the value items, without the node to remove. - $this->rootNode->items = $newItems; + // Reassign the value items, without the node to remove. + $this->rootNode->items = $newItems; + } } }