diff --git a/src/Storage/Adapter/AbstractAdapter.php b/src/Storage/Adapter/AbstractAdapter.php index 449f09dc7..d237b6a11 100644 --- a/src/Storage/Adapter/AbstractAdapter.php +++ b/src/Storage/Adapter/AbstractAdapter.php @@ -9,7 +9,6 @@ namespace Zend\Cache\Storage\Adapter; -use ArrayObject; use SplObjectStorage; use stdClass; use Traversable; @@ -124,7 +123,7 @@ public function setOptions($options) $options->setAdapter($this); $this->options = $options; - $event = new Event('option', $this, new ArrayObject($options->toArray())); + $event = new Event('option', $this, $options->toArray()); $this->getEventManager()->triggerEvent($event); } @@ -199,25 +198,26 @@ public function getEventManager() * Trigger a pre event and return the event response collection * * @param string $eventName - * @param ArrayObject $args + * @param array $params * @return \Zend\EventManager\ResponseCollection All handler return values - */ - protected function triggerPre($eventName, ArrayObject $args) + * + protected function triggerPre($eventName, array $params) { - return $this->getEventManager()->triggerEvent(new Event($eventName . '.pre', $this, $args)); + return $this->getEventManager()->triggerEvent(new Event($eventName . '.pre', $this, $params)); } + */ /** * Triggers the PostEvent and return the result value. * - * @param string $eventName - * @param ArrayObject $args - * @param mixed $result + * @param string $eventName + * @param array $params + * @param mixed $result * @return mixed */ - protected function triggerPost($eventName, ArrayObject $args, & $result) + protected function triggerPost($eventName, array $params, $result) { - $postEvent = new PostEvent($eventName . '.post', $this, $args, $result); + $postEvent = new PostEvent($eventName . '.post', $this, $params, $result); $eventRs = $this->getEventManager()->triggerEvent($postEvent); return $eventRs->stopped() @@ -231,16 +231,16 @@ protected function triggerPost($eventName, ArrayObject $args, & $result) * If the ExceptionEvent has the flag "throwException" enabled throw the * exception after trigger else return the result. * - * @param string $eventName - * @param ArrayObject $args - * @param mixed $result - * @param \Exception $exception + * @param string $eventName + * @param array $params + * @param mixed $result + * @param \Exception $exception * @throws Exception\ExceptionInterface * @return mixed */ - protected function triggerException($eventName, ArrayObject $args, & $result, \Exception $exception) + protected function triggerException($eventName, array $params, $result, \Exception $exception) { - $exceptionEvent = new ExceptionEvent($eventName . '.exception', $this, $args, $result, $exception); + $exceptionEvent = new ExceptionEvent($eventName . '.exception', $this, $params, $result, $exception); $eventRs = $this->getEventManager()->triggerEvent($exceptionEvent); if ($exceptionEvent->getThrowException()) { @@ -340,38 +340,39 @@ public function getItem($key, & $success = null, & $casToken = null) return; } - $this->normalizeKey($key); + $key = $this->normalizeKey($key); - $argn = func_num_args(); - $args = [ - 'key' => & $key, + $argn = func_num_args(); + $params = [ + 'key' => $key, ]; if ($argn > 1) { - $args['success'] = & $success; + $params['success'] = & $success; } if ($argn > 2) { - $args['casToken'] = & $casToken; + $params['casToken'] = & $casToken; } - $args = new ArrayObject($args); try { - $eventRs = $this->triggerPre(__FUNCTION__, $args); + $event = new Event('getItem.pre', $this, $params); + $eventRs = $this->getEventManager()->triggerEvent($event); + $params = $event->getParams(); if ($eventRs->stopped()) { $result = $eventRs->last(); - } elseif ($args->offsetExists('success') && $args->offsetExists('casToken')) { - $result = $this->internalGetItem($args['key'], $args['success'], $args['casToken']); - } elseif ($args->offsetExists('success')) { - $result = $this->internalGetItem($args['key'], $args['success']); + } elseif (array_key_exists('success', $params) && array_key_exists('casToken', $params)) { + $result = $this->internalGetItem($params['key'], $params['success'], $params['casToken']); + } elseif (array_key_exists('success', $params)) { + $result = $this->internalGetItem($params['key'], $params['success']); } else { - $result = $this->internalGetItem($args['key']); + $result = $this->internalGetItem($params['key']); } - return $this->triggerPost(__FUNCTION__, $args, $result); + return $this->triggerPost(__FUNCTION__, $params, $result); } catch (\Exception $e) { - $result = null; + $result = null; $success = false; - return $this->triggerException(__FUNCTION__, $args, $result, $e); + return $this->triggerException(__FUNCTION__, $params, $result, $e); } } @@ -384,7 +385,7 @@ public function getItem($key, & $success = null, & $casToken = null) * @return mixed Data on success, null on failure * @throws Exception\ExceptionInterface */ - abstract protected function internalGetItem(& $normalizedKey, & $success = null, & $casToken = null); + abstract protected function internalGetItem($normalizedKey, & $success = null, & $casToken = null); /** * Get multiple items. @@ -403,22 +404,22 @@ public function getItems(array $keys) return []; } - $this->normalizeKeys($keys); - $args = new ArrayObject([ - 'keys' => & $keys, - ]); + $keys = $this->normalizeKeys($keys); + $params = ['keys' => $keys]; try { - $eventRs = $this->triggerPre(__FUNCTION__, $args); + $event = new Event('getItems.pre', $this, $params); + $eventRs = $this->getEventManager()->triggerEvent($event); + $params = $event->getParams(); $result = $eventRs->stopped() ? $eventRs->last() - : $this->internalGetItems($args['keys']); + : $this->internalGetItems($params['keys']); - return $this->triggerPost(__FUNCTION__, $args, $result); + return $this->triggerPost(__FUNCTION__, $params, $result); } catch (\Exception $e) { $result = []; - return $this->triggerException(__FUNCTION__, $args, $result, $e); + return $this->triggerException(__FUNCTION__, $params, $result, $e); } } @@ -429,7 +430,7 @@ public function getItems(array $keys) * @return array Associative array of keys and values * @throws Exception\ExceptionInterface */ - protected function internalGetItems(array & $normalizedKeys) + protected function internalGetItems(array $normalizedKeys) { $success = null; $result = []; @@ -460,22 +461,22 @@ public function hasItem($key) return false; } - $this->normalizeKey($key); - $args = new ArrayObject([ - 'key' => & $key, - ]); + $key = $this->normalizeKey($key); + $params = ['key' => $key]; try { - $eventRs = $this->triggerPre(__FUNCTION__, $args); + $event = new Event('hasItem.pre', $this, $params); + $eventRs = $this->getEventManager()->triggerEvent($event); + $params = $event->getParams(); $result = $eventRs->stopped() ? $eventRs->last() - : $this->internalHasItem($args['key']); + : $this->internalHasItem($params['key']); - return $this->triggerPost(__FUNCTION__, $args, $result); + return $this->triggerPost(__FUNCTION__, $params, $result); } catch (\Exception $e) { $result = false; - return $this->triggerException(__FUNCTION__, $args, $result, $e); + return $this->triggerException(__FUNCTION__, $params, $result, $e); } } @@ -486,7 +487,7 @@ public function hasItem($key) * @return bool * @throws Exception\ExceptionInterface */ - protected function internalHasItem(& $normalizedKey) + protected function internalHasItem($normalizedKey) { $success = null; $this->internalGetItem($normalizedKey, $success); @@ -510,22 +511,22 @@ public function hasItems(array $keys) return []; } - $this->normalizeKeys($keys); - $args = new ArrayObject([ - 'keys' => & $keys, - ]); + $keys = $this->normalizeKeys($keys); + $params = ['keys' => $keys]; try { - $eventRs = $this->triggerPre(__FUNCTION__, $args); + $event = new Event('hasItems.pre', $this, $params); + $eventRs = $this->getEventManager()->triggerEvent($event); + $params = $event->getParams(); $result = $eventRs->stopped() ? $eventRs->last() - : $this->internalHasItems($args['keys']); + : $this->internalHasItems($params['keys']); - return $this->triggerPost(__FUNCTION__, $args, $result); + return $this->triggerPost(__FUNCTION__, $params, $result); } catch (\Exception $e) { $result = []; - return $this->triggerException(__FUNCTION__, $args, $result, $e); + return $this->triggerException(__FUNCTION__, $params, $result, $e); } } @@ -536,7 +537,7 @@ public function hasItems(array $keys) * @return array Array of found keys * @throws Exception\ExceptionInterface */ - protected function internalHasItems(array & $normalizedKeys) + protected function internalHasItems(array $normalizedKeys) { $result = []; foreach ($normalizedKeys as $normalizedKey) { @@ -564,22 +565,22 @@ public function getMetadata($key) return false; } - $this->normalizeKey($key); - $args = new ArrayObject([ - 'key' => & $key, - ]); + $key = $this->normalizeKey($key); + $params = ['key' => $key]; try { - $eventRs = $this->triggerPre(__FUNCTION__, $args); + $event = new Event('getMetadata.pre', $this, $params); + $eventRs = $this->getEventManager()->triggerEvent($event); + $params = $event->getParams(); $result = $eventRs->stopped() ? $eventRs->last() - : $this->internalGetMetadata($args['key']); + : $this->internalGetMetadata($params['key']); - return $this->triggerPost(__FUNCTION__, $args, $result); + return $this->triggerPost(__FUNCTION__, $params, $result); } catch (\Exception $e) { $result = false; - return $this->triggerException(__FUNCTION__, $args, $result, $e); + return $this->triggerException(__FUNCTION__, $params, $result, $e); } } @@ -590,7 +591,7 @@ public function getMetadata($key) * @return array|bool Metadata on success, false on failure * @throws Exception\ExceptionInterface */ - protected function internalGetMetadata(& $normalizedKey) + protected function internalGetMetadata($normalizedKey) { if (!$this->internalHasItem($normalizedKey)) { return false; @@ -616,22 +617,22 @@ public function getMetadatas(array $keys) return []; } - $this->normalizeKeys($keys); - $args = new ArrayObject([ - 'keys' => & $keys, - ]); + $keys = $this->normalizeKeys($keys); + $params = ['keys' => $keys]; try { - $eventRs = $this->triggerPre(__FUNCTION__, $args); + $event = new Event('getMetadatas.pre', $this, $params); + $eventRs = $this->getEventManager()->triggerEvent($event); + $params = $event->getParams(); $result = $eventRs->stopped() ? $eventRs->last() - : $this->internalGetMetadatas($args['keys']); + : $this->internalGetMetadatas($params['keys']); - return $this->triggerPost(__FUNCTION__, $args, $result); + return $this->triggerPost(__FUNCTION__, $params, $result); } catch (\Exception $e) { $result = []; - return $this->triggerException(__FUNCTION__, $args, $result, $e); + return $this->triggerException(__FUNCTION__, $params, $result, $e); } } @@ -642,7 +643,7 @@ public function getMetadatas(array $keys) * @return array Associative array of keys and metadata * @throws Exception\ExceptionInterface */ - protected function internalGetMetadatas(array & $normalizedKeys) + protected function internalGetMetadatas(array $normalizedKeys) { $result = []; foreach ($normalizedKeys as $normalizedKey) { @@ -674,23 +675,25 @@ public function setItem($key, $value) return false; } - $this->normalizeKey($key); - $args = new ArrayObject([ - 'key' => & $key, - 'value' => & $value, - ]); + $key = $this->normalizeKey($key); + $params = [ + 'key' => $key, + 'value' => $value, + ]; try { - $eventRs = $this->triggerPre(__FUNCTION__, $args); + $event = new Event('setItem.pre', $this, $params); + $eventRs = $this->getEventManager()->triggerEvent($event); + $params = $event->getParams(); $result = $eventRs->stopped() ? $eventRs->last() - : $this->internalSetItem($args['key'], $args['value']); + : $this->internalSetItem($params['key'], $params['value']); - return $this->triggerPost(__FUNCTION__, $args, $result); + return $this->triggerPost(__FUNCTION__, $params, $result); } catch (\Exception $e) { $result = false; - return $this->triggerException(__FUNCTION__, $args, $result, $e); + return $this->triggerException(__FUNCTION__, $params, $result, $e); } } @@ -702,7 +705,7 @@ public function setItem($key, $value) * @return bool * @throws Exception\ExceptionInterface */ - abstract protected function internalSetItem(& $normalizedKey, & $value); + abstract protected function internalSetItem($normalizedKey, $value); /** * Store multiple items. @@ -721,22 +724,24 @@ public function setItems(array $keyValuePairs) return array_keys($keyValuePairs); } - $this->normalizeKeyValuePairs($keyValuePairs); - $args = new ArrayObject([ - 'keyValuePairs' => & $keyValuePairs, - ]); + $keyValuePairs = $this->normalizeKeyValuePairs($keyValuePairs); + $params = [ + 'keyValuePairs' => $keyValuePairs, + ]; try { - $eventRs = $this->triggerPre(__FUNCTION__, $args); + $event = new Event('setItems.pre', $this, $params); + $eventRs = $this->getEventManager()->triggerEvent($event); + $params = $event->getParams(); $result = $eventRs->stopped() ? $eventRs->last() - : $this->internalSetItems($args['keyValuePairs']); + : $this->internalSetItems($params['keyValuePairs']); - return $this->triggerPost(__FUNCTION__, $args, $result); + return $this->triggerPost(__FUNCTION__, $params, $result); } catch (\Exception $e) { $result = array_keys($keyValuePairs); - return $this->triggerException(__FUNCTION__, $args, $result, $e); + return $this->triggerException(__FUNCTION__, $params, $result, $e); } } @@ -747,7 +752,7 @@ public function setItems(array $keyValuePairs) * @return array Array of not stored keys * @throws Exception\ExceptionInterface */ - protected function internalSetItems(array & $normalizedKeyValuePairs) + protected function internalSetItems(array $normalizedKeyValuePairs) { $failedKeys = []; foreach ($normalizedKeyValuePairs as $normalizedKey => $value) { @@ -776,23 +781,25 @@ public function addItem($key, $value) return false; } - $this->normalizeKey($key); - $args = new ArrayObject([ - 'key' => & $key, - 'value' => & $value, - ]); + $key = $this->normalizeKey($key); + $params = [ + 'key' => $key, + 'value' => $value, + ]; try { - $eventRs = $this->triggerPre(__FUNCTION__, $args); + $event = new Event('addItem.pre', $this, $params); + $eventRs = $this->getEventManager()->triggerEvent($event); + $params = $event->getParams(); $result = $eventRs->stopped() ? $eventRs->last() - : $this->internalAddItem($args['key'], $args['value']); + : $this->internalAddItem($params['key'], $params['value']); - return $this->triggerPost(__FUNCTION__, $args, $result); + return $this->triggerPost(__FUNCTION__, $params, $result); } catch (\Exception $e) { $result = false; - return $this->triggerException(__FUNCTION__, $args, $result, $e); + return $this->triggerException(__FUNCTION__, $params, $result, $e); } } @@ -804,7 +811,7 @@ public function addItem($key, $value) * @return bool * @throws Exception\ExceptionInterface */ - protected function internalAddItem(& $normalizedKey, & $value) + protected function internalAddItem($normalizedKey, $value) { if ($this->internalHasItem($normalizedKey)) { return false; @@ -829,22 +836,24 @@ public function addItems(array $keyValuePairs) return array_keys($keyValuePairs); } - $this->normalizeKeyValuePairs($keyValuePairs); - $args = new ArrayObject([ - 'keyValuePairs' => & $keyValuePairs, - ]); + $keyValuePairs = $this->normalizeKeyValuePairs($keyValuePairs); + $params = [ + 'keyValuePairs' => $keyValuePairs, + ]; try { - $eventRs = $this->triggerPre(__FUNCTION__, $args); + $event = new Event('addItems.pre', $this, $params); + $eventRs = $this->getEventManager()->triggerEvent($event); + $params = $event->getParams(); $result = $eventRs->stopped() ? $eventRs->last() - : $this->internalAddItems($args['keyValuePairs']); + : $this->internalAddItems($params['keyValuePairs']); - return $this->triggerPost(__FUNCTION__, $args, $result); + return $this->triggerPost(__FUNCTION__, $params, $result); } catch (\Exception $e) { $result = array_keys($keyValuePairs); - return $this->triggerException(__FUNCTION__, $args, $result, $e); + return $this->triggerException(__FUNCTION__, $params, $result, $e); } } @@ -855,7 +864,7 @@ public function addItems(array $keyValuePairs) * @return array Array of not stored keys * @throws Exception\ExceptionInterface */ - protected function internalAddItems(array & $normalizedKeyValuePairs) + protected function internalAddItems(array $normalizedKeyValuePairs) { $result = []; foreach ($normalizedKeyValuePairs as $normalizedKey => $value) { @@ -884,23 +893,25 @@ public function replaceItem($key, $value) return false; } - $this->normalizeKey($key); - $args = new ArrayObject([ - 'key' => & $key, - 'value' => & $value, - ]); + $key = $this->normalizeKey($key); + $params = [ + 'key' => $key, + 'value' => $value, + ]; try { - $eventRs = $this->triggerPre(__FUNCTION__, $args); + $event = new Event('replaceItem.pre', $this, $params); + $eventRs = $this->getEventManager()->triggerEvent($event); + $params = $event->getParams(); $result = $eventRs->stopped() ? $eventRs->last() - : $this->internalReplaceItem($args['key'], $args['value']); + : $this->internalReplaceItem($params['key'], $params['value']); - return $this->triggerPost(__FUNCTION__, $args, $result); + return $this->triggerPost(__FUNCTION__, $params, $result); } catch (\Exception $e) { $result = false; - return $this->triggerException(__FUNCTION__, $args, $result, $e); + return $this->triggerException(__FUNCTION__, $params, $result, $e); } } @@ -912,7 +923,7 @@ public function replaceItem($key, $value) * @return bool * @throws Exception\ExceptionInterface */ - protected function internalReplaceItem(& $normalizedKey, & $value) + protected function internalReplaceItem($normalizedKey, $value) { if (!$this->internalhasItem($normalizedKey)) { return false; @@ -938,22 +949,24 @@ public function replaceItems(array $keyValuePairs) return array_keys($keyValuePairs); } - $this->normalizeKeyValuePairs($keyValuePairs); - $args = new ArrayObject([ - 'keyValuePairs' => & $keyValuePairs, - ]); + $keyValuePairs = $this->normalizeKeyValuePairs($keyValuePairs); + $params = [ + 'keyValuePairs' => $keyValuePairs, + ]; try { - $eventRs = $this->triggerPre(__FUNCTION__, $args); + $event = new Event('replaceItems.pre', $this, $params); + $eventRs = $this->getEventManager()->triggerEvent($event); + $params = $event->getParams(); $result = $eventRs->stopped() ? $eventRs->last() - : $this->internalReplaceItems($args['keyValuePairs']); + : $this->internalReplaceItems($params['keyValuePairs']); - return $this->triggerPost(__FUNCTION__, $args, $result); + return $this->triggerPost(__FUNCTION__, $params, $result); } catch (\Exception $e) { $result = array_keys($keyValuePairs); - return $this->triggerException(__FUNCTION__, $args, $result, $e); + return $this->triggerException(__FUNCTION__, $params, $result, $e); } } @@ -964,7 +977,7 @@ public function replaceItems(array $keyValuePairs) * @return array Array of not stored keys * @throws Exception\ExceptionInterface */ - protected function internalReplaceItems(array & $normalizedKeyValuePairs) + protected function internalReplaceItems(array $normalizedKeyValuePairs) { $result = []; foreach ($normalizedKeyValuePairs as $normalizedKey => $value) { @@ -995,24 +1008,26 @@ public function checkAndSetItem($token, $key, $value) return false; } - $this->normalizeKey($key); - $args = new ArrayObject([ - 'token' => & $token, - 'key' => & $key, - 'value' => & $value, - ]); + $key = $this->normalizeKey($key); + $params = [ + 'token' => $token, + 'key' => $key, + 'value' => $value, + ]; try { - $eventRs = $this->triggerPre(__FUNCTION__, $args); + $event = new Event('checkAndSetItem.pre', $this, $params); + $eventRs = $this->getEventManager()->triggerEvent($event); + $params = $event->getParams(); $result = $eventRs->stopped() ? $eventRs->last() - : $this->internalCheckAndSetItem($args['token'], $args['key'], $args['value']); + : $this->internalCheckAndSetItem($params['token'], $params['key'], $params['value']); - return $this->triggerPost(__FUNCTION__, $args, $result); + return $this->triggerPost(__FUNCTION__, $params, $result); } catch (\Exception $e) { $result = false; - return $this->triggerException(__FUNCTION__, $args, $result, $e); + return $this->triggerException(__FUNCTION__, $params, $result, $e); } } @@ -1027,7 +1042,7 @@ public function checkAndSetItem($token, $key, $value) * @see getItem() * @see setItem() */ - protected function internalCheckAndSetItem(& $token, & $normalizedKey, & $value) + protected function internalCheckAndSetItem($token, $normalizedKey, $value) { $oldValue = $this->internalGetItem($normalizedKey); if ($oldValue !== $token) { @@ -1054,22 +1069,22 @@ public function touchItem($key) return false; } - $this->normalizeKey($key); - $args = new ArrayObject([ - 'key' => & $key, - ]); + $key = $this->normalizeKey($key); + $params = ['key' => $key]; try { - $eventRs = $this->triggerPre(__FUNCTION__, $args); + $event = new Event('touchItem.pre', $this, $params); + $eventRs = $this->getEventManager()->triggerEvent($event); + $params = $event->getParams(); $result = $eventRs->stopped() ? $eventRs->last() - : $this->internalTouchItem($args['key']); + : $this->internalTouchItem($params['key']); - return $this->triggerPost(__FUNCTION__, $args, $result); + return $this->triggerPost(__FUNCTION__, $params, $result); } catch (\Exception $e) { $result = false; - return $this->triggerException(__FUNCTION__, $args, $result, $e); + return $this->triggerException(__FUNCTION__, $params, $result, $e); } } @@ -1080,7 +1095,7 @@ public function touchItem($key) * @return bool * @throws Exception\ExceptionInterface */ - protected function internalTouchItem(& $normalizedKey) + protected function internalTouchItem($normalizedKey) { $success = null; $value = $this->internalGetItem($normalizedKey, $success); @@ -1108,21 +1123,21 @@ public function touchItems(array $keys) return $keys; } - $this->normalizeKeys($keys); - $args = new ArrayObject([ - 'keys' => & $keys, - ]); + $keys = $this->normalizeKeys($keys); + $params = ['keys' => $keys]; try { - $eventRs = $this->triggerPre(__FUNCTION__, $args); + $event = new Event('touchItems.pre', $this, $params); + $eventRs = $this->getEventManager()->triggerEvent($event); + $params = $event->getParams(); $result = $eventRs->stopped() ? $eventRs->last() - : $this->internalTouchItems($args['keys']); + : $this->internalTouchItems($params['keys']); - return $this->triggerPost(__FUNCTION__, $args, $result); + return $this->triggerPost(__FUNCTION__, $params, $result); } catch (\Exception $e) { - return $this->triggerException(__FUNCTION__, $args, $keys, $e); + return $this->triggerException(__FUNCTION__, $params, $keys, $e); } } @@ -1133,7 +1148,7 @@ public function touchItems(array $keys) * @return array Array of not updated keys * @throws Exception\ExceptionInterface */ - protected function internalTouchItems(array & $normalizedKeys) + protected function internalTouchItems(array $normalizedKeys) { $result = []; foreach ($normalizedKeys as $normalizedKey) { @@ -1161,22 +1176,22 @@ public function removeItem($key) return false; } - $this->normalizeKey($key); - $args = new ArrayObject([ - 'key' => & $key, - ]); + $key = $this->normalizeKey($key); + $params = ['key' => $key]; try { - $eventRs = $this->triggerPre(__FUNCTION__, $args); + $event = new Event('removeItem.pre', $this, $params); + $eventRs = $this->getEventManager()->triggerEvent($event); + $params = $event->getParams(); $result = $eventRs->stopped() ? $eventRs->last() - : $this->internalRemoveItem($args['key']); + : $this->internalRemoveItem($params['key']); - return $this->triggerPost(__FUNCTION__, $args, $result); + return $this->triggerPost(__FUNCTION__, $params, $result); } catch (\Exception $e) { $result = false; - return $this->triggerException(__FUNCTION__, $args, $result, $e); + return $this->triggerException(__FUNCTION__, $params, $result, $e); } } @@ -1187,7 +1202,7 @@ public function removeItem($key) * @return bool * @throws Exception\ExceptionInterface */ - abstract protected function internalRemoveItem(& $normalizedKey); + abstract protected function internalRemoveItem($normalizedKey); /** * Remove multiple items. @@ -1206,21 +1221,21 @@ public function removeItems(array $keys) return $keys; } - $this->normalizeKeys($keys); - $args = new ArrayObject([ - 'keys' => & $keys, - ]); + $keys = $this->normalizeKeys($keys); + $params = ['keys' => $keys]; try { - $eventRs = $this->triggerPre(__FUNCTION__, $args); + $event = new Event('removeItems.pre', $this, $params); + $eventRs = $this->getEventManager()->triggerEvent($event); + $params = $event->getParams(); $result = $eventRs->stopped() ? $eventRs->last() - : $this->internalRemoveItems($args['keys']); + : $this->internalRemoveItems($params['keys']); - return $this->triggerPost(__FUNCTION__, $args, $result); + return $this->triggerPost(__FUNCTION__, $params, $result); } catch (\Exception $e) { - return $this->triggerException(__FUNCTION__, $args, $keys, $e); + return $this->triggerException(__FUNCTION__, $params, $keys, $e); } } @@ -1231,7 +1246,7 @@ public function removeItems(array $keys) * @return array Array of not removed keys * @throws Exception\ExceptionInterface */ - protected function internalRemoveItems(array & $normalizedKeys) + protected function internalRemoveItems(array $normalizedKeys) { $result = []; foreach ($normalizedKeys as $normalizedKey) { @@ -1260,23 +1275,25 @@ public function incrementItem($key, $value) return false; } - $this->normalizeKey($key); - $args = new ArrayObject([ - 'key' => & $key, - 'value' => & $value, - ]); + $key = $this->normalizeKey($key); + $params = [ + 'key' => $key, + 'value' => $value, + ]; try { - $eventRs = $this->triggerPre(__FUNCTION__, $args); + $event = new Event('incrementItem.pre', $this, $params); + $eventRs = $this->getEventManager()->triggerEvent($event); + $params = $event->getParams(); $result = $eventRs->stopped() ? $eventRs->last() - : $this->internalIncrementItem($args['key'], $args['value']); + : $this->internalIncrementItem($params['key'], $params['value']); - return $this->triggerPost(__FUNCTION__, $args, $result); + return $this->triggerPost(__FUNCTION__, $params, $result); } catch (\Exception $e) { $result = false; - return $this->triggerException(__FUNCTION__, $args, $result, $e); + return $this->triggerException(__FUNCTION__, $params, $result, $e); } } @@ -1288,7 +1305,7 @@ public function incrementItem($key, $value) * @return int|bool The new value on success, false on failure * @throws Exception\ExceptionInterface */ - protected function internalIncrementItem(& $normalizedKey, & $value) + protected function internalIncrementItem($normalizedKey, $value) { $success = null; $value = (int) $value; @@ -1321,22 +1338,22 @@ public function incrementItems(array $keyValuePairs) return []; } - $this->normalizeKeyValuePairs($keyValuePairs); - $args = new ArrayObject([ - 'keyValuePairs' => & $keyValuePairs, - ]); + $keyValuePairs = $this->normalizeKeyValuePairs($keyValuePairs); + $params = ['keyValuePairs' => $keyValuePairs]; try { - $eventRs = $this->triggerPre(__FUNCTION__, $args); + $event = new Event('incrementItems.pre', $this, $params); + $eventRs = $this->getEventManager()->triggerEvent($event); + $params = $event->getParams(); $result = $eventRs->stopped() ? $eventRs->last() - : $this->internalIncrementItems($args['keyValuePairs']); + : $this->internalIncrementItems($params['keyValuePairs']); - return $this->triggerPost(__FUNCTION__, $args, $result); + return $this->triggerPost(__FUNCTION__, $params, $result); } catch (\Exception $e) { $result = []; - return $this->triggerException(__FUNCTION__, $args, $result, $e); + return $this->triggerException(__FUNCTION__, $params, $result, $e); } } @@ -1347,7 +1364,7 @@ public function incrementItems(array $keyValuePairs) * @return array Associative array of keys and new values * @throws Exception\ExceptionInterface */ - protected function internalIncrementItems(array & $normalizedKeyValuePairs) + protected function internalIncrementItems(array $normalizedKeyValuePairs) { $result = []; foreach ($normalizedKeyValuePairs as $normalizedKey => $value) { @@ -1377,23 +1394,25 @@ public function decrementItem($key, $value) return false; } - $this->normalizeKey($key); - $args = new ArrayObject([ - 'key' => & $key, - 'value' => & $value, - ]); + $key = $this->normalizeKey($key); + $params = [ + 'key' => $key, + 'value' => $value, + ]; try { - $eventRs = $this->triggerPre(__FUNCTION__, $args); + $event = new Event('decrementItem.pre', $this, $params); + $eventRs = $this->getEventManager()->triggerEvent($event); + $params = $event->getParams(); $result = $eventRs->stopped() ? $eventRs->last() - : $this->internalDecrementItem($args['key'], $args['value']); + : $this->internalDecrementItem($params['key'], $params['value']); - return $this->triggerPost(__FUNCTION__, $args, $result); + return $this->triggerPost(__FUNCTION__, $params, $result); } catch (\Exception $e) { $result = false; - return $this->triggerException(__FUNCTION__, $args, $result, $e); + return $this->triggerException(__FUNCTION__, $params, $result, $e); } } @@ -1405,7 +1424,7 @@ public function decrementItem($key, $value) * @return int|bool The new value on success, false on failure * @throws Exception\ExceptionInterface */ - protected function internalDecrementItem(& $normalizedKey, & $value) + protected function internalDecrementItem($normalizedKey, $value) { $success = null; $value = (int) $value; @@ -1438,22 +1457,22 @@ public function decrementItems(array $keyValuePairs) return []; } - $this->normalizeKeyValuePairs($keyValuePairs); - $args = new ArrayObject([ - 'keyValuePairs' => & $keyValuePairs, - ]); + $keyValuePairs = $this->normalizeKeyValuePairs($keyValuePairs); + $params = ['keyValuePairs' => $keyValuePairs]; try { - $eventRs = $this->triggerPre(__FUNCTION__, $args); + $event = new Event('decrementItems.pre', $this, $params); + $eventRs = $this->getEventManager()->triggerEvent($event); + $params = $event->getParams(); $result = $eventRs->stopped() ? $eventRs->last() - : $this->internalDecrementItems($args['keyValuePairs']); + : $this->internalDecrementItems($params['keyValuePairs']); - return $this->triggerPost(__FUNCTION__, $args, $result); + return $this->triggerPost(__FUNCTION__, $params, $result); } catch (\Exception $e) { $result = []; - return $this->triggerException(__FUNCTION__, $args, $result, $e); + return $this->triggerException(__FUNCTION__, $params, $result, $e); } } @@ -1464,7 +1483,7 @@ public function decrementItems(array $keyValuePairs) * @return array Associative array of keys and new values * @throws Exception\ExceptionInterface */ - protected function internalDecrementItems(array & $normalizedKeyValuePairs) + protected function internalDecrementItems(array $normalizedKeyValuePairs) { $result = []; foreach ($normalizedKeyValuePairs as $normalizedKey => $value) { @@ -1488,19 +1507,18 @@ protected function internalDecrementItems(array & $normalizedKeyValuePairs) */ public function getCapabilities() { - $args = new ArrayObject(); - try { - $eventRs = $this->triggerPre(__FUNCTION__, $args); + $event = new Event('getCapabilities.pre', $this); + $eventRs = $this->getEventManager()->triggerEvent($event); $result = $eventRs->stopped() ? $eventRs->last() : $this->internalGetCapabilities(); - return $this->triggerPost(__FUNCTION__, $args, $result); + return $this->triggerPost(__FUNCTION__, [], $result); } catch (\Exception $e) { $result = false; - return $this->triggerException(__FUNCTION__, $args, $result, $e); + return $this->triggerException(__FUNCTION__, [], $result, $e); } } @@ -1524,10 +1542,10 @@ protected function internalGetCapabilities() * Validates and normalizes a key * * @param string $key - * @return void + * @return string * @throws Exception\InvalidArgumentException On an invalid key */ - protected function normalizeKey(& $key) + protected function normalizeKey($key) { $key = (string) $key; @@ -1540,16 +1558,18 @@ protected function normalizeKey(& $key) "The key '{$key}' doesn't match against pattern '{$p}'" ); } + + return $key; } /** * Validates and normalizes multiple keys * - * @param array $keys - * @return void + * @param string[] $keys + * @return string[] * @throws Exception\InvalidArgumentException On an invalid key */ - protected function normalizeKeys(array & $keys) + protected function normalizeKeys(array $keys) { if (!$keys) { throw new Exception\InvalidArgumentException( @@ -1557,24 +1577,25 @@ protected function normalizeKeys(array & $keys) ); } - array_walk($keys, [$this, 'normalizeKey']); + $keys = array_map([$this, 'normalizeKey'], $keys); $keys = array_values(array_unique($keys)); + return $keys; } /** * Validates and normalizes an array of key-value pairs * * @param array $keyValuePairs - * @return void + * @return array * @throws Exception\InvalidArgumentException On an invalid key */ - protected function normalizeKeyValuePairs(array & $keyValuePairs) + protected function normalizeKeyValuePairs(array $keyValuePairs) { $normalizedKeyValuePairs = []; foreach ($keyValuePairs as $key => $value) { $this->normalizeKey($key); $normalizedKeyValuePairs[$key] = $value; } - $keyValuePairs = $normalizedKeyValuePairs; + return $normalizedKeyValuePairs; } } diff --git a/src/Storage/Adapter/AbstractZendServer.php b/src/Storage/Adapter/AbstractZendServer.php index 0f07eea3f..b2038c6ec 100644 --- a/src/Storage/Adapter/AbstractZendServer.php +++ b/src/Storage/Adapter/AbstractZendServer.php @@ -33,7 +33,7 @@ abstract class AbstractZendServer extends AbstractAdapter * @return mixed Data on success, null on failure * @throws Exception\ExceptionInterface */ - protected function internalGetItem(& $normalizedKey, & $success = null, & $casToken = null) + protected function internalGetItem($normalizedKey, & $success = null, & $casToken = null) { $namespace = $this->getOptions()->getNamespace(); $prefix = ($namespace === '') ? '' : $namespace . self::NAMESPACE_SEPARATOR; @@ -57,7 +57,7 @@ protected function internalGetItem(& $normalizedKey, & $success = null, & $casTo * @return array Associative array of keys and values * @throws Exception\ExceptionInterface */ - protected function internalGetItems(array & $normalizedKeys) + protected function internalGetItems(array $normalizedKeys) { $namespace = $this->getOptions()->getNamespace(); if ($namespace === '') { @@ -73,7 +73,7 @@ protected function internalGetItems(array & $normalizedKeys) $fetch = $this->zdcFetchMulti($internalKeys); $result = []; $prefixL = strlen($prefix); - foreach ($fetch as $k => & $v) { + foreach ($fetch as $k => $v) { $result[substr($k, $prefixL)] = $v; } @@ -87,7 +87,7 @@ protected function internalGetItems(array & $normalizedKeys) * @return bool * @throws Exception\ExceptionInterface */ - protected function internalHasItem(& $normalizedKey) + protected function internalHasItem($normalizedKey) { $namespace = $this->getOptions()->getNamespace(); $prefix = ($namespace === '') ? '' : $namespace . self::NAMESPACE_SEPARATOR; @@ -101,7 +101,7 @@ protected function internalHasItem(& $normalizedKey) * @return array Array of found keys * @throws Exception\ExceptionInterface */ - protected function internalHasItems(array & $normalizedKeys) + protected function internalHasItems(array $normalizedKeys) { $namespace = $this->getOptions()->getNamespace(); if ($namespace === '') { @@ -117,7 +117,7 @@ protected function internalHasItems(array & $normalizedKeys) $fetch = $this->zdcFetchMulti($internalKeys); $result = []; $prefixL = strlen($prefix); - foreach ($fetch as $internalKey => & $value) { + foreach ($fetch as $internalKey => $value) { $result[] = substr($internalKey, $prefixL); } @@ -134,7 +134,7 @@ protected function internalHasItems(array & $normalizedKeys) * @triggers getMetadatas.post(PostEvent) * @triggers getMetadatas.exception(ExceptionEvent) */ - protected function internalGetMetadatas(array & $normalizedKeys) + protected function internalGetMetadatas(array $normalizedKeys) { $namespace = $this->getOptions()->getNamespace(); if ($namespace === '') { @@ -168,7 +168,7 @@ protected function internalGetMetadatas(array & $normalizedKeys) * @return bool * @throws Exception\ExceptionInterface */ - protected function internalSetItem(& $normalizedKey, & $value) + protected function internalSetItem($normalizedKey, $value) { $options = $this->getOptions(); $namespace = $options->getNamespace(); @@ -184,7 +184,7 @@ protected function internalSetItem(& $normalizedKey, & $value) * @return bool * @throws Exception\ExceptionInterface */ - protected function internalRemoveItem(& $normalizedKey) + protected function internalRemoveItem($normalizedKey) { $namespace = $this->getOptions()->getNamespace(); $prefix = ($namespace === '') ? '' : $namespace . self::NAMESPACE_SEPARATOR; diff --git a/src/Storage/Adapter/AdapterOptions.php b/src/Storage/Adapter/AdapterOptions.php index 93ce44ce0..dde2fab42 100644 --- a/src/Storage/Adapter/AdapterOptions.php +++ b/src/Storage/Adapter/AdapterOptions.php @@ -9,7 +9,6 @@ namespace Zend\Cache\Storage\Adapter; -use ArrayObject; use Zend\Cache\Exception; use Zend\Cache\Storage\Event; use Zend\Cache\Storage\StorageInterface; @@ -242,7 +241,7 @@ public function getWritable() protected function triggerOptionEvent($optionName, $optionValue) { if ($this->adapter instanceof EventsCapableInterface) { - $event = new Event('option', $this->adapter, new ArrayObject([$optionName => $optionValue])); + $event = new Event('option', $this->adapter, [$optionName => $optionValue]); $this->adapter->getEventManager()->triggerEvent($event); } } diff --git a/src/Storage/Adapter/Apc.php b/src/Storage/Adapter/Apc.php index 195ddba4e..ea85ed5d4 100644 --- a/src/Storage/Adapter/Apc.php +++ b/src/Storage/Adapter/Apc.php @@ -212,7 +212,7 @@ public function clearByPrefix($prefix) * @return mixed Data on success, null on failure * @throws Exception\ExceptionInterface */ - protected function internalGetItem(& $normalizedKey, & $success = null, & $casToken = null) + protected function internalGetItem($normalizedKey, & $success = null, & $casToken = null) { $options = $this->getOptions(); $namespace = $options->getNamespace(); @@ -235,7 +235,7 @@ protected function internalGetItem(& $normalizedKey, & $success = null, & $casTo * @return array Associative array of keys and values * @throws Exception\ExceptionInterface */ - protected function internalGetItems(array & $normalizedKeys) + protected function internalGetItems(array $normalizedKeys) { $options = $this->getOptions(); $namespace = $options->getNamespace(); @@ -268,7 +268,7 @@ protected function internalGetItems(array & $normalizedKeys) * @return bool * @throws Exception\ExceptionInterface */ - protected function internalHasItem(& $normalizedKey) + protected function internalHasItem($normalizedKey) { $options = $this->getOptions(); $namespace = $options->getNamespace(); @@ -283,7 +283,7 @@ protected function internalHasItem(& $normalizedKey) * @return array Array of found keys * @throws Exception\ExceptionInterface */ - protected function internalHasItems(array & $normalizedKeys) + protected function internalHasItems(array $normalizedKeys) { $options = $this->getOptions(); $namespace = $options->getNamespace(); @@ -317,7 +317,7 @@ protected function internalHasItems(array & $normalizedKeys) * @return array|bool Metadata on success, false on failure * @throws Exception\ExceptionInterface */ - protected function internalGetMetadata(& $normalizedKey) + protected function internalGetMetadata($normalizedKey) { $options = $this->getOptions(); $namespace = $options->getNamespace(); @@ -338,8 +338,7 @@ protected function internalGetMetadata(& $normalizedKey) return false; } - $this->normalizeMetadata($metadata); - return $metadata; + return $this->normalizeMetadata($metadata); } /** @@ -352,7 +351,7 @@ protected function internalGetMetadata(& $normalizedKey) * @triggers getMetadatas.post(PostEvent) * @triggers getMetadatas.exception(ExceptionEvent) */ - protected function internalGetMetadatas(array & $normalizedKeys) + protected function internalGetMetadatas(array $normalizedKeys) { $keysRegExp = []; foreach ($normalizedKeys as $normalizedKey) { @@ -380,8 +379,7 @@ protected function internalGetMetadatas(array & $normalizedKeys) continue; } - $this->normalizeMetadata($metadata); - $result[substr($internalKey, $prefixL)] = $metadata; + $result[substr($internalKey, $prefixL)] = $this->normalizeMetadata($metadata); } return $result; @@ -397,7 +395,7 @@ protected function internalGetMetadatas(array & $normalizedKeys) * @return bool * @throws Exception\ExceptionInterface */ - protected function internalSetItem(& $normalizedKey, & $value) + protected function internalSetItem($normalizedKey, $value) { $options = $this->getOptions(); $namespace = $options->getNamespace(); @@ -422,7 +420,7 @@ protected function internalSetItem(& $normalizedKey, & $value) * @return array Array of not stored keys * @throws Exception\ExceptionInterface */ - protected function internalSetItems(array & $normalizedKeyValuePairs) + protected function internalSetItems(array $normalizedKeyValuePairs) { $options = $this->getOptions(); $namespace = $options->getNamespace(); @@ -457,7 +455,7 @@ protected function internalSetItems(array & $normalizedKeyValuePairs) * @return bool * @throws Exception\ExceptionInterface */ - protected function internalAddItem(& $normalizedKey, & $value) + protected function internalAddItem($normalizedKey, $value) { $options = $this->getOptions(); $namespace = $options->getNamespace(); @@ -486,7 +484,7 @@ protected function internalAddItem(& $normalizedKey, & $value) * @return array Array of not stored keys * @throws Exception\ExceptionInterface */ - protected function internalAddItems(array & $normalizedKeyValuePairs) + protected function internalAddItems(array $normalizedKeyValuePairs) { $options = $this->getOptions(); $namespace = $options->getNamespace(); @@ -521,7 +519,7 @@ protected function internalAddItems(array & $normalizedKeyValuePairs) * @return bool * @throws Exception\ExceptionInterface */ - protected function internalReplaceItem(& $normalizedKey, & $value) + protected function internalReplaceItem($normalizedKey, $value) { $options = $this->getOptions(); $namespace = $options->getNamespace(); @@ -550,7 +548,7 @@ protected function internalReplaceItem(& $normalizedKey, & $value) * @return bool * @throws Exception\ExceptionInterface */ - protected function internalRemoveItem(& $normalizedKey) + protected function internalRemoveItem($normalizedKey) { $options = $this->getOptions(); $namespace = $options->getNamespace(); @@ -565,7 +563,7 @@ protected function internalRemoveItem(& $normalizedKey) * @return array Array of not removed keys * @throws Exception\ExceptionInterface */ - protected function internalRemoveItems(array & $normalizedKeys) + protected function internalRemoveItems(array $normalizedKeys) { $options = $this->getOptions(); $namespace = $options->getNamespace(); @@ -598,7 +596,7 @@ protected function internalRemoveItems(array & $normalizedKeys) * @return int|bool The new value on success, false on failure * @throws Exception\ExceptionInterface */ - protected function internalIncrementItem(& $normalizedKey, & $value) + protected function internalIncrementItem($normalizedKey, $value) { $options = $this->getOptions(); $namespace = $options->getNamespace(); @@ -629,7 +627,7 @@ protected function internalIncrementItem(& $normalizedKey, & $value) * @return int|bool The new value on success, false on failure * @throws Exception\ExceptionInterface */ - protected function internalDecrementItem(& $normalizedKey, & $value) + protected function internalDecrementItem($normalizedKey, $value) { $options = $this->getOptions(); $namespace = $options->getNamespace(); @@ -716,12 +714,11 @@ protected function internalGetCapabilities() * Normalize metadata to work with APC * * @param array $metadata - * @return void + * @return array */ - protected function normalizeMetadata(array & $metadata) + protected function normalizeMetadata(array $metadata) { - $apcMetadata = $metadata; - $metadata = [ + return [ 'internal_key' => isset($metadata['key']) ? $metadata['key'] : $metadata['info'], 'atime' => isset($metadata['access_time']) ? $metadata['access_time'] : $metadata['atime'], 'ctime' => isset($metadata['creation_time']) ? $metadata['creation_time'] : $metadata['ctime'], @@ -743,7 +740,7 @@ protected function normalizeMetadata(array & $metadata) * @see getItem() * @see setItem() */ - protected function internalCheckAndSetItem(& $token, & $normalizedKey, & $value) + protected function internalCheckAndSetItem($token, $normalizedKey, $value) { if (is_int($token) && is_int($value)) { return apc_cas($normalizedKey, $token, $value); diff --git a/src/Storage/Adapter/Apcu.php b/src/Storage/Adapter/Apcu.php index fa40d89cb..9eb6aeaf8 100644 --- a/src/Storage/Adapter/Apcu.php +++ b/src/Storage/Adapter/Apcu.php @@ -207,7 +207,7 @@ public function clearByPrefix($prefix) * @return mixed Data on success, null on failure * @throws Exception\ExceptionInterface */ - protected function internalGetItem(& $normalizedKey, & $success = null, & $casToken = null) + protected function internalGetItem($normalizedKey, & $success = null, & $casToken = null) { $options = $this->getOptions(); $namespace = $options->getNamespace(); @@ -230,7 +230,7 @@ protected function internalGetItem(& $normalizedKey, & $success = null, & $casTo * @return array Associative array of keys and values * @throws Exception\ExceptionInterface */ - protected function internalGetItems(array & $normalizedKeys) + protected function internalGetItems(array $normalizedKeys) { $options = $this->getOptions(); $namespace = $options->getNamespace(); @@ -263,7 +263,7 @@ protected function internalGetItems(array & $normalizedKeys) * @return bool * @throws Exception\ExceptionInterface */ - protected function internalHasItem(& $normalizedKey) + protected function internalHasItem($normalizedKey) { $options = $this->getOptions(); $namespace = $options->getNamespace(); @@ -278,7 +278,7 @@ protected function internalHasItem(& $normalizedKey) * @return array Array of found keys * @throws Exception\ExceptionInterface */ - protected function internalHasItems(array & $normalizedKeys) + protected function internalHasItems(array $normalizedKeys) { $options = $this->getOptions(); $namespace = $options->getNamespace(); @@ -312,7 +312,7 @@ protected function internalHasItems(array & $normalizedKeys) * @return array|bool Metadata on success, false on failure * @throws Exception\ExceptionInterface */ - protected function internalGetMetadata(& $normalizedKey) + protected function internalGetMetadata($normalizedKey) { $options = $this->getOptions(); $namespace = $options->getNamespace(); @@ -328,8 +328,7 @@ protected function internalGetMetadata(& $normalizedKey) return false; } - $this->normalizeMetadata($metadata); - return $metadata; + return $this->normalizeMetadata($metadata); } /** @@ -342,7 +341,7 @@ protected function internalGetMetadata(& $normalizedKey) * @triggers getMetadatas.post(PostEvent) * @triggers getMetadatas.exception(ExceptionEvent) */ - protected function internalGetMetadatas(array & $normalizedKeys) + protected function internalGetMetadatas(array $normalizedKeys) { $keysRegExp = []; foreach ($normalizedKeys as $normalizedKey) { @@ -365,8 +364,7 @@ protected function internalGetMetadatas(array & $normalizedKeys) $it = new BaseApcuIterator($pattern, $format, 100, APC_LIST_ACTIVE); $result = []; foreach ($it as $internalKey => $metadata) { - $this->normalizeMetadata($metadata); - $result[substr($internalKey, $prefixL)] = $metadata; + $result[substr($internalKey, $prefixL)] = $this->normalizeMetadata($metadata); } return $result; @@ -382,7 +380,7 @@ protected function internalGetMetadatas(array & $normalizedKeys) * @return bool * @throws Exception\ExceptionInterface */ - protected function internalSetItem(& $normalizedKey, & $value) + protected function internalSetItem($normalizedKey, $value) { $options = $this->getOptions(); $namespace = $options->getNamespace(); @@ -407,7 +405,7 @@ protected function internalSetItem(& $normalizedKey, & $value) * @return array Array of not stored keys * @throws Exception\ExceptionInterface */ - protected function internalSetItems(array & $normalizedKeyValuePairs) + protected function internalSetItems(array $normalizedKeyValuePairs) { $options = $this->getOptions(); $namespace = $options->getNamespace(); @@ -442,7 +440,7 @@ protected function internalSetItems(array & $normalizedKeyValuePairs) * @return bool * @throws Exception\ExceptionInterface */ - protected function internalAddItem(& $normalizedKey, & $value) + protected function internalAddItem($normalizedKey, $value) { $options = $this->getOptions(); $namespace = $options->getNamespace(); @@ -471,7 +469,7 @@ protected function internalAddItem(& $normalizedKey, & $value) * @return array Array of not stored keys * @throws Exception\ExceptionInterface */ - protected function internalAddItems(array & $normalizedKeyValuePairs) + protected function internalAddItems(array $normalizedKeyValuePairs) { $options = $this->getOptions(); $namespace = $options->getNamespace(); @@ -506,7 +504,7 @@ protected function internalAddItems(array & $normalizedKeyValuePairs) * @return bool * @throws Exception\ExceptionInterface */ - protected function internalReplaceItem(& $normalizedKey, & $value) + protected function internalReplaceItem($normalizedKey, $value) { $options = $this->getOptions(); $ttl = $options->getTtl(); @@ -539,7 +537,7 @@ protected function internalReplaceItem(& $normalizedKey, & $value) * @see getItem() * @see setItem() */ - protected function internalCheckAndSetItem(& $token, & $normalizedKey, & $value) + protected function internalCheckAndSetItem($token, $normalizedKey, $value) { if (is_int($token) && is_int($value)) { return apcu_cas($normalizedKey, $token, $value); @@ -555,7 +553,7 @@ protected function internalCheckAndSetItem(& $token, & $normalizedKey, & $value) * @return bool * @throws Exception\ExceptionInterface */ - protected function internalRemoveItem(& $normalizedKey) + protected function internalRemoveItem($normalizedKey) { $options = $this->getOptions(); $namespace = $options->getNamespace(); @@ -570,7 +568,7 @@ protected function internalRemoveItem(& $normalizedKey) * @return array Array of not removed keys * @throws Exception\ExceptionInterface */ - protected function internalRemoveItems(array & $normalizedKeys) + protected function internalRemoveItems(array $normalizedKeys) { $options = $this->getOptions(); $namespace = $options->getNamespace(); @@ -603,7 +601,7 @@ protected function internalRemoveItems(array & $normalizedKeys) * @return int|bool The new value on success, false on failure * @throws Exception\ExceptionInterface */ - protected function internalIncrementItem(& $normalizedKey, & $value) + protected function internalIncrementItem($normalizedKey, $value) { $options = $this->getOptions(); $namespace = $options->getNamespace(); @@ -634,7 +632,7 @@ protected function internalIncrementItem(& $normalizedKey, & $value) * @return int|bool The new value on success, false on failure * @throws Exception\ExceptionInterface */ - protected function internalDecrementItem(& $normalizedKey, & $value) + protected function internalDecrementItem($normalizedKey, $value) { $options = $this->getOptions(); $namespace = $options->getNamespace(); @@ -723,10 +721,9 @@ protected function internalGetCapabilities() * @param array $metadata * @return void */ - protected function normalizeMetadata(array & $metadata) + protected function normalizeMetadata(array $metadata) { - $apcMetadata = $metadata; - $metadata = [ + return [ 'internal_key' => $metadata['key'], 'atime' => $metadata['access_time'], 'ctime' => $metadata['creation_time'], diff --git a/src/Storage/Adapter/BlackHole.php b/src/Storage/Adapter/BlackHole.php index 655bc2037..c3745f536 100644 --- a/src/Storage/Adapter/BlackHole.php +++ b/src/Storage/Adapter/BlackHole.php @@ -105,9 +105,9 @@ public function getOptions() /** * Get an item. * - * @param string $key - * @param bool $success - * @param mixed $casToken + * @param string $key + * @param bool $success + * @param mixed $casToken * @return mixed Data on success, null on failure */ public function getItem($key, & $success = null, & $casToken = null) diff --git a/src/Storage/Adapter/Dba.php b/src/Storage/Adapter/Dba.php index e6c1f42d6..ea5d1a5c3 100644 --- a/src/Storage/Adapter/Dba.php +++ b/src/Storage/Adapter/Dba.php @@ -325,7 +325,7 @@ public function optimize() * @return mixed Data on success, null on failure * @throws Exception\ExceptionInterface */ - protected function internalGetItem(& $normalizedKey, & $success = null, & $casToken = null) + protected function internalGetItem($normalizedKey, & $success = null, & $casToken = null) { $options = $this->getOptions(); $namespace = $options->getNamespace(); @@ -351,7 +351,7 @@ protected function internalGetItem(& $normalizedKey, & $success = null, & $casTo * @return bool * @throws Exception\ExceptionInterface */ - protected function internalHasItem(& $normalizedKey) + protected function internalHasItem($normalizedKey) { $options = $this->getOptions(); $namespace = $options->getNamespace(); @@ -371,7 +371,7 @@ protected function internalHasItem(& $normalizedKey) * @return bool * @throws Exception\ExceptionInterface */ - protected function internalSetItem(& $normalizedKey, & $value) + protected function internalSetItem($normalizedKey, $value) { $options = $this->getOptions(); $namespace = $options->getNamespace(); @@ -396,7 +396,7 @@ protected function internalSetItem(& $normalizedKey, & $value) * @return bool * @throws Exception\ExceptionInterface */ - protected function internalAddItem(& $normalizedKey, & $value) + protected function internalAddItem($normalizedKey, $value) { $options = $this->getOptions(); $namespace = $options->getNamespace(); @@ -429,7 +429,7 @@ protected function internalAddItem(& $normalizedKey, & $value) * @return bool * @throws Exception\ExceptionInterface */ - protected function internalRemoveItem(& $normalizedKey) + protected function internalRemoveItem($normalizedKey) { $options = $this->getOptions(); $namespace = $options->getNamespace(); diff --git a/src/Storage/Adapter/Filesystem.php b/src/Storage/Adapter/Filesystem.php index 7a645e6c5..13279a682 100644 --- a/src/Storage/Adapter/Filesystem.php +++ b/src/Storage/Adapter/Filesystem.php @@ -25,7 +25,6 @@ use Zend\Cache\Storage\TaggableInterface; use Zend\Cache\Storage\TotalSpaceCapableInterface; use Zend\Stdlib\ErrorHandler; -use ArrayObject; class Filesystem extends AbstractAdapter implements AvailableSpaceCapableInterface, @@ -166,7 +165,7 @@ public function clearExpired() $result = false; return $this->triggerException( __FUNCTION__, - new ArrayObject(), + [], $result, new Exception\RuntimeException('Failed to clear expired items', 0, $error) ); @@ -262,7 +261,7 @@ public function clearByPrefix($prefix) */ public function setTags($key, array $tags) { - $this->normalizeKey($key); + $key = $this->normalizeKey($key); if (!$this->internalHasItem($key)) { return false; } @@ -286,7 +285,7 @@ public function setTags($key, array $tags) */ public function getTags($key) { - $this->normalizeKey($key); + $key = $this->normalizeKey($key); if (!$this->internalHasItem($key)) { return false; } @@ -521,7 +520,7 @@ public function getItems(array $keys) * @throws Exception\ExceptionInterface * @throws BaseException */ - protected function internalGetItem(& $normalizedKey, & $success = null, & $casToken = null) + protected function internalGetItem($normalizedKey, & $success = null, & $casToken = null) { if (!$this->internalHasItem($normalizedKey)) { $success = false; @@ -551,7 +550,7 @@ protected function internalGetItem(& $normalizedKey, & $success = null, & $casTo * @return array Associative array of keys and values * @throws Exception\ExceptionInterface */ - protected function internalGetItems(array & $normalizedKeys) + protected function internalGetItems(array $normalizedKeys) { $keys = $normalizedKeys; // Don't change argument passed by reference $result = []; @@ -634,7 +633,7 @@ public function hasItems(array $keys) * @return bool * @throws Exception\ExceptionInterface */ - protected function internalHasItem(& $normalizedKey) + protected function internalHasItem($normalizedKey) { $file = $this->getFileSpec($normalizedKey) . '.dat'; if (!file_exists($file)) { @@ -697,7 +696,7 @@ public function getMetadatas(array $keys, array $options = []) * @param string $normalizedKey * @return array|bool Metadata on success, false on failure */ - protected function internalGetMetadata(& $normalizedKey) + protected function internalGetMetadata($normalizedKey) { if (!$this->internalHasItem($normalizedKey)) { return false; @@ -730,7 +729,7 @@ protected function internalGetMetadata(& $normalizedKey) * @return array Associative array of keys and metadata * @throws Exception\ExceptionInterface */ - protected function internalGetMetadatas(array & $normalizedKeys) + protected function internalGetMetadatas(array $normalizedKeys) { $options = $this->getOptions(); $result = []; @@ -896,7 +895,7 @@ public function replaceItems(array $keyValuePairs) * @return bool * @throws Exception\ExceptionInterface */ - protected function internalSetItem(& $normalizedKey, & $value) + protected function internalSetItem($normalizedKey, $value) { $filespec = $this->getFileSpec($normalizedKey); $this->prepareDirectoryStructure($filespec); @@ -923,16 +922,16 @@ protected function internalSetItem(& $normalizedKey, & $value) * @return array Array of not stored keys * @throws Exception\ExceptionInterface */ - protected function internalSetItems(array & $normalizedKeyValuePairs) + protected function internalSetItems(array $normalizedKeyValuePairs) { // create an associated array of files and contents to write $contents = []; - foreach ($normalizedKeyValuePairs as $key => & $value) { + foreach ($normalizedKeyValuePairs as $key => $value) { $filespec = $this->getFileSpec($key); $this->prepareDirectoryStructure($filespec); // *.dat file - $contents[$filespec . '.dat'] = & $value; + $contents[$filespec . '.dat'] = $value; // *.tag file $this->unlink($filespec . '.tag'); @@ -943,7 +942,7 @@ protected function internalSetItems(array & $normalizedKeyValuePairs) $nonBlocking = count($contents) > 1; $wouldblock = null; - foreach ($contents as $file => & $content) { + foreach ($contents as $file => $content) { $this->putFileContent($file, $content, $nonBlocking, $wouldblock); if (!$nonBlocking || !$wouldblock) { unset($contents[$file]); @@ -990,7 +989,7 @@ public function checkAndSetItem($token, $key, $value) * @see getItem() * @see setItem() */ - protected function internalCheckAndSetItem(& $token, & $normalizedKey, & $value) + protected function internalCheckAndSetItem($token, $normalizedKey, $value) { if (!$this->internalHasItem($normalizedKey)) { return false; @@ -1055,7 +1054,7 @@ public function touchItems(array $keys) * @return bool * @throws Exception\ExceptionInterface */ - protected function internalTouchItem(& $normalizedKey) + protected function internalTouchItem($normalizedKey) { if (!$this->internalHasItem($normalizedKey)) { return false; @@ -1122,7 +1121,7 @@ public function removeItems(array $keys) * @return bool * @throws Exception\ExceptionInterface */ - protected function internalRemoveItem(& $normalizedKey) + protected function internalRemoveItem($normalizedKey) { $filespec = $this->getFileSpec($normalizedKey); if (!file_exists($filespec . '.dat')) { diff --git a/src/Storage/Adapter/Memcache.php b/src/Storage/Adapter/Memcache.php index a7990181e..e4bfdd9cb 100644 --- a/src/Storage/Adapter/Memcache.php +++ b/src/Storage/Adapter/Memcache.php @@ -138,7 +138,7 @@ public function getOptions() * @param mixed $value * @return int */ - protected function getWriteFlag(& $value) + protected function getWriteFlag($value) { if (!$this->getOptions()->getCompression()) { return 0; @@ -212,7 +212,7 @@ public function getAvailableSpace() * @return mixed Data on success, null on failure * @throws Exception\ExceptionInterface */ - protected function internalGetItem(& $normalizedKey, & $success = null, & $casToken = null) + protected function internalGetItem($normalizedKey, & $success = null, & $casToken = null) { $memc = $this->getMemcacheResource(); $internalKey = $this->namespacePrefix . $normalizedKey; @@ -234,7 +234,7 @@ protected function internalGetItem(& $normalizedKey, & $success = null, & $casTo * @return array Associative array of keys and values * @throws Exception\ExceptionInterface */ - protected function internalGetItems(array & $normalizedKeys) + protected function internalGetItems(array $normalizedKeys) { $memc = $this->getMemcacheResource(); @@ -251,8 +251,8 @@ protected function internalGetItems(array & $normalizedKeys) if ($this->namespacePrefix !== '') { $tmp = []; $nsPrefixLength = strlen($this->namespacePrefix); - foreach ($result as $internalKey => & $value) { - $tmp[substr($internalKey, $nsPrefixLength)] = & $value; + foreach ($result as $internalKey => $value) { + $tmp[substr($internalKey, $nsPrefixLength)] = $value; } $result = $tmp; } @@ -267,7 +267,7 @@ protected function internalGetItems(array & $normalizedKeys) * @return bool * @throws Exception\ExceptionInterface */ - protected function internalHasItem(& $normalizedKey) + protected function internalHasItem($normalizedKey) { $memc = $this->getMemcacheResource(); $value = $memc->get($this->namespacePrefix . $normalizedKey); @@ -281,7 +281,7 @@ protected function internalHasItem(& $normalizedKey) * @return array Array of found keys * @throws Exception\ExceptionInterface */ - protected function internalHasItems(array & $normalizedKeys) + protected function internalHasItems(array $normalizedKeys) { $memc = $this->getMemcacheResource(); @@ -315,7 +315,7 @@ protected function internalHasItems(array & $normalizedKeys) * @return array Associative array of keys and metadata * @throws Exception\ExceptionInterface */ - protected function internalGetMetadatas(array & $normalizedKeys) + protected function internalGetMetadatas(array $normalizedKeys) { $memc = $this->getMemcacheResource(); @@ -354,7 +354,7 @@ protected function internalGetMetadatas(array & $normalizedKeys) * @return bool * @throws Exception\ExceptionInterface */ - protected function internalSetItem(& $normalizedKey, & $value) + protected function internalSetItem($normalizedKey, $value) { $memc = $this->getMemcacheResource(); $expiration = $this->expirationTime(); @@ -375,7 +375,7 @@ protected function internalSetItem(& $normalizedKey, & $value) * @return bool * @throws Exception\ExceptionInterface */ - protected function internalAddItem(& $normalizedKey, & $value) + protected function internalAddItem($normalizedKey, $value) { $memc = $this->getMemcacheResource(); $expiration = $this->expirationTime(); @@ -392,7 +392,7 @@ protected function internalAddItem(& $normalizedKey, & $value) * @return bool * @throws Exception\ExceptionInterface */ - protected function internalReplaceItem(& $normalizedKey, & $value) + protected function internalReplaceItem($normalizedKey, $value) { $memc = $this->getMemcacheResource(); $expiration = $this->expirationTime(); @@ -408,7 +408,7 @@ protected function internalReplaceItem(& $normalizedKey, & $value) * @return bool * @throws Exception\ExceptionInterface */ - protected function internalRemoveItem(& $normalizedKey) + protected function internalRemoveItem($normalizedKey) { $memc = $this->getMemcacheResource(); // Delete's second parameter (timeout) is deprecated and not supported. @@ -425,7 +425,7 @@ protected function internalRemoveItem(& $normalizedKey) * @return int|bool The new value on success, false on failure * @throws Exception\ExceptionInterface */ - protected function internalIncrementItem(& $normalizedKey, & $value) + protected function internalIncrementItem($normalizedKey, $value) { $memc = $this->getMemcacheResource(); $internalKey = $this->namespacePrefix . $normalizedKey; @@ -454,7 +454,7 @@ protected function internalIncrementItem(& $normalizedKey, & $value) * @return int|bool The new value on success, false on failure * @throws Exception\ExceptionInterface */ - protected function internalDecrementItem(& $normalizedKey, & $value) + protected function internalDecrementItem($normalizedKey, $value) { $memc = $this->getMemcacheResource(); $internalKey = $this->namespacePrefix . $normalizedKey; diff --git a/src/Storage/Adapter/MemcacheResourceManager.php b/src/Storage/Adapter/MemcacheResourceManager.php index be5b17d68..f9c0b755f 100644 --- a/src/Storage/Adapter/MemcacheResourceManager.php +++ b/src/Storage/Adapter/MemcacheResourceManager.php @@ -126,7 +126,7 @@ public function setResource($id, $resource, $failureCallback = null, $serverDefa } $resourceOptions = [ - 'servers' => [], + 'servers' => [], 'auto_compress_threshold' => null, 'auto_compress_min_savings' => null, ]; @@ -137,14 +137,12 @@ public function setResource($id, $resource, $failureCallback = null, $serverDefa $resource['auto_compress_threshold'], $resource['auto_compress_min_savings'] ); - $this->normalizeServers($resource['servers']); + $resource['servers'] = $this->normalizeServers($resource['servers']); } - $this->normalizeServerDefaults($serverDefaults); - - $this->resources[$id] = $resource; + $this->resources[$id] = $resource; $this->failureCallbacks[$id] = $failureCallback; - $this->serverDefaults[$id] = $serverDefaults; + $this->serverDefaults[$id] = $this->normalizeServerDefaults($serverDefaults); return $this; } @@ -214,7 +212,7 @@ public function getAutoCompressThreshold($id) throw new Exception\RuntimeException("No resource with id '{$id}'"); } - $resource = & $this->resources[$id]; + $resource = $this->resources[$id]; if ($resource instanceof MemcacheResource) { // Cannot get options from Memcache resource once created throw new Exception\RuntimeException("Cannot get compress threshold once resource is created"); @@ -240,13 +238,12 @@ public function setAutoCompressThreshold($id, $threshold, $minSavings = false) $this->normalizeAutoCompressThreshold($threshold, $minSavings); - $resource = & $this->resources[$id]; - if ($resource instanceof MemcacheResource) { - $this->setResourceAutoCompressThreshold($resource, $threshold, $minSavings); + if ($this->resources[$id] instanceof MemcacheResource) { + $this->setResourceAutoCompressThreshold($this->resources[$id], $threshold, $minSavings); } else { - $resource['auto_compress_threshold'] = $threshold; + $this->resources[$id]['auto_compress_threshold'] = $threshold; if ($minSavings !== false) { - $resource['auto_compress_min_savings'] = $minSavings; + $this->resources[$id]['auto_compress_min_savings'] = $minSavings; } } return $this; @@ -265,7 +262,7 @@ public function getAutoCompressMinSavings($id) throw new Exception\RuntimeException("No resource with id '{$id}'"); } - $resource = & $this->resources[$id]; + $resource = $this->resources[$id]; if ($resource instanceof MemcacheResource) { // Cannot get options from Memcache resource once created throw new Exception\RuntimeException("Cannot get compress min savings once resource is created"); @@ -285,19 +282,16 @@ public function setAutoCompressMinSavings($id, $minSavings) { if (!$this->hasResource($id)) { return $this->setResource($id, [ - 'auto_compress_min_savings' => $minSavings, + 'auto_compress_min_savings' => (float) $minSavings, ]); } - $minSavings = (float) $minSavings; - - $resource = & $this->resources[$id]; - if ($resource instanceof MemcacheResource) { + if ($this->resources[$id] instanceof MemcacheResource) { throw new Exception\RuntimeException( "Cannot set compress min savings without a threshold value once a resource is created" ); } else { - $resource['auto_compress_min_savings'] = $minSavings; + $this->resources[$id]['auto_compress_min_savings'] = (float) $minSavings; } return $this; } @@ -320,8 +314,7 @@ public function setServerDefaults($id, array $serverDefaults) ]); } - $this->normalizeServerDefaults($serverDefaults); - $this->serverDefaults[$id] = $serverDefaults; + $this->serverDefaults[$id] = $this->normalizeServerDefaults($serverDefaults); return $this; } @@ -342,10 +335,11 @@ public function getServerDefaults($id) } /** - * @param array $serverDefaults + * @param array $serverDefaults + * @return array * @throws Exception\InvalidArgumentException */ - protected function normalizeServerDefaults(& $serverDefaults) + protected function normalizeServerDefaults($serverDefaults) { if (!is_array($serverDefaults) && !($serverDefaults instanceof Traversable)) { throw new Exception\InvalidArgumentException( @@ -355,9 +349,9 @@ protected function normalizeServerDefaults(& $serverDefaults) // Defaults $result = [ - 'persistent' => true, - 'weight' => 1, - 'timeout' => 1, // seconds + 'persistent' => true, + 'weight' => 1, + 'timeout' => 1, // seconds 'retry_interval' => 15, // seconds ]; @@ -375,7 +369,7 @@ protected function normalizeServerDefaults(& $serverDefaults) $result[$key] = $value; } - $serverDefaults = $result; + return $result; } /** @@ -423,7 +417,7 @@ public function getServers($id) throw new Exception\RuntimeException("No resource with id '{$id}'"); } - $resource = & $this->resources[$id]; + $resource = $this->resources[$id]; if ($resource instanceof MemcacheResource) { throw new Exception\RuntimeException("Cannot get server list once resource is created"); } @@ -445,9 +439,8 @@ public function addServers($id, $servers) ]); } - $this->normalizeServers($servers); - - $resource = & $this->resources[$id]; + $servers = $this->normalizeServers($servers); + $resource = $this->resources[$id]; if ($resource instanceof MemcacheResource) { foreach ($servers as $server) { $this->addServerToResource( @@ -459,7 +452,7 @@ public function addServers($id, $servers) } } else { // don't add servers twice - $resource['servers'] = array_merge( + $this->resources[$id]['servers'] = array_merge( $resource['servers'], array_udiff($servers, $resource['servers'], [$this, 'compareServers']) ); @@ -515,9 +508,10 @@ protected function addServerToResource( * Normalize a list of servers into the following format: * array(array('host' => , 'port' => , 'weight' => )[, ...]) * - * @param string|array $servers + * @param string|array $servers + * @return array */ - protected function normalizeServers(& $servers) + protected function normalizeServers($servers) { if (is_string($servers)) { // Convert string into a list of servers @@ -526,11 +520,11 @@ protected function normalizeServers(& $servers) $result = []; foreach ($servers as $server) { - $this->normalizeServer($server); + $server = $this->normalizeServer($server); $result[$server['host'] . ':' . $server['port']] = $server; } - $servers = array_values($result); + return array_values($result); } /** @@ -541,15 +535,16 @@ protected function normalizeServers(& $servers) * 'timeout' => , 'retry_interval' => , * ) * - * @param string|array $server + * @param string|array $server + * @return array * @throws Exception\InvalidArgumentException */ - protected function normalizeServer(& $server) + protected function normalizeServer($server) { // WARNING: The order of this array is important. // Used for converting an ordered array to a keyed array. // Append new options, do not insert or you will break BC. - $sTmp = [ + $result = [ 'host' => null, 'port' => 11211, 'weight' => null, @@ -569,11 +564,11 @@ protected function normalizeServer(& $server) // Convert ordered array to keyed array // array([, [, [, [, [, [, ]]]]]]) $server = array_combine( - array_slice(array_keys($sTmp), 0, count($server)), + array_slice(array_keys($result), 0, count($server)), $server ); } - $sTmp = array_merge($sTmp, $server); + $result = array_merge($result, $server); } elseif (is_string($server)) { // parse server from URI host{:?port}{?weight} $server = trim($server); @@ -586,20 +581,20 @@ protected function normalizeServer(& $server) throw new Exception\InvalidArgumentException("Invalid server given"); } - $sTmp = array_merge($sTmp, array_intersect_key($urlParts, $sTmp)); + $result = array_merge($result, array_intersect_key($urlParts, $result)); if (isset($urlParts['query'])) { $query = null; parse_str($urlParts['query'], $query); - $sTmp = array_merge($sTmp, array_intersect_key($query, $sTmp)); + $result = array_merge($result, array_intersect_key($query, $result)); } } - if (!$sTmp['host']) { + if (!$result['host']) { throw new Exception\InvalidArgumentException('Missing required server host'); } // Filter values - foreach ($sTmp as $key => $value) { + foreach ($result as $key => $value) { if (isset($value)) { switch ($key) { case 'host': @@ -617,16 +612,12 @@ protected function normalizeServer(& $server) break; } } - $sTmp[$key] = $value; + $result[$key] = $value; } - $sTmp = array_filter( - $sTmp, - function ($val) { - return isset($val); - } - ); - $server = $sTmp; + return array_filter($result, function ($val) { + return isset($val); + }); } /** diff --git a/src/Storage/Adapter/Memcached.php b/src/Storage/Adapter/Memcached.php index fec10210f..7d87976b0 100644 --- a/src/Storage/Adapter/Memcached.php +++ b/src/Storage/Adapter/Memcached.php @@ -198,7 +198,7 @@ public function getAvailableSpace() * @return mixed Data on success, null on failure * @throws Exception\ExceptionInterface */ - protected function internalGetItem(& $normalizedKey, & $success = null, & $casToken = null) + protected function internalGetItem($normalizedKey, & $success = null, & $casToken = null) { $memc = $this->getMemcachedResource(); $internalKey = $this->namespacePrefix . $normalizedKey; @@ -231,7 +231,7 @@ protected function internalGetItem(& $normalizedKey, & $success = null, & $casTo * @return array Associative array of keys and values * @throws Exception\ExceptionInterface */ - protected function internalGetItems(array & $normalizedKeys) + protected function internalGetItems(array $normalizedKeys) { $memc = $this->getMemcachedResource(); @@ -270,7 +270,7 @@ protected function internalGetItems(array & $normalizedKeys) * @return bool * @throws Exception\ExceptionInterface */ - protected function internalHasItem(& $normalizedKey) + protected function internalHasItem($normalizedKey) { $memc = $this->getMemcachedResource(); $value = $memc->get($this->namespacePrefix . $normalizedKey); @@ -295,7 +295,7 @@ protected function internalHasItem(& $normalizedKey) * @return array Array of found keys * @throws Exception\ExceptionInterface */ - protected function internalHasItems(array & $normalizedKeys) + protected function internalHasItems(array $normalizedKeys) { return array_keys($this->internalGetItems($normalizedKeys)); } @@ -307,7 +307,7 @@ protected function internalHasItems(array & $normalizedKeys) * @return array Associative array of keys and metadata * @throws Exception\ExceptionInterface */ - protected function internalGetMetadatas(array & $normalizedKeys) + protected function internalGetMetadatas(array $normalizedKeys) { return array_fill_keys(array_keys($this->internalGetItems($normalizedKeys)), []); } @@ -322,7 +322,7 @@ protected function internalGetMetadatas(array & $normalizedKeys) * @return bool * @throws Exception\ExceptionInterface */ - protected function internalSetItem(& $normalizedKey, & $value) + protected function internalSetItem($normalizedKey, $value) { $memc = $this->getMemcachedResource(); $expiration = $this->expirationTime(); @@ -340,7 +340,7 @@ protected function internalSetItem(& $normalizedKey, & $value) * @return array Array of not stored keys * @throws Exception\ExceptionInterface */ - protected function internalSetItems(array & $normalizedKeyValuePairs) + protected function internalSetItems(array $normalizedKeyValuePairs) { $memc = $this->getMemcachedResource(); $expiration = $this->expirationTime(); @@ -365,7 +365,7 @@ protected function internalSetItems(array & $normalizedKeyValuePairs) * @return bool * @throws Exception\ExceptionInterface */ - protected function internalAddItem(& $normalizedKey, & $value) + protected function internalAddItem($normalizedKey, $value) { $memc = $this->getMemcachedResource(); $expiration = $this->expirationTime(); @@ -387,7 +387,7 @@ protected function internalAddItem(& $normalizedKey, & $value) * @return bool * @throws Exception\ExceptionInterface */ - protected function internalReplaceItem(& $normalizedKey, & $value) + protected function internalReplaceItem($normalizedKey, $value) { $memc = $this->getMemcachedResource(); $expiration = $this->expirationTime(); @@ -413,7 +413,7 @@ protected function internalReplaceItem(& $normalizedKey, & $value) * @see getItem() * @see setItem() */ - protected function internalCheckAndSetItem(& $token, & $normalizedKey, & $value) + protected function internalCheckAndSetItem($token, $normalizedKey, $value) { $memc = $this->getMemcachedResource(); $expiration = $this->expirationTime(); @@ -436,7 +436,7 @@ protected function internalCheckAndSetItem(& $token, & $normalizedKey, & $value) * @return bool * @throws Exception\ExceptionInterface */ - protected function internalRemoveItem(& $normalizedKey) + protected function internalRemoveItem($normalizedKey) { $memc = $this->getMemcachedResource(); $result = $memc->delete($this->namespacePrefix . $normalizedKey); @@ -460,7 +460,7 @@ protected function internalRemoveItem(& $normalizedKey) * @return array Array of not removed keys * @throws Exception\ExceptionInterface */ - protected function internalRemoveItems(array & $normalizedKeys) + protected function internalRemoveItems(array $normalizedKeys) { $memc = $this->getMemcachedResource(); @@ -503,7 +503,7 @@ protected function internalRemoveItems(array & $normalizedKeys) * @return int|bool The new value on success, false on failure * @throws Exception\ExceptionInterface */ - protected function internalIncrementItem(& $normalizedKey, & $value) + protected function internalIncrementItem($normalizedKey, $value) { $memc = $this->getMemcachedResource(); $internalKey = $this->namespacePrefix . $normalizedKey; @@ -536,7 +536,7 @@ protected function internalIncrementItem(& $normalizedKey, & $value) * @return int|bool The new value on success, false on failure * @throws Exception\ExceptionInterface */ - protected function internalDecrementItem(& $normalizedKey, & $value) + protected function internalDecrementItem($normalizedKey, $value) { $memc = $this->getMemcachedResource(); $internalKey = $this->namespacePrefix . $normalizedKey; diff --git a/src/Storage/Adapter/MemcachedResourceManager.php b/src/Storage/Adapter/MemcachedResourceManager.php index 7c8fd91ed..354845ce2 100644 --- a/src/Storage/Adapter/MemcachedResourceManager.php +++ b/src/Storage/Adapter/MemcachedResourceManager.php @@ -39,8 +39,7 @@ public function getServers($id) throw new Exception\RuntimeException("No resource with id '{$id}'"); } - $resource = & $this->resources[$id]; - + $resource = $this->resources[$id]; if ($resource instanceof MemcachedResource) { return $resource->getServerList(); } @@ -51,10 +50,11 @@ public function getServers($id) * Normalize one server into the following format: * array('host' => , 'port' => , 'weight' => ) * - * @param string|array &$server + * @param string|array $server + * @return array * @throws Exception\InvalidArgumentException */ - protected function normalizeServer(&$server) + protected function normalizeServer($server) { $host = null; $port = 11211; @@ -107,7 +107,7 @@ protected function normalizeServer(&$server) throw new Exception\InvalidArgumentException('Missing required server host'); } - $server = [ + return [ 'host' => $host, 'port' => $port, 'weight' => $weight, @@ -195,9 +195,9 @@ public function setResource($id, $resource) ], $resource); // normalize and validate params - $this->normalizePersistentId($resource['persistent_id']); - $this->normalizeLibOptions($resource['lib_options']); - $this->normalizeServers($resource['servers']); + $resource['persistent_id'] = $this->normalizePersistentId($resource['persistent_id']); + $resource['lib_options'] = $this->normalizeLibOptions($resource['lib_options']); + $resource['servers'] = $this->normalizeServers($resource['servers']); } $this->resources[$id] = $resource; @@ -232,15 +232,14 @@ public function setPersistentId($id, $persistentId) ]); } - $resource = & $this->resources[$id]; - if ($resource instanceof MemcachedResource) { + if ($this->resources[$id] instanceof MemcachedResource) { throw new Exception\RuntimeException( "Can't change persistent id of resource {$id} after instanziation" ); } - $this->normalizePersistentId($persistentId); - $resource['persistent_id'] = $persistentId; + $persistentId = $this->normalizePersistentId($persistentId); + $this->resources[$id]['persistent_id'] = $persistentId; return $this; } @@ -258,8 +257,7 @@ public function getPersistentId($id) throw new Exception\RuntimeException("No resource with id '{$id}'"); } - $resource = & $this->resources[$id]; - + $resource = $this->resources[$id]; if ($resource instanceof MemcachedResource) { throw new Exception\RuntimeException( "Can't get persistent id of an instantiated memcached resource" @@ -272,11 +270,12 @@ public function getPersistentId($id) /** * Normalize the persistent id * - * @param string $persistentId + * @param string $persistentId + * @return string */ - protected function normalizePersistentId(& $persistentId) + protected function normalizePersistentId($persistentId) { - $persistentId = (string) $persistentId; + return (string) $persistentId; } /** @@ -294,9 +293,9 @@ public function setLibOptions($id, array $libOptions) ]); } - $this->normalizeLibOptions($libOptions); + $libOptions = $this->normalizeLibOptions($libOptions); - $resource = & $this->resources[$id]; + $resource = $this->resources[$id]; if ($resource instanceof MemcachedResource) { if (method_exists($resource, 'setOptions')) { $resource->setOptions($libOptions); @@ -306,7 +305,7 @@ public function setLibOptions($id, array $libOptions) } } } else { - $resource['lib_options'] = $libOptions; + $this->resources[$id]['lib_options'] = $libOptions; } return $this; @@ -325,7 +324,7 @@ public function getLibOptions($id) throw new Exception\RuntimeException("No resource with id '{$id}'"); } - $resource = & $this->resources[$id]; + $resource = $this->resources[$id]; if ($resource instanceof MemcachedResource) { $libOptions = []; @@ -368,8 +367,8 @@ public function getLibOption($id, $key) throw new Exception\RuntimeException("No resource with id '{$id}'"); } - $this->normalizeLibOptionKey($key); - $resource = & $this->resources[$id]; + $key = $this->normalizeLibOptionKey($key); + $resource = $this->resources[$id]; if ($resource instanceof MemcachedResource) { return $resource->getOption($key); @@ -381,10 +380,11 @@ public function getLibOption($id, $key) /** * Normalize libmemcached options * - * @param array|Traversable $libOptions + * @param array|Traversable $libOptions + * @return array * @throws Exception\InvalidArgumentException */ - protected function normalizeLibOptions(& $libOptions) + protected function normalizeLibOptions($libOptions) { if (!is_array($libOptions) && !($libOptions instanceof Traversable)) { throw new Exception\InvalidArgumentException( @@ -394,20 +394,21 @@ protected function normalizeLibOptions(& $libOptions) $result = []; foreach ($libOptions as $key => $value) { - $this->normalizeLibOptionKey($key); + $key = $this->normalizeLibOptionKey($key); $result[$key] = $value; } - $libOptions = $result; + return $result; } /** * Convert option name into it's constant value * - * @param string|int $key + * @param string|int $key + * @return int * @throws Exception\InvalidArgumentException */ - protected function normalizeLibOptionKey(& $key) + protected function normalizeLibOptionKey($key) { // convert option name into it's constant value if (is_string($key)) { @@ -415,10 +416,9 @@ protected function normalizeLibOptionKey(& $key) if (!defined($const)) { throw new Exception\InvalidArgumentException("Unknown libmemcached option '{$key}' ({$const})"); } - $key = constant($const); - } else { - $key = (int) $key; + return constant($const); } + return (int) $key; } /** @@ -442,9 +442,8 @@ public function setServers($id, $servers) ]); } - $this->normalizeServers($servers); - - $resource = & $this->resources[$id]; + $servers = $this->normalizeServers($servers); + $resource = $this->resources[$id]; if ($resource instanceof MemcachedResource) { // don't add servers twice $servers = array_udiff($servers, $resource->getServerList(), [$this, 'compareServers']); @@ -452,7 +451,7 @@ public function setServers($id, $servers) $resource->addServers($servers); } } else { - $resource['servers'] = $servers; + $this->resources[$id]['servers'] = $servers; } return $this; @@ -473,9 +472,8 @@ public function addServers($id, $servers) ]); } - $this->normalizeServers($servers); - - $resource = & $this->resources[$id]; + $servers = $this->normalizeServers($servers); + $resource = $this->resources[$id]; if ($resource instanceof MemcachedResource) { // don't add servers twice $servers = array_udiff($servers, $resource->getServerList(), [$this, 'compareServers']); @@ -484,7 +482,7 @@ public function addServers($id, $servers) } } else { // don't add servers twice - $resource['servers'] = array_merge( + $this->resources[$id]['servers'] = array_merge( $resource['servers'], array_udiff($servers, $resource['servers'], [$this, 'compareServers']) ); @@ -509,9 +507,10 @@ public function addServer($id, $server) * Normalize a list of servers into the following format: * array(array('host' => , 'port' => , 'weight' => )[, ...]) * - * @param string|array $servers + * @param string|array $servers + * @return array[] */ - protected function normalizeServers(& $servers) + protected function normalizeServers($servers) { if (!is_array($servers) && !$servers instanceof Traversable) { // Convert string into a list of servers @@ -520,11 +519,11 @@ protected function normalizeServers(& $servers) $result = []; foreach ($servers as $server) { - $this->normalizeServer($server); + $server = $this->normalizeServer($server); $result[$server['host'] . ':' . $server['port']] = $server; } - $servers = array_values($result); + return array_values($result); } /** diff --git a/src/Storage/Adapter/Memory.php b/src/Storage/Adapter/Memory.php index 9362be47a..7fb046ed5 100644 --- a/src/Storage/Adapter/Memory.php +++ b/src/Storage/Adapter/Memory.php @@ -118,7 +118,7 @@ public function getIterator() $keys = []; if (isset($this->data[$ns])) { - foreach ($this->data[$ns] as $key => & $tmp) { + foreach ($this->data[$ns] as $key => $tmp) { if ($this->internalHasItem($key)) { $keys[] = $key; } @@ -160,10 +160,9 @@ public function clearExpired() return true; } - $data = & $this->data[$ns]; - foreach ($data as $key => & $item) { - if (microtime(true) >= $data[$key][1] + $ttl) { - unset($data[$key]); + foreach ($this->data[$ns] as $key => $item) { + if (microtime(true) >= $this->data[$ns][$key][1] + $ttl) { + unset($this->data[$ns][$key]); } } @@ -204,10 +203,9 @@ public function clearByPrefix($prefix) } $prefixL = strlen($prefix); - $data = & $this->data[$ns]; - foreach ($data as $key => & $item) { + foreach ($this->data[$ns] as $key => $item) { if (substr($key, 0, $prefixL) === $prefix) { - unset($data[$key]); + unset($this->data[$ns][$key]); } } @@ -269,12 +267,11 @@ public function clearByTags(array $tags, $disjunction = false) } $tagCount = count($tags); - $data = & $this->data[$ns]; - foreach ($data as $key => & $item) { + foreach ($this->data[$ns] as $key => $item) { if (isset($item['tags'])) { $diff = array_diff($tags, $item['tags']); if (($disjunction && count($diff) < $tagCount) || (!$disjunction && !$diff)) { - unset($data[$key]); + unset($this->data[$ns][$key]); } } } @@ -293,13 +290,13 @@ public function clearByTags(array $tags, $disjunction = false) * @return mixed Data on success, null on failure * @throws Exception\ExceptionInterface */ - protected function internalGetItem(& $normalizedKey, & $success = null, & $casToken = null) + protected function internalGetItem($normalizedKey, & $success = null, & $casToken = null) { $options = $this->getOptions(); $ns = $options->getNamespace(); $success = isset($this->data[$ns][$normalizedKey]); if ($success) { - $data = & $this->data[$ns][$normalizedKey]; + $data = $this->data[$ns][$normalizedKey]; $ttl = $options->getTtl(); if ($ttl && microtime(true) >= ($data[1] + $ttl)) { $success = false; @@ -321,7 +318,7 @@ protected function internalGetItem(& $normalizedKey, & $success = null, & $casTo * @return array Associative array of keys and values * @throws Exception\ExceptionInterface */ - protected function internalGetItems(array & $normalizedKeys) + protected function internalGetItems(array $normalizedKeys) { $options = $this->getOptions(); $ns = $options->getNamespace(); @@ -329,7 +326,7 @@ protected function internalGetItems(array & $normalizedKeys) return []; } - $data = & $this->data[$ns]; + $data = $this->data[$ns]; $ttl = $options->getTtl(); $now = microtime(true); @@ -351,7 +348,7 @@ protected function internalGetItems(array & $normalizedKeys) * @param string $normalizedKey * @return bool */ - protected function internalHasItem(& $normalizedKey) + protected function internalHasItem($normalizedKey) { $options = $this->getOptions(); $ns = $options->getNamespace(); @@ -374,7 +371,7 @@ protected function internalHasItem(& $normalizedKey) * @param array $normalizedKeys * @return array Array of found keys */ - protected function internalHasItems(array & $normalizedKeys) + protected function internalHasItems(array $normalizedKeys) { $options = $this->getOptions(); $ns = $options->getNamespace(); @@ -382,7 +379,7 @@ protected function internalHasItems(array & $normalizedKeys) return []; } - $data = & $this->data[$ns]; + $data = $this->data[$ns]; $ttl = $options->getTtl(); $now = microtime(true); @@ -409,7 +406,7 @@ protected function internalHasItems(array & $normalizedKeys) * @triggers getMetadata.post(PostEvent) * @triggers getMetadata.exception(ExceptionEvent) */ - protected function internalGetMetadata(& $normalizedKey) + protected function internalGetMetadata($normalizedKey) { if (!$this->internalHasItem($normalizedKey)) { return false; @@ -431,7 +428,7 @@ protected function internalGetMetadata(& $normalizedKey) * @return bool * @throws Exception\ExceptionInterface */ - protected function internalSetItem(& $normalizedKey, & $value) + protected function internalSetItem($normalizedKey, $value) { $options = $this->getOptions(); @@ -455,7 +452,7 @@ protected function internalSetItem(& $normalizedKey, & $value) * @return array Array of not stored keys * @throws Exception\ExceptionInterface */ - protected function internalSetItems(array & $normalizedKeyValuePairs) + protected function internalSetItems(array $normalizedKeyValuePairs) { $options = $this->getOptions(); @@ -471,10 +468,9 @@ protected function internalSetItems(array & $normalizedKeyValuePairs) $this->data[$ns] = []; } - $data = & $this->data[$ns]; - $now = microtime(true); + $now = microtime(true); foreach ($normalizedKeyValuePairs as $normalizedKey => $value) { - $data[$normalizedKey] = [$value, $now]; + $this->data[$ns][$normalizedKey] = [$value, $now]; } return []; @@ -488,7 +484,7 @@ protected function internalSetItems(array & $normalizedKeyValuePairs) * @return bool * @throws Exception\ExceptionInterface */ - protected function internalAddItem(& $normalizedKey, & $value) + protected function internalAddItem($normalizedKey, $value) { $options = $this->getOptions(); @@ -515,7 +511,7 @@ protected function internalAddItem(& $normalizedKey, & $value) * @return array Array of not stored keys * @throws Exception\ExceptionInterface */ - protected function internalAddItems(array & $normalizedKeyValuePairs) + protected function internalAddItems(array $normalizedKeyValuePairs) { $options = $this->getOptions(); @@ -532,13 +528,12 @@ protected function internalAddItems(array & $normalizedKeyValuePairs) } $result = []; - $data = & $this->data[$ns]; $now = microtime(true); foreach ($normalizedKeyValuePairs as $normalizedKey => $value) { - if (isset($data[$normalizedKey])) { + if (isset($this->data[$ns][$normalizedKey])) { $result[] = $normalizedKey; } else { - $data[$normalizedKey] = [$value, $now]; + $this->data[$ns][$normalizedKey] = [$value, $now]; } } @@ -553,7 +548,7 @@ protected function internalAddItems(array & $normalizedKeyValuePairs) * @return bool * @throws Exception\ExceptionInterface */ - protected function internalReplaceItem(& $normalizedKey, & $value) + protected function internalReplaceItem($normalizedKey, $value) { $ns = $this->getOptions()->getNamespace(); if (!isset($this->data[$ns][$normalizedKey])) { @@ -571,7 +566,7 @@ protected function internalReplaceItem(& $normalizedKey, & $value) * @return array Array of not stored keys * @throws Exception\ExceptionInterface */ - protected function internalReplaceItems(array & $normalizedKeyValuePairs) + protected function internalReplaceItems(array $normalizedKeyValuePairs) { $ns = $this->getOptions()->getNamespace(); if (!isset($this->data[$ns])) { @@ -579,12 +574,11 @@ protected function internalReplaceItems(array & $normalizedKeyValuePairs) } $result = []; - $data = & $this->data[$ns]; foreach ($normalizedKeyValuePairs as $normalizedKey => $value) { - if (!isset($data[$normalizedKey])) { + if (!isset($this->data[$ns][$normalizedKey])) { $result[] = $normalizedKey; } else { - $data[$normalizedKey] = [$value, microtime(true)]; + $this->data[$ns][$normalizedKey] = [$value, microtime(true)]; } } @@ -598,7 +592,7 @@ protected function internalReplaceItems(array & $normalizedKeyValuePairs) * @return bool * @throws Exception\ExceptionInterface */ - protected function internalTouchItem(& $normalizedKey) + protected function internalTouchItem($normalizedKey) { $ns = $this->getOptions()->getNamespace(); @@ -617,7 +611,7 @@ protected function internalTouchItem(& $normalizedKey) * @return bool * @throws Exception\ExceptionInterface */ - protected function internalRemoveItem(& $normalizedKey) + protected function internalRemoveItem($normalizedKey) { $ns = $this->getOptions()->getNamespace(); if (!isset($this->data[$ns][$normalizedKey])) { @@ -642,14 +636,13 @@ protected function internalRemoveItem(& $normalizedKey) * @return int|bool The new value on success, false on failure * @throws Exception\ExceptionInterface */ - protected function internalIncrementItem(& $normalizedKey, & $value) + protected function internalIncrementItem($normalizedKey, $value) { $ns = $this->getOptions()->getNamespace(); if (isset($this->data[$ns][$normalizedKey])) { - $data = & $this->data[$ns][$normalizedKey]; - $data[0]+= $value; - $data[1] = microtime(true); - $newValue = $data[0]; + $this->data[$ns][$normalizedKey][0] += $value; + $this->data[$ns][$normalizedKey][1] = microtime(true); + $newValue = $this->data[$ns][$normalizedKey][0]; } else { // initial value $newValue = $value; @@ -667,14 +660,13 @@ protected function internalIncrementItem(& $normalizedKey, & $value) * @return int|bool The new value on success, false on failure * @throws Exception\ExceptionInterface */ - protected function internalDecrementItem(& $normalizedKey, & $value) + protected function internalDecrementItem($normalizedKey, $value) { $ns = $this->getOptions()->getNamespace(); if (isset($this->data[$ns][$normalizedKey])) { - $data = & $this->data[$ns][$normalizedKey]; - $data[0]-= $value; - $data[1] = microtime(true); - $newValue = $data[0]; + $this->data[$ns][$normalizedKey][0] -= $value; + $this->data[$ns][$normalizedKey][1] = microtime(true); + $newValue = $this->data[$ns][$normalizedKey][0]; } else { // initial value $newValue = -$value; diff --git a/src/Storage/Adapter/MongoDb.php b/src/Storage/Adapter/MongoDb.php index 415ece9d1..afcbadb01 100644 --- a/src/Storage/Adapter/MongoDb.php +++ b/src/Storage/Adapter/MongoDb.php @@ -114,7 +114,7 @@ public function getOptions() * * @throws Exception\RuntimeException */ - protected function internalGetItem(& $normalizedKey, & $success = null, & $casToken = null) + protected function internalGetItem($normalizedKey, & $success = null, & $casToken = null) { $result = $this->fetchFromCollection($normalizedKey); $success = false; @@ -158,7 +158,7 @@ protected function internalGetItem(& $normalizedKey, & $success = null, & $casTo * * @throws Exception\RuntimeException */ - protected function internalSetItem(& $normalizedKey, & $value) + protected function internalSetItem($normalizedKey, $value) { $mongo = $this->getMongoDbResource(); $key = $this->namespacePrefix . $normalizedKey; @@ -191,7 +191,7 @@ protected function internalSetItem(& $normalizedKey, & $value) * * @throws Exception\RuntimeException */ - protected function internalRemoveItem(& $normalizedKey) + protected function internalRemoveItem($normalizedKey) { try { $result = $this->getMongoDbResource()->remove(['key' => $this->namespacePrefix . $normalizedKey]); @@ -253,7 +253,7 @@ protected function internalGetCapabilities() * * @throws Exception\ExceptionInterface */ - protected function internalGetMetadata(& $normalizedKey) + protected function internalGetMetadata($normalizedKey) { $result = $this->fetchFromCollection($normalizedKey); @@ -269,7 +269,7 @@ protected function internalGetMetadata(& $normalizedKey) * * @throws Exception\RuntimeException */ - private function fetchFromCollection(& $normalizedKey) + private function fetchFromCollection($normalizedKey) { try { return $this->getMongoDbResource()->findOne(['key' => $this->namespacePrefix . $normalizedKey]); diff --git a/src/Storage/Adapter/Redis.php b/src/Storage/Adapter/Redis.php index 93770af62..5addfed16 100644 --- a/src/Storage/Adapter/Redis.php +++ b/src/Storage/Adapter/Redis.php @@ -144,7 +144,7 @@ public function getOptions() * @return mixed Data on success, false on key not found * @throws Exception\RuntimeException */ - protected function internalGetItem(& $normalizedKey, & $success = null, & $casToken = null) + protected function internalGetItem($normalizedKey, & $success = null, & $casToken = null) { $redis = $this->getRedisResource(); try { @@ -171,7 +171,7 @@ protected function internalGetItem(& $normalizedKey, & $success = null, & $casTo * @return array Associative array of keys and values * @throws Exception\RuntimeException */ - protected function internalGetItems(array & $normalizedKeys) + protected function internalGetItems(array $normalizedKeys) { $redis = $this->getRedisResource(); @@ -202,7 +202,7 @@ function ($value) { * @return bool * @throws Exception\RuntimeException */ - protected function internalHasItem(& $normalizedKey) + protected function internalHasItem($normalizedKey) { $redis = $this->getRedisResource(); try { @@ -221,7 +221,7 @@ protected function internalHasItem(& $normalizedKey) * @return bool * @throws Exception\RuntimeException */ - protected function internalSetItem(& $normalizedKey, & $value) + protected function internalSetItem($normalizedKey, $value) { $redis = $this->getRedisResource(); $options = $this->getOptions(); @@ -251,7 +251,7 @@ protected function internalSetItem(& $normalizedKey, & $value) * @return array Array of not stored keys * @throws Exception\RuntimeException */ - protected function internalSetItems(array & $normalizedKeyValuePairs) + protected function internalSetItems(array $normalizedKeyValuePairs) { $redis = $this->getRedisResource(); $options = $this->getOptions(); @@ -295,7 +295,7 @@ protected function internalSetItems(array & $normalizedKeyValuePairs) * @return bool * @throws Exception\RuntimeException */ - protected function internalAddItem(& $normalizedKey, & $value) + protected function internalAddItem($normalizedKey, $value) { $redis = $this->getRedisResource(); $options = $this->getOptions(); @@ -333,7 +333,7 @@ protected function internalAddItem(& $normalizedKey, & $value) * @return bool * @throws Exception\RuntimeException */ - protected function internalTouchItem(& $normalizedKey) + protected function internalTouchItem($normalizedKey) { $redis = $this->getRedisResource(); try { @@ -352,7 +352,7 @@ protected function internalTouchItem(& $normalizedKey) * @return bool * @throws Exception\RuntimeException */ - protected function internalRemoveItem(& $normalizedKey) + protected function internalRemoveItem($normalizedKey) { $redis = $this->getRedisResource(); try { @@ -370,7 +370,7 @@ protected function internalRemoveItem(& $normalizedKey) * @return int|bool The new value on success, false on failure * @throws Exception\RuntimeException */ - protected function internalIncrementItem(& $normalizedKey, & $value) + protected function internalIncrementItem($normalizedKey, $value) { $redis = $this->getRedisResource(); try { @@ -388,7 +388,7 @@ protected function internalIncrementItem(& $normalizedKey, & $value) * @return int|bool The new value on success, false on failure * @throws Exception\RuntimeException */ - protected function internalDecrementItem(& $normalizedKey, & $value) + protected function internalDecrementItem($normalizedKey, $value) { $redis = $this->getRedisResource(); try { @@ -547,7 +547,7 @@ protected function internalGetCapabilities() * * @throws Exception\ExceptionInterface */ - protected function internalGetMetadata(& $normalizedKey) + protected function internalGetMetadata($normalizedKey) { $redis = $this->getRedisResource(); $metadata = []; diff --git a/src/Storage/Adapter/RedisResourceManager.php b/src/Storage/Adapter/RedisResourceManager.php index 6573850ab..53a2cf58b 100644 --- a/src/Storage/Adapter/RedisResourceManager.php +++ b/src/Storage/Adapter/RedisResourceManager.php @@ -79,7 +79,7 @@ public function getDatabase($id) throw new Exception\RuntimeException("No resource with id '{$id}'"); } - $resource = & $this->resources[$id]; + $resource = $this->resources[$id]; return $resource['database']; } @@ -95,7 +95,7 @@ public function getPassword($id) throw new Exception\RuntimeException("No resource with id '{$id}'"); } - $resource = & $this->resources[$id]; + $resource = $this->resources[$id]; return $resource['password']; } @@ -103,7 +103,7 @@ public function getPassword($id) * Gets a redis resource * * @param string $id - * @return RedisResourceManager + * @return RedisResource * @throws Exception\RuntimeException */ public function getResource($id) @@ -112,30 +112,50 @@ public function getResource($id) throw new Exception\RuntimeException("No resource with id '{$id}'"); } - $resource = & $this->resources[$id]; - if ($resource['resource'] instanceof RedisResource) { - //in case new server was set then connect - if (!$resource['initialized']) { - $this->connect($resource); + // initialize the redis instance and connection if not already done + if (!$this->resources[$id]['initialized']) { + + // create a new instance if we don't have it + if (!$this->resources[$id]['resource']) { + $this->resources[$id]['resource'] = new RedisResource(); + } + + $resource = $this->resources[$id]; + $server = $resource['server']; + $redis = $resource['resource']; + + if ($resource['persistent_id'] !== '') { + //connect or reuse persistent connection + $success = $redis->pconnect($server['host'], $server['port'], $server['timeout'], $resource['persistent_id']); + } elseif ($server['port']) { + $success = $redis->connect($server['host'], $server['port'], $server['timeout']); + } elseif ($server['timeout']) { + //connect through unix domain socket + $success = $redis->connect($server['host'], $server['timeout']); + } else { + $success = $redis->connect($server['host']); + } + + if (!$success) { + throw new Exception\RuntimeException('Could not estabilish connection with Redis instance'); + } + + foreach ($resource['lib_options'] as $k => $v) { + $redis->setOption($k, $v); } - $info = $resource['resource']->info(); - $resource['version'] = $info['redis_version']; - return $resource['resource']; - } - $redis = new RedisResource(); + if ($resource['password']) { + $redis->auth($resource['password']); + } - $resource['resource'] = $redis; - $this->connect($resource); + $redis->select($resource['database']); - foreach ($resource['lib_options'] as $k => $v) { - $redis->setOption($k, $v); + $info = $redis->info(); + $this->resources[$id]['version'] = $info['redis_version']; + $this->resources[$id]['initialized'] = true; } - $info = $redis->info(); - $resource['version'] = $info['redis_version']; - $this->resources[$id]['resource'] = $redis; - return $redis; + return $this->resources[$id]['resource']; } /** @@ -150,7 +170,7 @@ public function getServer($id) throw new Exception\RuntimeException("No resource with id '{$id}'"); } - $resource = & $this->resources[$id]; + $resource = $this->resources[$id]; return $resource['server']; } @@ -191,7 +211,7 @@ protected function normalizeServer(&$server) // parse server from URI host{:?port} $server = trim($server); if (strpos($server, '/') !== 0) { - //non unix domain socket connection + // non unix domain socket connection $server = parse_url($server); } else { $server = ['host' => $server]; @@ -247,42 +267,6 @@ protected function extractPassword($resource, $serverUri) return isset($server['pass']) ? $server['pass'] : null; } - /** - * Connects to redis server - * - * - * @param array & $resource - * - * @return null - * @throws Exception\RuntimeException - */ - protected function connect(array & $resource) - { - $server = $resource['server']; - $redis = $resource['resource']; - if ($resource['persistent_id'] !== '') { - //connect or reuse persistent connection - $success = $redis->pconnect($server['host'], $server['port'], $server['timeout'], $resource['persistent_id']); - } elseif ($server['port']) { - $success = $redis->connect($server['host'], $server['port'], $server['timeout']); - } elseif ($server['timeout']) { - //connect through unix domain socket - $success = $redis->connect($server['host'], $server['timeout']); - } else { - $success = $redis->connect($server['host']); - } - - if (!$success) { - throw new Exception\RuntimeException('Could not estabilish connection with Redis instance'); - } - - $resource['initialized'] = true; - if ($resource['password']) { - $redis->auth($resource['password']); - } - $redis->select($resource['database']); - } - /** * Set a resource * @@ -293,17 +277,18 @@ protected function connect(array & $resource) public function setResource($id, $resource) { $id = (string) $id; - //TODO: how to get back redis connection info from resource? + $defaults = [ + 'resource' => null, 'persistent_id' => '', 'lib_options' => [], 'server' => [], 'password' => '', 'database' => 0, - 'resource' => null, - 'initialized' => false, 'version' => 0, + 'initialized' => false, ]; + if (!$resource instanceof RedisResource) { if ($resource instanceof Traversable) { $resource = ArrayUtils::iteratorToArray($resource); @@ -313,10 +298,12 @@ public function setResource($id, $resource) ); } + // set missing options to default $resource = array_merge($defaults, $resource); + // normalize and validate params - $this->normalizePersistentId($resource['persistent_id']); - $this->normalizeLibOptions($resource['lib_options']); + $resource['persistent_id'] = $this->normalizePersistentId($resource['persistent_id']); + $resource['lib_options'] = $this->normalizeLibOptions($resource['lib_options']); // #6495 note: order is important here, as `normalizeServer` applies destructive // transformations on $resource['server'] @@ -324,19 +311,16 @@ public function setResource($id, $resource) $this->normalizeServer($resource['server']); } else { - //there are two ways of determining if redis is already initialized - //with connect function: - //1) pinging server - //2) checking undocumented property socket which is available only - //after successful connect - $resource = array_merge( - $defaults, - [ - 'resource' => $resource, - 'initialized' => isset($resource->socket), - ] - ); + // there are two ways of determining if redis is already initialized with connect function: + // 1) ping server + // 2) check undocumented property 'socket' which is available only after successful connect + // TODO: how to get back redis connection info from resource? + $resource = array_merge($defaults, [ + 'resource' => $resource, + 'initialized' => isset($resource->socket), + ]); } + $this->resources[$id] = $resource; return $this; } @@ -369,15 +353,13 @@ public function setPersistentId($id, $persistentId) ]); } - $resource = & $this->resources[$id]; - if ($resource instanceof RedisResource) { + if ($this->resources[$id] instanceof RedisResource) { throw new Exception\RuntimeException( "Can't change persistent id of resource {$id} after instanziation" ); } - $this->normalizePersistentId($persistentId); - $resource['persistent_id'] = $persistentId; + $this->resources[$id]['persistent_id'] = $this->normalizePersistentId($persistentId); return $this; } @@ -395,25 +377,24 @@ public function getPersistentId($id) throw new Exception\RuntimeException("No resource with id '{$id}'"); } - $resource = & $this->resources[$id]; - - if ($resource instanceof RedisResource) { + if ($this->resources[$id] instanceof RedisResource) { throw new Exception\RuntimeException( "Can't get persistent id of an instantiated redis resource" ); } - return $resource['persistent_id']; + return $this->resources[$id]['persistent_id']; } /** * Normalize the persistent id * * @param string $persistentId + * @return string */ protected function normalizePersistentId(& $persistentId) { - $persistentId = (string) $persistentId; + return (string) $persistentId; } /** @@ -431,13 +412,11 @@ public function setLibOptions($id, array $libOptions) ]); } - $this->normalizeLibOptions($libOptions); - $resource = & $this->resources[$id]; - - $resource['lib_options'] = $libOptions; + $libOptions = $this->normalizeLibOptions($libOptions); + $this->resources[$id]['lib_options'] = $libOptions; - if ($resource['resource'] instanceof RedisResource) { - $redis = & $resource['resource']; + if ($this->resources[$id]['resource'] instanceof RedisResource) { + $redis = $this->resources[$id]['resource']; if (method_exists($redis, 'setOptions')) { $redis->setOptions($libOptions); } else { @@ -463,9 +442,7 @@ public function getLibOptions($id) throw new Exception\RuntimeException("No resource with id '{$id}'"); } - $resource = & $this->resources[$id]; - - if ($resource instanceof RedisResource) { + if ($this->resources[$id] instanceof RedisResource) { $libOptions = []; $reflection = new ReflectionClass('Redis'); $constants = $reflection->getConstants(); @@ -476,7 +453,8 @@ public function getLibOptions($id) } return $libOptions; } - return $resource['lib_options']; + + return $this->resources[$id]['lib_options']; } /** @@ -506,23 +484,23 @@ public function getLibOption($id, $key) throw new Exception\RuntimeException("No resource with id '{$id}'"); } - $this->normalizeLibOptionKey($key); - $resource = & $this->resources[$id]; + $key = $this->normalizeLibOptionKey($key); - if ($resource instanceof RedisResource) { + if ($this->resources[$id] instanceof RedisResource) { return $resource->getOption($key); } - return isset($resource['lib_options'][$key]) ? $resource['lib_options'][$key] : null; + return isset($this->resources[$id]['lib_options'][$key]) ? $this->resources[$id]['lib_options'][$key] : null; } /** * Normalize Redis options * * @param array|Traversable $libOptions + * @return array * @throws Exception\InvalidArgumentException */ - protected function normalizeLibOptions(& $libOptions) + protected function normalizeLibOptions($libOptions) { if (!is_array($libOptions) && !($libOptions instanceof Traversable)) { throw new Exception\InvalidArgumentException( @@ -532,20 +510,21 @@ protected function normalizeLibOptions(& $libOptions) $result = []; foreach ($libOptions as $key => $value) { - $this->normalizeLibOptionKey($key); + $key = $this->normalizeLibOptionKey($key); $result[$key] = $value; } - $libOptions = $result; + return $result; } /** * Convert option name into it's constant value * * @param string|int $key + * @return string * @throws Exception\InvalidArgumentException */ - protected function normalizeLibOptionKey(& $key) + protected function normalizeLibOptionKey($key) { // convert option name into it's constant value if (is_string($key)) { @@ -553,10 +532,10 @@ protected function normalizeLibOptionKey(& $key) if (!defined($const)) { throw new Exception\InvalidArgumentException("Unknown redis option '{$key}' ({$const})"); } - $key = constant($const); - } else { - $key = (int) $key; + return constant($const); } + + return (int) $key; } /** @@ -614,9 +593,9 @@ public function setPassword($id, $password) ]); } - $resource = & $this->resources[$id]; - $resource['password'] = $password; - $resource['initialized'] = false; + $this->resources[$id]['password'] = $password; + $this->resources[$id]['initialized'] = false; + return $this; } @@ -642,7 +621,8 @@ public function setDatabase($id, $database) $resource['resource']->select($database); } - $resource['database'] = $database; + $resource['database'] = $database; + $resource['initialized'] = false; return $this; } diff --git a/src/Storage/Adapter/Session.php b/src/Storage/Adapter/Session.php index 38dfa59bc..449d88133 100644 --- a/src/Storage/Adapter/Session.php +++ b/src/Storage/Adapter/Session.php @@ -124,7 +124,7 @@ public function clearByPrefix($prefix) $data = $cntr->offsetGet($ns); $prefixL = strlen($prefix); - foreach ($data as $key => & $item) { + foreach ($data as $key => $item) { if (substr($key, 0, $prefixL) === $prefix) { unset($data[$key]); } @@ -145,7 +145,7 @@ public function clearByPrefix($prefix) * @return mixed Data on success, null on failure * @throws Exception\ExceptionInterface */ - protected function internalGetItem(& $normalizedKey, & $success = null, & $casToken = null) + protected function internalGetItem($normalizedKey, & $success = null, & $casToken = null) { $cntr = $this->getSessionContainer(); $ns = $this->getOptions()->getNamespace(); @@ -172,7 +172,7 @@ protected function internalGetItem(& $normalizedKey, & $success = null, & $casTo * @return array Associative array of keys and values * @throws Exception\ExceptionInterface */ - protected function internalGetItems(array & $normalizedKeys) + protected function internalGetItems(array $normalizedKeys) { $cntr = $this->getSessionContainer(); $ns = $this->getOptions()->getNamespace(); @@ -198,7 +198,7 @@ protected function internalGetItems(array & $normalizedKeys) * @param string $normalizedKey * @return bool */ - protected function internalHasItem(& $normalizedKey) + protected function internalHasItem($normalizedKey) { $cntr = $this->getSessionContainer(); $ns = $this->getOptions()->getNamespace(); @@ -217,7 +217,7 @@ protected function internalHasItem(& $normalizedKey) * @param array $normalizedKeys * @return array Array of found keys */ - protected function internalHasItems(array & $normalizedKeys) + protected function internalHasItems(array $normalizedKeys) { $cntr = $this->getSessionContainer(); $ns = $this->getOptions()->getNamespace(); @@ -248,7 +248,7 @@ protected function internalHasItems(array & $normalizedKeys) * @triggers getMetadata.post(PostEvent) * @triggers getMetadata.exception(ExceptionEvent) */ - protected function internalGetMetadata(& $normalizedKey) + protected function internalGetMetadata($normalizedKey) { return $this->internalHasItem($normalizedKey) ? [] : false; } @@ -263,7 +263,7 @@ protected function internalGetMetadata(& $normalizedKey) * @return bool * @throws Exception\ExceptionInterface */ - protected function internalSetItem(& $normalizedKey, & $value) + protected function internalSetItem($normalizedKey, $value) { $cntr = $this->getSessionContainer(); $ns = $this->getOptions()->getNamespace(); @@ -280,7 +280,7 @@ protected function internalSetItem(& $normalizedKey, & $value) * @return array Array of not stored keys * @throws Exception\ExceptionInterface */ - protected function internalSetItems(array & $normalizedKeyValuePairs) + protected function internalSetItems(array $normalizedKeyValuePairs) { $cntr = $this->getSessionContainer(); $ns = $this->getOptions()->getNamespace(); @@ -303,7 +303,7 @@ protected function internalSetItems(array & $normalizedKeyValuePairs) * @return bool * @throws Exception\ExceptionInterface */ - protected function internalAddItem(& $normalizedKey, & $value) + protected function internalAddItem($normalizedKey, $value) { $cntr = $this->getSessionContainer(); $ns = $this->getOptions()->getNamespace(); @@ -331,7 +331,7 @@ protected function internalAddItem(& $normalizedKey, & $value) * @return array Array of not stored keys * @throws Exception\ExceptionInterface */ - protected function internalAddItems(array & $normalizedKeyValuePairs) + protected function internalAddItems(array $normalizedKeyValuePairs) { $cntr = $this->getSessionContainer(); $ns = $this->getOptions()->getNamespace(); @@ -363,7 +363,7 @@ protected function internalAddItems(array & $normalizedKeyValuePairs) * @return bool * @throws Exception\ExceptionInterface */ - protected function internalReplaceItem(& $normalizedKey, & $value) + protected function internalReplaceItem($normalizedKey, $value) { $cntr = $this->getSessionContainer(); $ns = $this->getOptions()->getNamespace(); @@ -389,7 +389,7 @@ protected function internalReplaceItem(& $normalizedKey, & $value) * @return array Array of not stored keys * @throws Exception\ExceptionInterface */ - protected function internalReplaceItems(array & $normalizedKeyValuePairs) + protected function internalReplaceItems(array $normalizedKeyValuePairs) { $cntr = $this->getSessionContainer(); $ns = $this->getOptions()->getNamespace(); @@ -418,7 +418,7 @@ protected function internalReplaceItems(array & $normalizedKeyValuePairs) * @return bool * @throws Exception\ExceptionInterface */ - protected function internalRemoveItem(& $normalizedKey) + protected function internalRemoveItem($normalizedKey) { $cntr = $this->getSessionContainer(); $ns = $this->getOptions()->getNamespace(); @@ -451,7 +451,7 @@ protected function internalRemoveItem(& $normalizedKey) * @return int|bool The new value on success, false on failure * @throws Exception\ExceptionInterface */ - protected function internalIncrementItem(& $normalizedKey, & $value) + protected function internalIncrementItem($normalizedKey, $value) { $cntr = $this->getSessionContainer(); $ns = $this->getOptions()->getNamespace(); @@ -483,7 +483,7 @@ protected function internalIncrementItem(& $normalizedKey, & $value) * @return int|bool The new value on success, false on failure * @throws Exception\ExceptionInterface */ - protected function internalDecrementItem(& $normalizedKey, & $value) + protected function internalDecrementItem($normalizedKey, $value) { $cntr = $this->getSessionContainer(); $ns = $this->getOptions()->getNamespace(); diff --git a/src/Storage/Adapter/WinCache.php b/src/Storage/Adapter/WinCache.php index 93621c8d5..e624aef90 100644 --- a/src/Storage/Adapter/WinCache.php +++ b/src/Storage/Adapter/WinCache.php @@ -129,7 +129,7 @@ public function flush() * @return mixed Data on success, null on failure * @throws Exception\ExceptionInterface */ - protected function internalGetItem(& $normalizedKey, & $success = null, & $casToken = null) + protected function internalGetItem($normalizedKey, & $success = null, & $casToken = null) { $options = $this->getOptions(); $namespace = $options->getNamespace(); @@ -153,7 +153,7 @@ protected function internalGetItem(& $normalizedKey, & $success = null, & $casTo * @return array Associative array of keys and values * @throws Exception\ExceptionInterface */ - protected function internalGetItems(array & $normalizedKeys) + protected function internalGetItems(array $normalizedKeys) { $options = $this->getOptions(); $namespace = $options->getNamespace(); @@ -172,8 +172,8 @@ protected function internalGetItems(array & $normalizedKeys) // remove namespace prefix $prefixL = strlen($prefix); $result = []; - foreach ($fetch as $internalKey => & $value) { - $result[substr($internalKey, $prefixL)] = & $value; + foreach ($fetch as $internalKey => $value) { + $result[substr($internalKey, $prefixL)] = $value; } return $result; @@ -186,7 +186,7 @@ protected function internalGetItems(array & $normalizedKeys) * @return bool * @throws Exception\ExceptionInterface */ - protected function internalHasItem(& $normalizedKey) + protected function internalHasItem($normalizedKey) { $options = $this->getOptions(); $namespace = $options->getNamespace(); @@ -201,7 +201,7 @@ protected function internalHasItem(& $normalizedKey) * @return array|bool Metadata on success, false on failure * @throws Exception\ExceptionInterface */ - protected function internalGetMetadata(& $normalizedKey) + protected function internalGetMetadata($normalizedKey) { $options = $this->getOptions(); $namespace = $options->getNamespace(); @@ -210,9 +210,7 @@ protected function internalGetMetadata(& $normalizedKey) $info = wincache_ucache_info(true, $internalKey); if (isset($info['ucache_entries'][1])) { - $metadata = $info['ucache_entries'][1]; - $this->normalizeMetadata($metadata); - return $metadata; + return $this->normalizeMetadata($info['ucache_entries'][1]); } return false; @@ -228,7 +226,7 @@ protected function internalGetMetadata(& $normalizedKey) * @return bool * @throws Exception\ExceptionInterface */ - protected function internalSetItem(& $normalizedKey, & $value) + protected function internalSetItem($normalizedKey, $value) { $options = $this->getOptions(); $namespace = $options->getNamespace(); @@ -253,7 +251,7 @@ protected function internalSetItem(& $normalizedKey, & $value) * @return array Array of not stored keys * @throws Exception\ExceptionInterface */ - protected function internalSetItems(array & $normalizedKeyValuePairs) + protected function internalSetItems(array $normalizedKeyValuePairs) { $options = $this->getOptions(); $namespace = $options->getNamespace(); @@ -263,9 +261,9 @@ protected function internalSetItems(array & $normalizedKeyValuePairs) $prefix = $namespace . $options->getNamespaceSeparator(); $internalKeyValuePairs = []; - foreach ($normalizedKeyValuePairs as $normalizedKey => & $value) { + foreach ($normalizedKeyValuePairs as $normalizedKey => $value) { $internalKey = $prefix . $normalizedKey; - $internalKeyValuePairs[$internalKey] = & $value; + $internalKeyValuePairs[$internalKey] = $value; } $result = wincache_ucache_set($internalKeyValuePairs, null, $options->getTtl()); @@ -287,7 +285,7 @@ protected function internalSetItems(array & $normalizedKeyValuePairs) * @return bool * @throws Exception\ExceptionInterface */ - protected function internalAddItem(& $normalizedKey, & $value) + protected function internalAddItem($normalizedKey, $value) { $options = $this->getOptions(); $namespace = $options->getNamespace(); @@ -312,7 +310,7 @@ protected function internalAddItem(& $normalizedKey, & $value) * @return array Array of not stored keys * @throws Exception\ExceptionInterface */ - protected function internalAddItems(array & $normalizedKeyValuePairs) + protected function internalAddItems(array $normalizedKeyValuePairs) { $options = $this->getOptions(); $namespace = $options->getNamespace(); @@ -346,7 +344,7 @@ protected function internalAddItems(array & $normalizedKeyValuePairs) * @return bool * @throws Exception\ExceptionInterface */ - protected function internalReplaceItem(& $normalizedKey, & $value) + protected function internalReplaceItem($normalizedKey, $value) { $options = $this->getOptions(); $namespace = $options->getNamespace(); @@ -374,7 +372,7 @@ protected function internalReplaceItem(& $normalizedKey, & $value) * @return bool * @throws Exception\ExceptionInterface */ - protected function internalRemoveItem(& $normalizedKey) + protected function internalRemoveItem($normalizedKey) { $options = $this->getOptions(); $namespace = $options->getNamespace(); @@ -390,7 +388,7 @@ protected function internalRemoveItem(& $normalizedKey) * @return array Array of not removed keys * @throws Exception\ExceptionInterface */ - protected function internalRemoveItems(array & $normalizedKeys) + protected function internalRemoveItems(array $normalizedKeys) { $options = $this->getOptions(); $namespace = $options->getNamespace(); @@ -427,7 +425,7 @@ protected function internalRemoveItems(array & $normalizedKeys) * @return int|bool The new value on success, false on failure * @throws Exception\ExceptionInterface */ - protected function internalIncrementItem(& $normalizedKey, & $value) + protected function internalIncrementItem($normalizedKey, $value) { $options = $this->getOptions(); $namespace = $options->getNamespace(); @@ -444,7 +442,7 @@ protected function internalIncrementItem(& $normalizedKey, & $value) * @return int|bool The new value on success, false on failure * @throws Exception\ExceptionInterface */ - protected function internalDecrementItem(& $normalizedKey, & $value) + protected function internalDecrementItem($normalizedKey, $value) { $options = $this->getOptions(); $namespace = $options->getNamespace(); @@ -514,20 +512,15 @@ protected function internalGetCapabilities() * Normalize metadata to work with WinCache * * @param array $metadata - * @return void + * @return array */ - protected function normalizeMetadata(array & $metadata) + protected function normalizeMetadata(array $metadata) { - $metadata['internal_key'] = $metadata['key_name']; - $metadata['hits'] = $metadata['hitcount']; - $metadata['ttl'] = $metadata['ttl_seconds']; - $metadata['size'] = $metadata['value_size']; - - unset( - $metadata['key_name'], - $metadata['hitcount'], - $metadata['ttl_seconds'], - $metadata['value_size'] - ); + return [ + 'internal_key' => $metadata['key_name'], + 'ttl' => $metadata['ttl_seconds'], + 'hits' => $metadata['hitcount'], + 'size' => $metadata['value_size'], + ]; } } diff --git a/src/Storage/Adapter/XCache.php b/src/Storage/Adapter/XCache.php index 6d613f0f0..5afc2ca92 100644 --- a/src/Storage/Adapter/XCache.php +++ b/src/Storage/Adapter/XCache.php @@ -230,7 +230,7 @@ public function getIterator() $cnt = xcache_count(XC_TYPE_VAR); for ($i=0; $i < $cnt; $i++) { $list = xcache_list(XC_TYPE_VAR, $i); - foreach ($list['cache_list'] as & $item) { + foreach ($list['cache_list'] as $item) { $keys[] = $item['name']; } } @@ -241,7 +241,7 @@ public function getIterator() $cnt = xcache_count(XC_TYPE_VAR); for ($i=0; $i < $cnt; $i++) { $list = xcache_list(XC_TYPE_VAR, $i); - foreach ($list['cache_list'] as & $item) { + foreach ($list['cache_list'] as $item) { $keys[] = substr($item['name'], $prefixL); } } @@ -263,7 +263,7 @@ public function getIterator() * @return mixed Data on success, null on failure * @throws Exception\ExceptionInterface */ - protected function internalGetItem(& $normalizedKey, & $success = null, & $casToken = null) + protected function internalGetItem($normalizedKey, & $success = null, & $casToken = null) { $options = $this->getOptions(); $namespace = $options->getNamespace(); @@ -287,7 +287,7 @@ protected function internalGetItem(& $normalizedKey, & $success = null, & $casTo * @return bool * @throws Exception\ExceptionInterface */ - protected function internalHasItem(& $normalizedKey) + protected function internalHasItem($normalizedKey) { $options = $this->getOptions(); $namespace = $options->getNamespace(); @@ -302,7 +302,7 @@ protected function internalHasItem(& $normalizedKey) * @return array|bool Metadata on success, false on failure * @throws Exception\ExceptionInterface */ - protected function internalGetMetadata(& $normalizedKey) + protected function internalGetMetadata($normalizedKey) { $options = $this->getOptions(); $namespace = $options->getNamespace(); @@ -314,10 +314,9 @@ protected function internalGetMetadata(& $normalizedKey) $cnt = xcache_count(XC_TYPE_VAR); for ($i=0; $i < $cnt; $i++) { $list = xcache_list(XC_TYPE_VAR, $i); - foreach ($list['cache_list'] as & $metadata) { + foreach ($list['cache_list'] as $metadata) { if ($metadata['name'] === $internalKey) { - $this->normalizeMetadata($metadata); - return $metadata; + return $this->normalizeMetadata($metadata); } } } @@ -337,7 +336,7 @@ protected function internalGetMetadata(& $normalizedKey) * @return bool * @throws Exception\ExceptionInterface */ - protected function internalSetItem(& $normalizedKey, & $value) + protected function internalSetItem($normalizedKey, $value) { $options = $this->getOptions(); $namespace = $options->getNamespace(); @@ -369,7 +368,7 @@ protected function internalSetItem(& $normalizedKey, & $value) * @return bool * @throws Exception\ExceptionInterface */ - protected function internalRemoveItem(& $normalizedKey) + protected function internalRemoveItem($normalizedKey) { $options = $this->getOptions(); $namespace = $options->getNamespace(); @@ -387,7 +386,7 @@ protected function internalRemoveItem(& $normalizedKey) * @return int|bool The new value on success, false on failure * @throws Exception\ExceptionInterface */ - protected function internalIncrementItem(& $normalizedKey, & $value) + protected function internalIncrementItem($normalizedKey, $value) { $options = $this->getOptions(); $namespace = $options->getNamespace(); @@ -407,7 +406,7 @@ protected function internalIncrementItem(& $normalizedKey, & $value) * @return int|bool The new value on success, false on failure * @throws Exception\ExceptionInterface */ - protected function internalDecrementItem(& $normalizedKey, & $value) + protected function internalDecrementItem($normalizedKey, $value) { $options = $this->getOptions(); $namespace = $options->getNamespace(); @@ -522,10 +521,18 @@ protected function resetAdminAuth() * Normalize metadata to work with XCache * * @param array $metadata + * @return array */ - protected function normalizeMetadata(array & $metadata) + protected function normalizeMetadata(array $metadata) { - $metadata['internal_key'] = &$metadata['name']; - unset($metadata['name']); + return [ + 'internal_key' => $metadata['name'], + 'size' => $metadata['size'], + 'refcount' => $metadata['refcount'], + 'hits' => $metadata['hits'], + 'ctime' => $metadata['ctime'], + 'atime' => $metadata['atime'], + 'hvalue' => $metadata['hvalue'], + ]; } } diff --git a/src/Storage/Capabilities.php b/src/Storage/Capabilities.php index 56f8aa27b..de34ac916 100644 --- a/src/Storage/Capabilities.php +++ b/src/Storage/Capabilities.php @@ -568,9 +568,7 @@ protected function setCapability(stdClass $marker, $property, $value) // trigger event if ($this->storage instanceof EventsCapableInterface) { - $this->storage->getEventManager()->trigger('capability', $this->storage, new ArrayObject([ - $property => $value - ])); + $this->storage->getEventManager()->trigger('capability', $this->storage, [$property => $value]); } } diff --git a/src/Storage/Event.php b/src/Storage/Event.php index 362f9a55a..d14fe9c8f 100644 --- a/src/Storage/Event.php +++ b/src/Storage/Event.php @@ -9,7 +9,6 @@ namespace Zend\Cache\Storage; -use ArrayObject; use Zend\EventManager\Event as BaseEvent; class Event extends BaseEvent @@ -21,9 +20,9 @@ class Event extends BaseEvent * * @param string $name Event name * @param StorageInterface $storage - * @param ArrayObject $params + * @param array $params */ - public function __construct($name, StorageInterface $storage, ArrayObject $params) + public function __construct($name, StorageInterface $storage, array $params = null) { parent::__construct($name, $storage, $params); } @@ -62,4 +61,37 @@ public function getStorage() { return $this->getTarget(); } + + /** + * {@inheritdoc} + * + * Overwritten for performance reasons as the storage adapter events will handle + * params as plain arrays only. + * + * @param string $name + * @param mixed $default + * @return mixed + */ + public function getParam($name, $default = null) + { + if (array_key_exists($name, $this->params)) { + return $this->params[$name]; + } + + return $default; + } + + /** + * {@inheritdoc} + * + * Overwritten for performance reasons as the storage adapter events will handle + * params as plain arrays only. + * + * @param string $name + * @param mixed $value + */ + public function setParam($name, $value) + { + $this->params[$name] = $value; + } } diff --git a/src/Storage/ExceptionEvent.php b/src/Storage/ExceptionEvent.php index 29e27f785..b05013fdd 100644 --- a/src/Storage/ExceptionEvent.php +++ b/src/Storage/ExceptionEvent.php @@ -9,7 +9,6 @@ namespace Zend\Cache\Storage; -use ArrayObject; use Exception; class ExceptionEvent extends PostEvent @@ -33,13 +32,13 @@ class ExceptionEvent extends PostEvent * * Accept a target and its parameters. * - * @param string $name + * @param string $name * @param StorageInterface $storage - * @param ArrayObject $params - * @param mixed $result + * @param array $params + * @param mixed $result * @param Exception $exception */ - public function __construct($name, StorageInterface $storage, ArrayObject $params, & $result, Exception $exception) + public function __construct($name, StorageInterface $storage, array $params, $result, Exception $exception) { parent::__construct($name, $storage, $params, $result); $this->setException($exception); diff --git a/src/Storage/Plugin/Serializer.php b/src/Storage/Plugin/Serializer.php index eb8be788a..12103fe84 100644 --- a/src/Storage/Plugin/Serializer.php +++ b/src/Storage/Plugin/Serializer.php @@ -70,8 +70,7 @@ public function onReadItemPost(PostEvent $event) $result = $event->getResult(); if ($result !== null) { $serializer = $this->getOptions()->getSerializer(); - $result = $serializer->unserialize($result); - $event->setResult($result); + $event->setResult($serializer->unserialize($result)); } } @@ -84,11 +83,7 @@ public function onReadItemPost(PostEvent $event) public function onReadItemsPost(PostEvent $event) { $serializer = $this->getOptions()->getSerializer(); - $result = $event->getResult(); - foreach ($result as &$value) { - $value = $serializer->unserialize($value); - } - $event->setResult($result); + $event->setResult(array_map([$serializer, 'unserialize'], $event->getResult())); } /** @@ -100,8 +95,7 @@ public function onReadItemsPost(PostEvent $event) public function onWriteItemPre(Event $event) { $serializer = $this->getOptions()->getSerializer(); - $params = $event->getParams(); - $params['value'] = $serializer->serialize($params['value']); + $event->setParam('value', $serializer->serialize($event->getParam('value'))); } /** @@ -112,11 +106,9 @@ public function onWriteItemPre(Event $event) */ public function onWriteItemsPre(Event $event) { - $serializer = $this->getOptions()->getSerializer(); - $params = $event->getParams(); - foreach ($params['keyValuePairs'] as &$value) { - $value = $serializer->serialize($value); - } + $serializer = $this->getOptions()->getSerializer(); + $keyValuePairs = array_map([$serializer, 'serialize'], $event->getParam('keyValuePairs')); + $event->setParam('keyValuePairs', $keyValuePairs); } /** diff --git a/src/Storage/PostEvent.php b/src/Storage/PostEvent.php index ef4b8b349..963169c7c 100644 --- a/src/Storage/PostEvent.php +++ b/src/Storage/PostEvent.php @@ -9,8 +9,6 @@ namespace Zend\Cache\Storage; -use ArrayObject; - class PostEvent extends Event { /** @@ -27,10 +25,10 @@ class PostEvent extends Event * * @param string $name * @param StorageInterface $storage - * @param ArrayObject $params + * @param array $params * @param mixed $result */ - public function __construct($name, StorageInterface $storage, ArrayObject $params, & $result) + public function __construct($name, StorageInterface $storage, array $params, $result) { parent::__construct($name, $storage, $params); $this->setResult($result); @@ -42,9 +40,9 @@ public function __construct($name, StorageInterface $storage, ArrayObject $param * @param mixed $value * @return PostEvent */ - public function setResult(& $value) + public function setResult($value) { - $this->result = & $value; + $this->result = $value; return $this; } @@ -53,7 +51,7 @@ public function setResult(& $value) * * @return mixed */ - public function & getResult() + public function getResult() { return $this->result; } diff --git a/test/Storage/Adapter/AbstractAdapterTest.php b/test/Storage/Adapter/AbstractAdapterTest.php index cfe30a381..198eefb2e 100644 --- a/test/Storage/Adapter/AbstractAdapterTest.php +++ b/test/Storage/Adapter/AbstractAdapterTest.php @@ -145,7 +145,7 @@ public function testPluginRegistry() $this->assertEquals(0, count($this->_storage->getPluginRegistry())); $this->assertEquals(0, count($plugin->getHandles())); } - +/* public function testInternalTriggerPre() { $this->_storage = $this->getMockForAbstractAdapter(); @@ -153,10 +153,10 @@ public function testInternalTriggerPre() $plugin = new \ZendTest\Cache\Storage\TestAsset\MockPlugin(); $this->_storage->addPlugin($plugin); - $params = new \ArrayObject([ + $params = [ 'key' => 'key1', 'value' => 'value1' - ]); + ]; // call protected method $method = new \ReflectionMethod(get_class($this->_storage), 'triggerPre'); @@ -174,7 +174,7 @@ public function testInternalTriggerPre() $this->assertSame($this->_storage, $event->getTarget()); $this->assertSame($params, $event->getParams()); } - +*/ public function testInternalTriggerPost() { $this->_storage = $this->getMockForAbstractAdapter(); @@ -182,10 +182,10 @@ public function testInternalTriggerPost() $plugin = new \ZendTest\Cache\Storage\TestAsset\MockPlugin(); $this->_storage->addPlugin($plugin); - $params = new \ArrayObject([ + $params = [ 'key' => 'key1', 'value' => 'value1' - ]); + ]; $result = true; // call protected method @@ -216,10 +216,10 @@ public function testInternalTriggerExceptionThrowRuntimeException() $this->_storage->addPlugin($plugin); $result = null; - $params = new \ArrayObject([ + $params = [ 'key' => 'key1', 'value' => 'value1' - ]); + ]; // call protected method $method = new \ReflectionMethod(get_class($this->_storage), 'triggerException'); @@ -1053,10 +1053,7 @@ protected function checkPreEventCanChangeArguments($method, array $args, array $ // init mock $this->_storage = $this->getMockForAbstractAdapter([$internalMethod]); $this->_storage->getEventManager()->attach($eventName, function ($event) use ($expectedArgs) { - $params = $event->getParams(); - foreach ($expectedArgs as $k => $v) { - $params[$k] = $v; - } + $event->setParams($expectedArgs); }); // set expected arguments of internal method call diff --git a/test/Storage/Adapter/AdapterOptionsTest.php b/test/Storage/Adapter/AdapterOptionsTest.php index 4ca5b78a7..05e0d7c18 100644 --- a/test/Storage/Adapter/AdapterOptionsTest.php +++ b/test/Storage/Adapter/AdapterOptionsTest.php @@ -130,7 +130,7 @@ public function testTriggerOptionEvent() // assert (hopefully) called listener and arguments $this->assertCount(1, $calledArgs, '"option" event was not triggered or got a wrong number of arguments'); $this->assertInstanceOf(Event::class, $calledArgs[0]); - $this->assertEquals(['writable' => false], $calledArgs[0]->getParams()->getArrayCopy()); + $this->assertEquals(['writable' => false], $calledArgs[0]->getParams()); } public function testSetFromArrayWithoutPrioritizedOptions() diff --git a/test/Storage/Adapter/FilesystemTest.php b/test/Storage/Adapter/FilesystemTest.php index b9a4cfc29..7ae978f37 100644 --- a/test/Storage/Adapter/FilesystemTest.php +++ b/test/Storage/Adapter/FilesystemTest.php @@ -304,7 +304,7 @@ public function testClearExpiredExceptionTriggersEvent() } chmod($dirs[0], 0500); //make directory rx, unlink should fail sleep(1); //wait for the entry to expire - $plugin = new ExceptionHandler(); + $plugin = new ExceptionHandler(); $options = new PluginOptions(['throw_exceptions' => false]); $plugin->setOptions($options); $this->_storage->addPlugin($plugin); diff --git a/test/Storage/CapabilitiesTest.php b/test/Storage/CapabilitiesTest.php index 21d54ac58..ffa9a7cb8 100644 --- a/test/Storage/CapabilitiesTest.php +++ b/test/Storage/CapabilitiesTest.php @@ -88,7 +88,6 @@ public function testTriggerCapabilityEvent() $this->assertSame($this->_adapter, $event->getTarget()); $params = $event->getParams(); - $this->assertInstanceOf('ArrayObject', $params); $this->assertTrue(isset($params ['maxTtl'])); $this->assertEquals(100, $params['maxTtl']); } diff --git a/test/Storage/Plugin/ClearExpiredByFactorTest.php b/test/Storage/Plugin/ClearExpiredByFactorTest.php index 87c52db0a..41b758418 100644 --- a/test/Storage/Plugin/ClearExpiredByFactorTest.php +++ b/test/Storage/Plugin/ClearExpiredByFactorTest.php @@ -9,7 +9,6 @@ namespace ZendTest\Cache\Storage\Plugin; -use ArrayObject; use Zend\Cache; use Zend\Cache\Storage\PostEvent; use Zend\EventManager\Test\EventListenerIntrospectionTrait; @@ -89,9 +88,9 @@ public function testClearExpiredByFactor() // call event callback $result = true; - $event = new PostEvent('setItem.post', $adapter, new ArrayObject([ + $event = new PostEvent('setItem.post', $adapter, [ 'options' => [], - ]), $result); + ], $result); $this->_plugin->clearExpiredByFactor($event); $this->assertTrue($event->getResult()); diff --git a/test/Storage/Plugin/ExceptionHandlerTest.php b/test/Storage/Plugin/ExceptionHandlerTest.php index da89ea7f9..68d5a7586 100644 --- a/test/Storage/Plugin/ExceptionHandlerTest.php +++ b/test/Storage/Plugin/ExceptionHandlerTest.php @@ -9,7 +9,6 @@ namespace ZendTest\Cache\Storage\Plugin; -use ArrayObject; use Zend\Cache; use Zend\Cache\Storage\ExceptionEvent; use Zend\EventManager\Test\EventListenerIntrospectionTrait; @@ -114,10 +113,10 @@ public function testOnExceptionCallCallback() // run onException $result = null; - $event = new ExceptionEvent('getItem.exception', $this->_adapter, new ArrayObject([ + $event = new ExceptionEvent('getItem.exception', $this->_adapter, [ 'key' => 'key', 'options' => [] - ]), $result, $expectedException); + ], $result, $expectedException); $this->_plugin->onException($event); $this->assertTrue( @@ -132,10 +131,10 @@ public function testDontThrowException() // run onException $result = 'test'; - $event = new ExceptionEvent('getItem.exception', $this->_adapter, new ArrayObject([ + $event = new ExceptionEvent('getItem.exception', $this->_adapter, [ 'key' => 'key', 'options' => [] - ]), $result, new \Exception()); + ], $result, new \Exception()); $this->_plugin->onException($event); $this->assertFalse($event->getThrowException()); diff --git a/test/Storage/Plugin/OptimizeByFactorTest.php b/test/Storage/Plugin/OptimizeByFactorTest.php index 7358a6fce..43681bafc 100644 --- a/test/Storage/Plugin/OptimizeByFactorTest.php +++ b/test/Storage/Plugin/OptimizeByFactorTest.php @@ -9,7 +9,6 @@ namespace ZendTest\Cache\Storage\Plugin; -use ArrayObject; use Zend\Cache; use Zend\Cache\Storage\PostEvent; use Zend\EventManager\Test\EventListenerIntrospectionTrait; @@ -83,9 +82,9 @@ public function testOptimizeByFactor() // call event callback $result = true; - $event = new PostEvent('removeItem.post', $adapter, new ArrayObject([ + $event = new PostEvent('removeItem.post', $adapter, [ 'options' => [] - ]), $result); + ], $result); $this->_plugin->optimizeByFactor($event); diff --git a/test/Storage/Plugin/SerializerTest.php b/test/Storage/Plugin/SerializerTest.php index 81112ae47..b943f14c6 100644 --- a/test/Storage/Plugin/SerializerTest.php +++ b/test/Storage/Plugin/SerializerTest.php @@ -9,8 +9,9 @@ namespace ZendTest\Cache\Storage\Plugin; -use ArrayObject; +use stdClass; use Zend\Cache; +use Zend\Cache\Storage\Capabilities; use Zend\Cache\Storage\Event; use Zend\Cache\Storage\PostEvent; use Zend\EventManager\Test\EventListenerIntrospectionTrait; @@ -95,25 +96,41 @@ public function testRemovePlugin() $this->assertEquals(0, count($this->getEventsFromEventManager($this->_adapter->getEventManager()))); } - public function testUnserializeOnReadItem() + public function testSerializeOnWriteItemPre() { - $args = new ArrayObject([ - 'key' => 'test', - 'success' => true, - 'casToken' => null, - ]); - $value = serialize(123); - $event = new PostEvent('getItem.post', $this->_adapter, $args, $value); + $key = 'testKey'; + $value = 123; + $valueSer = serialize($value); + + $args = ['key' => $key, 'value' => $value]; + $event = new Event('setItem.pre', $this->_adapter, $args); + $this->_plugin->onWriteItemPre($event); + + $this->assertFalse($event->propagationIsStopped(), 'Event propagation has been stopped'); + $this->assertSame($valueSer, $event->getParam('value'), 'Value was not serialized'); + $this->assertSame($key, $event->getParam('key'), 'Missing or changed key'); + } + + public function testUnserializeOnReadItemPost() + { + $key = 'testKey'; + $value = 123; + $valueSer = serialize($value); + + $args = ['key' => $key, 'success' => true, 'casToken' => null]; + $event = new PostEvent('getItem.post', $this->_adapter, $args, $valueSer); $this->_plugin->onReadItemPost($event); $this->assertFalse($event->propagationIsStopped(), 'Event propagation has been stopped'); - $this->assertSame(123, $event->getResult(), 'Result was not unserialized'); + $this->assertSame($value, $event->getResult(), 'Result was not unserialized'); } - public function testDontUnserializeOnReadMissingItem() + public function testDontUnserializeOnReadMissingItemPost() { - $args = new ArrayObject(['key' => 'test']); + $key = 'testKey'; $value = null; + + $args = ['key' => $key]; $event = new PostEvent('getItem.post', $this->_adapter, $args, $value); $this->_plugin->onReadItemPost($event); @@ -121,12 +138,11 @@ public function testDontUnserializeOnReadMissingItem() $this->assertSame($value, $event->getResult(), 'Missing item was unserialized'); } - public function testUnserializeOnReadItems() + public function testUnserializeOnReadItemsPost() { $values = ['key1' => serialize(123), 'key2' => serialize(456)]; - $args = new ArrayObject(['keys' => array_keys($values) + ['missing']]); + $args = ['keys' => array_keys($values) + ['missing']]; $event = new PostEvent('getItems.post', $this->_adapter, $args, $values); - $this->_plugin->onReadItemsPost($event); $this->assertFalse($event->propagationIsStopped(), 'Event propagation has been stopped'); @@ -136,4 +152,35 @@ public function testUnserializeOnReadItems() $this->assertSame(456, $values['key2'], "Item 'key2' was not unserialized"); $this->assertArrayNotHasKey('missing', $values, 'Missing item should not be present in the result'); } + + public function testOnGetCapabilitiesPostOverwritesSupportedDatatypes() + { + $baseCapabilities = new Capabilities($this->_adapter, new stdClass, [ + 'supportedDatatypes' => [ + 'NULL' => false, + 'boolean' => false, + 'integer' => false, + 'double' => false, + 'string' => true, + 'array' => false, + 'object' => false, + 'resource' => true, + ] + ]); + + $event = new PostEvent('getCapabilities.post', $this->_adapter, [], $baseCapabilities); + $this->_plugin->onGetCapabilitiesPost($event); + + $this->assertFalse($event->propagationIsStopped(), 'Event propagation has been stopped'); + $this->assertSame([ + 'NULL' => true, + 'boolean' => true, + 'integer' => true, + 'double' => true, + 'string' => true, + 'array' => true, + 'object' => 'object', + 'resource' => false, + ], $event->getResult()->getSupportedDatatypes(), 'Unexpected supported datatypes'); + } } diff --git a/test/Storage/TestAsset/MockAdapter.php b/test/Storage/TestAsset/MockAdapter.php index 1837c8283..a9af124d8 100644 --- a/test/Storage/TestAsset/MockAdapter.php +++ b/test/Storage/TestAsset/MockAdapter.php @@ -14,15 +14,15 @@ class MockAdapter extends AbstractAdapter { - protected function internalGetItem(& $normalizedKey, & $success = null, & $casToken = null) + protected function internalGetItem($normalizedKey, & $success = null, & $casToken = null) { } - protected function internalSetItem(& $normalizedKey, & $value) + protected function internalSetItem($normalizedKey, $value) { } - protected function internalRemoveItem(& $normalizedKey) + protected function internalRemoveItem($normalizedKey) { } }