Skip to content

[Cache] Leverage union types #42015

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Symfony/Component/Cache/Adapter/AdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ interface AdapterInterface extends CacheItemPoolInterface
/**
* {@inheritdoc}
*/
public function getItem($key): CacheItem;
public function getItem(mixed $key): CacheItem;

/**
* {@inheritdoc}
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/Cache/Adapter/ArrayAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function delete(string $key): bool
/**
* {@inheritdoc}
*/
public function hasItem($key): bool
public function hasItem(mixed $key): bool
{
if (\is_string($key) && isset($this->expiries[$key]) && $this->expiries[$key] > microtime(true)) {
if ($this->maxItems) {
Expand All @@ -118,7 +118,7 @@ public function hasItem($key): bool
/**
* {@inheritdoc}
*/
public function getItem($key): CacheItem
public function getItem(mixed $key): CacheItem
{
if (!$isHit = $this->hasItem($key)) {
$value = null;
Expand Down Expand Up @@ -147,7 +147,7 @@ public function getItems(array $keys = []): iterable
/**
* {@inheritdoc}
*/
public function deleteItem($key): bool
public function deleteItem(mixed $key): bool
{
\assert('' !== CacheItem::validateKey($key));
unset($this->values[$key], $this->expiries[$key]);
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/Cache/Adapter/ChainAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public function get(string $key, callable $callback, float $beta = null, array &
/**
* {@inheritdoc}
*/
public function getItem($key): CacheItem
public function getItem(mixed $key): CacheItem
{
$syncItem = self::$syncItem;
$misses = [];
Expand Down Expand Up @@ -184,7 +184,7 @@ private function generateItems(iterable $items, int $adapterIndex): \Generator
/**
* {@inheritdoc}
*/
public function hasItem($key): bool
public function hasItem(mixed $key): bool
{
foreach ($this->adapters as $adapter) {
if ($adapter->hasItem($key)) {
Expand Down Expand Up @@ -217,7 +217,7 @@ public function clear(string $prefix = ''): bool
/**
* {@inheritdoc}
*/
public function deleteItem($key): bool
public function deleteItem(mixed $key): bool
{
$deleted = true;
$i = $this->adapterCount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ public function __construct(\CouchbaseBucket $bucket, string $namespace = '', in
$this->marshaller = $marshaller ?? new DefaultMarshaller();
}

/**
* @param array|string $servers
*/
public static function createConnection($servers, array $options = []): \CouchbaseBucket
public static function createConnection(array|string $servers, array $options = []): \CouchbaseBucket
{
if (\is_string($servers)) {
$servers = [$servers];
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Cache/Adapter/MemcachedAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public static function isSupported()
*
* @throws \ErrorException When invalid options or servers are provided
*/
public static function createConnection($servers, array $options = [])
public static function createConnection(array|string $servers, array $options = [])
{
if (\is_string($servers)) {
$servers = [$servers];
Expand Down Expand Up @@ -314,7 +314,7 @@ protected function doClear(string $namespace)
return '' === $namespace && $this->getClient()->flush();
}

private function checkResultCode($result)
private function checkResultCode(mixed $result)
{
$code = $this->client->getResultCode();

Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/Cache/Adapter/NullAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function get(string $key, callable $callback, float $beta = null, array &
/**
* {@inheritdoc}
*/
public function getItem($key): CacheItem
public function getItem(mixed $key): CacheItem
{
return (self::$createCacheItem)($key);
}
Expand All @@ -66,7 +66,7 @@ public function getItems(array $keys = []): iterable
/**
* {@inheritdoc}
*/
public function hasItem($key): bool
public function hasItem(mixed $key): bool
{
return false;
}
Expand All @@ -82,7 +82,7 @@ public function clear(string $prefix = ''): bool
/**
* {@inheritdoc}
*/
public function deleteItem($key): bool
public function deleteItem(mixed $key): bool
{
return true;
}
Expand Down
4 changes: 1 addition & 3 deletions src/Symfony/Component/Cache/Adapter/PdoAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,11 @@ class PdoAdapter extends AbstractAdapter implements PruneableInterface
* * db_password: The password when lazy-connect [default: '']
* * db_connection_options: An array of driver-specific connection options [default: []]
*
* @param \PDO|Connection|string $connOrDsn a \PDO or Connection instance or DSN string or null
*
* @throws InvalidArgumentException When first argument is not PDO nor Connection nor string
* @throws InvalidArgumentException When PDO error mode is not PDO::ERRMODE_EXCEPTION
* @throws InvalidArgumentException When namespace contains invalid characters
*/
public function __construct($connOrDsn, string $namespace = '', int $defaultLifetime = 0, array $options = [], MarshallerInterface $marshaller = null)
public function __construct(\PDO|Connection|string $connOrDsn, string $namespace = '', int $defaultLifetime = 0, array $options = [], MarshallerInterface $marshaller = null)
{
if (isset($namespace[0]) && preg_match('#[^-+.A-Za-z0-9]#', $namespace, $match)) {
throw new InvalidArgumentException(sprintf('Namespace contains "%s" but only characters in [-+.A-Za-z0-9] are allowed.', $match[0]));
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/Cache/Adapter/PhpArrayAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public function get(string $key, callable $callback, float $beta = null, array &
/**
* {@inheritdoc}
*/
public function getItem($key): CacheItem
public function getItem(mixed $key): CacheItem
{
if (!\is_string($key)) {
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', get_debug_type($key)));
Expand Down Expand Up @@ -165,7 +165,7 @@ public function getItems(array $keys = []): iterable
/**
* {@inheritdoc}
*/
public function hasItem($key): bool
public function hasItem(mixed $key): bool
{
if (!\is_string($key)) {
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', get_debug_type($key)));
Expand All @@ -180,7 +180,7 @@ public function hasItem($key): bool
/**
* {@inheritdoc}
*/
public function deleteItem($key): bool
public function deleteItem(mixed $key): bool
{
if (!\is_string($key)) {
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', get_debug_type($key)));
Expand Down
8 changes: 4 additions & 4 deletions src/Symfony/Component/Cache/Adapter/ProxyAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public function get(string $key, callable $callback, float $beta = null, array &
/**
* {@inheritdoc}
*/
public function getItem($key): CacheItem
public function getItem(mixed $key): CacheItem
{
$item = $this->pool->getItem($this->getId($key));

Expand All @@ -144,7 +144,7 @@ public function getItems(array $keys = []): iterable
/**
* {@inheritdoc}
*/
public function hasItem($key): bool
public function hasItem(mixed $key): bool
{
return $this->pool->hasItem($this->getId($key));
}
Expand All @@ -164,7 +164,7 @@ public function clear(string $prefix = ''): bool
/**
* {@inheritdoc}
*/
public function deleteItem($key): bool
public function deleteItem(mixed $key): bool
{
return $this->pool->deleteItem($this->getId($key));
}
Expand Down Expand Up @@ -245,7 +245,7 @@ private function generateItems(iterable $items): \Generator
}
}

private function getId($key): string
private function getId(mixed $key): string
{
\assert('' !== CacheItem::validateKey($key));

Expand Down
7 changes: 1 addition & 6 deletions src/Symfony/Component/Cache/Adapter/RedisAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,7 @@ class RedisAdapter extends AbstractAdapter
{
use RedisTrait;

/**
* @param \Redis|\RedisArray|\RedisCluster|\Predis\ClientInterface|RedisProxy|RedisClusterProxy $redis The redis client
* @param string $namespace The default namespace
* @param int $defaultLifetime The default lifetime
*/
public function __construct($redis, string $namespace = '', int $defaultLifetime = 0, MarshallerInterface $marshaller = null)
public function __construct(\Redis|\RedisArray|\RedisCluster|\Predis\ClientInterface|RedisProxy|RedisClusterProxy $redis, string $namespace = '', int $defaultLifetime = 0, MarshallerInterface $marshaller = null)
{
$this->init($redis, $namespace, $defaultLifetime, $marshaller);
}
Expand Down
7 changes: 1 addition & 6 deletions src/Symfony/Component/Cache/Adapter/RedisTagAwareAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,7 @@ class RedisTagAwareAdapter extends AbstractTagAwareAdapter
*/
private $redisEvictionPolicy;

/**
* @param \Redis|\RedisArray|\RedisCluster|\Predis\ClientInterface|RedisProxy|RedisClusterProxy $redis The redis client
* @param string $namespace The default namespace
* @param int $defaultLifetime The default lifetime
*/
public function __construct($redis, string $namespace = '', int $defaultLifetime = 0, MarshallerInterface $marshaller = null)
public function __construct(\Redis|\RedisArray|\RedisCluster|\Predis\ClientInterface|RedisProxy|RedisClusterProxy $redis, string $namespace = '', int $defaultLifetime = 0, MarshallerInterface $marshaller = null)
{
if ($redis instanceof \Predis\ClientInterface && $redis->getConnection() instanceof ClusterInterface && !$redis->getConnection() instanceof PredisCluster) {
throw new InvalidArgumentException(sprintf('Unsupported Predis cluster connection: only "%s" is, "%s" given.', PredisCluster::class, get_debug_type($redis->getConnection())));
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/Cache/Adapter/TagAwareAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public function invalidateTags(array $tags)
/**
* {@inheritdoc}
*/
public function hasItem($key): bool
public function hasItem(mixed $key): bool
{
if ($this->deferred) {
$this->commit();
Expand Down Expand Up @@ -184,7 +184,7 @@ public function hasItem($key): bool
/**
* {@inheritdoc}
*/
public function getItem($key): CacheItem
public function getItem(mixed $key): CacheItem
{
foreach ($this->getItems([$key]) as $item) {
return $item;
Expand Down Expand Up @@ -244,7 +244,7 @@ public function clear(string $prefix = ''): bool
/**
* {@inheritdoc}
*/
public function deleteItem($key): bool
public function deleteItem(mixed $key): bool
{
return $this->deleteItems([$key]);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/Cache/Adapter/TraceableAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function get(string $key, callable $callback, float $beta = null, array &
/**
* {@inheritdoc}
*/
public function getItem($key): CacheItem
public function getItem(mixed $key): CacheItem
{
$event = $this->start(__FUNCTION__);
try {
Expand All @@ -90,7 +90,7 @@ public function getItem($key): CacheItem
/**
* {@inheritdoc}
*/
public function hasItem($key): bool
public function hasItem(mixed $key): bool
{
$event = $this->start(__FUNCTION__);
try {
Expand All @@ -103,7 +103,7 @@ public function hasItem($key): bool
/**
* {@inheritdoc}
*/
public function deleteItem($key): bool
public function deleteItem(mixed $key): bool
{
$event = $this->start(__FUNCTION__);
try {
Expand Down
12 changes: 3 additions & 9 deletions src/Symfony/Component/Cache/CacheItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,9 @@ public function set($value): static
*
* @return $this
*/
public function expiresAt($expiration): static
public function expiresAt(?\DateTimeInterface $expiration): static
{
if (null === $expiration) {
$this->expiry = null;
} elseif ($expiration instanceof \DateTimeInterface) {
$this->expiry = (float) $expiration->format('U.u');
} else {
throw new InvalidArgumentException(sprintf('Expiration date must implement DateTimeInterface or be null, "%s" given.', get_debug_type($expiration)));
}
$this->expiry = null !== $expiration ? (float) $expiration->format('U.u') : null;

return $this;
}
Expand All @@ -92,7 +86,7 @@ public function expiresAt($expiration): static
*
* @return $this
*/
public function expiresAfter($time): static
public function expiresAfter(mixed $time): static
{
if (null === $time) {
$this->expiry = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function findCallback(ReverseContainer $reverseContainer): callable
return $callback;
}

private function __construct(CacheItem $item, string $pool, $callback)
private function __construct(CacheItem $item, string $pool, string|array $callback)
{
$this->item = $item;
$this->pool = $pool;
Expand Down
Loading