Skip to content
This repository has been archived by the owner on Oct 29, 2021. It is now read-only.

Commit

Permalink
Merge pull request #7 from lord2800/empty-consumers-list
Browse files Browse the repository at this point in the history
Don't try to array_combine two empty arrays
  • Loading branch information
PascalZajac committed Sep 11, 2014
2 parents 1f74342 + 1fb8219 commit eb58549
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions lib/Analytics/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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
Expand Down

0 comments on commit eb58549

Please sign in to comment.