Skip to content

Commit

Permalink
more codestyle improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
sgiehl committed Aug 18, 2019
1 parent 4104102 commit ad8d13b
Show file tree
Hide file tree
Showing 71 changed files with 820 additions and 539 deletions.
20 changes: 13 additions & 7 deletions Cache/Cache.php → Cache/CacheInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,44 @@
/**
* Device Detector - The Universal Device Detection library for parsing User Agents
*
* @link http://piwik.org
* @link https://matomo.org
*
* @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later
*/

namespace DeviceDetector\Cache;

interface Cache
interface CacheInterface
{
/**
* @param string $id
*
* @return mixed
*/
public function fetch($id);
public function fetch(string $id);

/**
* @param $id
* @param string $id
*
* @return bool
*/
public function contains($id): bool;
public function contains(string $id): bool;

/**
* @param string $id
* @param mixed $data
* @param int $lifeTime
*
* @return bool
*/
public function save($id, $data, $lifeTime = 0): bool;
public function save(string $id, $data, int $lifeTime = 0): bool;

/**
* @param string $id
*
* @return bool
*/
public function delete($id): bool;
public function delete(string $id): bool;

/**
* @return bool
Expand Down
14 changes: 7 additions & 7 deletions Cache/DoctrineBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Device Detector - The Universal Device Detection library for parsing User Agents
*
* @link http://piwik.org
* @link https://matomo.org
*
* @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later
*/
Expand All @@ -12,12 +12,12 @@

use Doctrine\Common\Cache\CacheProvider ;

class DoctrineBridge implements Cache
class DoctrineBridge implements CacheInterface
{
/**
* @var CacheProvider
*/
private $pool;
private $cache;

/**
* @param CacheProvider $cache
Expand All @@ -30,31 +30,31 @@ public function __construct(CacheProvider $cache)
/**
* @inheritDoc
*/
public function fetch($id)
public function fetch(string $id)
{
return $this->cache->fetch($id);
}

/**
* @inheritDoc
*/
public function contains($id): bool
public function contains(string $id): bool
{
return $this->cache->contains($id);
}

/**
* @inheritDoc
*/
public function save($id, $data, $lifeTime = 0): bool
public function save(string $id, $data, int $lifeTime = 0): bool
{
return $this->cache->save($id, $data, $lifeTime);
}

/**
* @inheritDoc
*/
public function delete($id): bool
public function delete(string $id): bool
{
return $this->cache->delete($id);
}
Expand Down
20 changes: 10 additions & 10 deletions Cache/PSR16Bridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,59 +3,59 @@
/**
* Device Detector - The Universal Device Detection library for parsing User Agents
*
* @link http://piwik.org
* @link https://matomo.org
*
* @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later
*/

namespace DeviceDetector\Cache;

use Psr\SimpleCache\CacheInterface;
use Psr\SimpleCache\CacheInterface as PsrCacheInterface;

class PSR16Bridge implements Cache
class PSR16Bridge implements CacheInterface
{
/**
* @var CacheInterface
* @var PsrCacheInterface
*/
private $cache;

/**
* PSR16Bridge constructor.
* @param CacheInterface $cache
* @param PsrCacheInterface $cache
*/
public function __construct(CacheInterface $cache)
public function __construct(PsrCacheInterface $cache)
{
$this->cache = $cache;
}

/**
* @inheritDoc
*/
public function fetch($id)
public function fetch(string $id)
{
return $this->cache->get($id, false);
}

/**
* @inheritDoc
*/
public function contains($id): bool
public function contains(string $id): bool
{
return $this->cache->has($id);
}

/**
* @inheritDoc
*/
public function save($id, $data, $lifeTime = 0): bool
public function save(string $id, $data, int $lifeTime = 0): bool
{
return $this->cache->set($id, $data, func_num_args() < 3 ? null : $lifeTime);
}

/**
* @inheritDoc
*/
public function delete($id): bool
public function delete(string $id): bool
{
return $this->cache->delete($id);
}
Expand Down
12 changes: 6 additions & 6 deletions Cache/PSR6Bridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Device Detector - The Universal Device Detection library for parsing User Agents
*
* @link http://piwik.org
* @link https://matomo.org
*
* @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later
*/
Expand All @@ -12,7 +12,7 @@

use Psr\Cache\CacheItemPoolInterface;

class PSR6Bridge implements Cache
class PSR6Bridge implements CacheInterface
{
/**
* @var CacheItemPoolInterface
Expand All @@ -31,7 +31,7 @@ public function __construct(CacheItemPoolInterface $pool)
/**
* @inheritDoc
*/
public function fetch($id)
public function fetch(string $id)
{
$item = $this->pool->getItem($id);

Expand All @@ -41,15 +41,15 @@ public function fetch($id)
/**
* @inheritDoc
*/
public function contains($id): bool
public function contains(string $id): bool
{
return $this->pool->hasItem($id);
}

/**
* @inheritDoc
*/
public function save($id, $data, $lifeTime = 0): bool
public function save(string $id, $data, int $lifeTime = 0): bool
{
$item = $this->pool->getItem($id);
$item->set($data);
Expand All @@ -64,7 +64,7 @@ public function save($id, $data, $lifeTime = 0): bool
/**
* @inheritDoc
*/
public function delete($id): bool
public function delete(string $id): bool
{
return $this->pool->deleteItem($id);
}
Expand Down
27 changes: 21 additions & 6 deletions Cache/StaticCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Device Detector - The Universal Device Detection library for parsing User Agents
*
* @link http://piwik.org
* @link https://matomo.org
*
* @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later
*/
Expand All @@ -16,38 +16,53 @@
* Simple Cache that caches in a static property
* (Speeds up multiple detections in one request)
*/
class StaticCache implements Cache
class StaticCache implements CacheInterface
{
/**
* Holds the static cache data
* @var array
*/
protected static $staticCache = [];

public function fetch($id)
/**
* @inheritdoc
*/
public function fetch(string $id)
{
return $this->contains($id) ? self::$staticCache[$id] : false;
}

public function contains($id): bool
/**
* @inheritdoc
*/
public function contains(string $id): bool
{
return isset(self::$staticCache[$id]) || array_key_exists($id, self::$staticCache);
}

public function save($id, $data, $lifeTime = 0): bool
/**
* @inheritdoc
*/
public function save(string $id, $data, int $lifeTime = 0): bool
{
self::$staticCache[$id] = $data;

return true;
}

public function delete($id): bool
/**
* @inheritdoc
*/
public function delete(string $id): bool
{
unset(self::$staticCache[$id]);

return true;
}

/**
* @inheritdoc
*/
public function flushAll(): bool
{
self::$staticCache = [];
Expand Down
Loading

0 comments on commit ad8d13b

Please sign in to comment.