Skip to content

Add union types #41424

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

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 1 addition & 3 deletions src/Symfony/Component/Cache/CacheItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,9 @@ public function getMetadata(): array
/**
* Validates a cache key according to PSR-6.
*
* @param string $key The key to validate
*
* @throws InvalidArgumentException When $key is not valid
*/
public static function validateKey($key): string
public static function validateKey(mixed $key): string
{
if (!\is_string($key)) {
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', get_debug_type($key)));
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
8 changes: 3 additions & 5 deletions src/Symfony/Component/Cache/Traits/AbstractAdapterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,14 +291,12 @@ public function saveDeferred(CacheItemInterface $item)
*
* Calling this method also clears the memoized namespace version and thus forces a resynchonization of it.
*
* @param bool $enable
*
* @return bool the previous state of versioning
*/
public function enableVersioning($enable = true)
public function enableVersioning(bool $enable = true)
{
$wasEnabled = $this->versioningIsEnabled;
$this->versioningIsEnabled = (bool) $enable;
$this->versioningIsEnabled = $enable;
$this->namespaceVersion = '';
$this->ids = [];

Expand Down Expand Up @@ -356,7 +354,7 @@ private function generateItems(iterable $items, array &$keys): iterable
}
}

private function getId($key)
private function getId(string|int $key)
{
if ($this->versioningIsEnabled && '' === $this->namespaceVersion) {
$this->ids = [];
Expand Down
10 changes: 4 additions & 6 deletions src/Symfony/Component/Cache/Traits/MemcachedTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,10 @@ private function init(\Memcached $client, string $namespace, int $defaultLifetim
*
* @throws \ErrorException When invalid options or servers are provided
*/
public static function createConnection($servers, array $options = [])
public static function createConnection(string|array $servers, array $options = [])
{
if (\is_string($servers)) {
$servers = [$servers];
} elseif (!\is_array($servers)) {
throw new InvalidArgumentException(sprintf('MemcachedAdapter::createClient() expects array or string as first argument, "%s" given.', \gettype($servers)));
}
if (!static::isSupported()) {
throw new CacheException('Memcached >= 2.2.0 is required.');
Expand Down Expand Up @@ -273,7 +271,7 @@ protected function doFetch(array $ids)
/**
* {@inheritdoc}
*/
protected function doHave($id)
protected function doHave(string $id)
{
return false !== $this->getClient()->get(self::encodeKey($id)) || $this->checkResultCode(\Memcached::RES_SUCCESS === $this->client->getResultCode());
}
Expand All @@ -298,12 +296,12 @@ protected function doDelete(array $ids)
/**
* {@inheritdoc}
*/
protected function doClear($namespace)
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
16 changes: 4 additions & 12 deletions src/Symfony/Component/Cache/Traits/RedisTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,14 @@ trait RedisTrait
private $redis;
private $marshaller;

/**
* @param \Redis|\RedisArray|\RedisCluster|\Predis\ClientInterface $redisClient
*/
private function init($redisClient, string $namespace, int $defaultLifetime, ?MarshallerInterface $marshaller)
private function init(\Redis|\RedisArray|\RedisCluster|\Predis\ClientInterface|RedisProxy|RedisClusterProxy $redisClient, string $namespace, int $defaultLifetime, ?MarshallerInterface $marshaller)
{
parent::__construct($namespace, $defaultLifetime);

if (preg_match('#[^-+_.A-Za-z0-9]#', $namespace, $match)) {
throw new InvalidArgumentException(sprintf('RedisAdapter namespace contains "%s" but only characters in [-+_.A-Za-z0-9] are allowed.', $match[0]));
}

if (!$redisClient instanceof \Redis && !$redisClient instanceof \RedisArray && !$redisClient instanceof \RedisCluster && !$redisClient instanceof \Predis\ClientInterface && !$redisClient instanceof RedisProxy && !$redisClient instanceof RedisClusterProxy) {
throw new InvalidArgumentException(sprintf('"%s()" expects parameter 1 to be Redis, RedisArray, RedisCluster or Predis\ClientInterface, "%s" given.', __METHOD__, get_debug_type($redisClient)));
}

if ($redisClient instanceof \Predis\ClientInterface && $redisClient->getOptions()->exceptions) {
$options = clone $redisClient->getOptions();
\Closure::bind(function () { $this->options['exceptions'] = false; }, $options, $options)();
Expand All @@ -82,14 +75,13 @@ private function init($redisClient, string $namespace, int $defaultLifetime, ?Ma
* - redis:///var/run/redis.sock
* - redis://secret@/var/run/redis.sock/13
*
* @param string $dsn
* @param array $options See self::$defaultConnectionOptions
* @param array $options See self::$defaultConnectionOptions
*
* @throws InvalidArgumentException when the DSN is invalid
*
* @return \Redis|\RedisCluster|RedisClusterProxy|RedisProxy|\Predis\ClientInterface According to the "class" option
*/
public static function createConnection($dsn, array $options = [])
public static function createConnection(string $dsn, array $options = [])
{
if (0 === strpos($dsn, 'redis:')) {
$scheme = 'redis';
Expand Down Expand Up @@ -498,7 +490,7 @@ protected function doSave(array $values, int $lifetime)
return $failed;
}

private function pipeline(\Closure $generator, $redis = null): \Generator
private function pipeline(\Closure $generator, object $redis = null): \Generator
{
$ids = [];
$redis = $redis ?? $this->redis;
Expand Down