From 3b6066c77aeaefd59ea7f1b356f8f99ca6f6df70 Mon Sep 17 00:00:00 2001
From: diosmosis <benaka@piwik.pro>
Date: Mon, 7 Sep 2015 15:20:09 -0700
Subject: [PATCH] Check for valid data in RowEvolution API to avoid unexpected
 fatal errors.

---
 plugins/API/RowEvolution.php | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/plugins/API/RowEvolution.php b/plugins/API/RowEvolution.php
index 6e31a13317..c708608089 100644
--- a/plugins/API/RowEvolution.php
+++ b/plugins/API/RowEvolution.php
@@ -360,9 +360,16 @@ class RowEvolution
         unset($metadata['logos']);
 
         $subDataTables = $dataTable->getDataTables();
+        if (empty($subDataTables)) {
+            throw new \Exception("Unexpected state: row evolution API call returned empty DataTable\\Map.");
+        }
+
         $firstDataTable = reset($subDataTables);
+        $this->checkDataTableInstance($firstDataTable);
         $firstDataTableRow = $firstDataTable->getFirstRow();
+
         $lastDataTable = end($subDataTables);
+        $this->checkDataTableInstance($lastDataTable);
         $lastDataTableRow = $lastDataTable->getFirstRow();
 
         // Process min/max values
@@ -533,4 +540,11 @@ class RowEvolution
         $label = SafeDecodeLabel::decodeLabelSafe($label);
         return $label;
     }
+
+    private function checkDataTableInstance($lastDataTable)
+    {
+        if (!($lastDataTable instanceof DataTable)) {
+            throw new \Exception("Unexpected state: row evolution returned DataTable\\Map w/ incorrect child table type: " . get_class($lastDataTable));
+        }
+    }
 }
-- 
GitLab