Skip to content

Commit

Permalink
psr/cache to 3.0 (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
frederikbosch authored Sep 25, 2024
1 parent c7a2e5d commit 19a8626
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 109 deletions.
7 changes: 0 additions & 7 deletions .phpstan.neon

This file was deleted.

10 changes: 9 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"ext-xsl" : "*",
"ext-dom" : "*",
"ext-intl" : "*",
"psr/simple-cache": "^1.0"
"psr/simple-cache": "^3.0"
},
"require-dev" : {
"phpunit/phpunit": "^9",
Expand All @@ -25,5 +25,13 @@
"psr-4" : {
"Genkgo\\Xsl\\" : ["test"]
}
},
"scripts": {
"test": [
"./vendor/bin/phpunit -c phpunit.xml",
"./vendor/bin/phpstan analyse -l 5 src",
"./vendor/bin/phpstan analyse -l 7 test",
"./vendor/bin/php-cs-fixer fix --dry-run --verbose --config .php-cs-fixer.dist.php ./src ./test"
]
}
}
70 changes: 13 additions & 57 deletions src/Cache/ArrayCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,9 @@

final class ArrayCache implements CacheInterface
{
/**
* @var array
*/
private $memory = [];

/**
* @param string $key
* @param mixed $default
* @return mixed
*/
public function get($key, $default = null)
private array $memory = [];

public function get(string $key, mixed $default = null): mixed
{
if (isset($this->memory[$key])) {
return $this->getNonExpiredCacheOrDefault($this->memory[$key], $default);
Expand All @@ -26,13 +18,7 @@ public function get($key, $default = null)
return $default;
}

/**
* @param string $key
* @param mixed $value
* @param int|null|\DateInterval $ttl
* @return bool
*/
public function set($key, $value, $ttl = null)
public function set(string $key, mixed $value, null|int|\DateInterval $ttl = null): bool
{
$expires = null;

Expand All @@ -47,43 +33,26 @@ public function set($key, $value, $ttl = null)
return true;
}

/**
* @param string $key
* @return bool
*/
public function delete($key)
public function delete(string $key): bool
{
unset($this->memory[$key]);
return true;
}

/**
* @return bool
*/
public function clear()
public function clear(): bool
{
$this->memory = [];
return true;
}

/**
* @param iterable $keys
* @param mixed $default
* @return iterable
*/
public function getMultiple($keys, $default = null)
public function getMultiple(iterable $keys, mixed $default = null): iterable
{
foreach (\array_keys($this->memory) as $key) {
yield $this->get($key, $default);
foreach ($keys as $key) {
yield $key => $this->get($key, $default);
}
}

/**
* @param iterable $values
* @param null|int|\DateInterval $ttl
* @return bool
*/
public function setMultiple($values, $ttl = null)
public function setMultiple(iterable $values, null|int|\DateInterval $ttl = null): bool
{
foreach ($values as $key => $value) {
$this->set($key, $value, $ttl);
Expand All @@ -92,11 +61,7 @@ public function setMultiple($values, $ttl = null)
return true;
}

/**
* @param iterable $keys
* @return bool
*/
public function deleteMultiple($keys)
public function deleteMultiple(iterable $keys): bool
{
foreach (\array_keys($this->memory) as $key) {
$this->delete($key);
Expand All @@ -105,21 +70,12 @@ public function deleteMultiple($keys)
return true;
}

/**
* @param string $key
* @return bool
*/
public function has($key)
public function has(string $key): bool
{
return isset($this->memory[$key]);
}

/**
* @param array $keyData
* @param mixed|null $default
* @return mixed
*/
private function getNonExpiredCacheOrDefault(array $keyData, $default = null)
private function getNonExpiredCacheOrDefault(array $keyData, mixed $default = null): mixed
{
[$value, $expires] = $keyData;

Expand Down
52 changes: 8 additions & 44 deletions src/Cache/NullCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,78 +7,42 @@

final class NullCache implements CacheInterface
{
/**
* @param string $key
* @param mixed $default
* @return mixed
*/
public function get($key, $default = null)
public function get(string $key, mixed $default = null): mixed
{
return $default;
}

/**
* @param string $key
* @param mixed $value
* @param int|null $ttl
* @return bool
*/
public function set($key, $value, $ttl = null)
public function set(string $key, mixed $value, null|int|\DateInterval $ttl = null): bool
{
return true;
}

/**
* @param string $key
* @return bool
*/
public function delete($key)
public function delete(string $key): bool
{
return true;
}

/**
* @return bool
*/
public function clear()
public function clear(): bool
{
return true;
}

/**
* @param iterable $keys
* @param mixed $default
* @return iterable
*/
public function getMultiple($keys, $default = null)
public function getMultiple(iterable $keys, mixed $default = null): iterable
{
return [];
}

/**
* @param iterable $values
* @param null|int|\DateInterval $ttl
* @return bool
*/
public function setMultiple($values, $ttl = null)
public function setMultiple(iterable $values, null|int|\DateInterval $ttl = null): bool
{
return true;
}

/**
* @param iterable $keys
* @return bool
*/
public function deleteMultiple($keys)
public function deleteMultiple(iterable $keys): bool
{
return true;
}

/**
* @param string $key
* @return bool
*/
public function has($key)
public function has(string $key): bool
{
return false;
}
Expand Down

0 comments on commit 19a8626

Please sign in to comment.