From 1fb8219cf53b18bd7dcd1a61b2deee0b989b3c6f Mon Sep 17 00:00:00 2001 From: Jeff Hubbard Date: Fri, 5 Sep 2014 11:18:15 -0700 Subject: [PATCH] Don't try to array_combine two empty arrays --- lib/Analytics/Client.php | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/Analytics/Client.php b/lib/Analytics/Client.php index cc19234..68c68b9 100644 --- a/lib/Analytics/Client.php +++ b/lib/Analytics/Client.php @@ -87,7 +87,8 @@ public function track($user_id, $event, $properties = null, ); }, $this->consumer); - return array_combine($this->getConsumerNames(), $returnValue); + $consumerNames = $this->getConsumerNames(); + return $this->possiblyEmptyResults($consumerNames, $returnValue); } /** @@ -119,7 +120,8 @@ public function identify($user_id, $traits = array(), $timestamp = null, }, $this->consumer); - return array_combine($this->getConsumerNames(), $returnValue); + $consumerNames = $this->getConsumerNames(); + return $this->possiblyEmptyResults($consumerNames, $returnValue); } public function getConsumerNames() @@ -155,7 +157,8 @@ public function alias($from, $to, $timestamp = null, $context = array()) { ); }, $this->consumer); - return array_combine($this->getConsumerNames(), $returnValue); + $consumerNames = $this->getConsumerNames(); + return $this->possiblyEmptyResults($consumerNames, $returnValue); } /** @@ -171,6 +174,18 @@ private function formatTime($timestamp) { return date("c", $timestamp); } + /** + * Return either an empty array or the combined result of key => value mappings of each of the arrays passed in + * This is necessary because `array_combine` causes a PHP fatal if both arrays are empty. + * + * @param $consumers The list of consumer names + * @param $results The result of calling each consumer with the event data + * @return array Either an empty array or the map of consumers to their results + */ + private function possiblyEmptyResults($consumers, $results) + { + return empty($consumers) && empty($results) ? array() : array_combine($consumers, $results); + } /** * Add the segment.io context to the request