minor #32282 [Cache] [5.0] Add type-hint to the Cache component. (Simperfit)

This PR was merged into the 5.0-dev branch.

Discussion
----------

[Cache] [5.0] Add type-hint to the Cache component.

| Q             | A
| ------------- | ---
| Branch?       | 5.0
| Bug fix?      | no
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | Contribute to #32179   <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | none <!-- required for new features -->

<!--
Replace this notice by a short README for your feature/bugfix. This will help people
understand your PR and can be used as a start for the documentation.

Additionally (see https://symfony.com/roadmap):
 - Bug fixes must be submitted against the lowest maintained branch where they apply
   (lowest branches are regularly merged to upper ones so they get the fixes too).
 - Features and deprecations must be submitted against branch 4.4.
 - Legacy code removals go to the master branch.
-->

Add type-hint in the cache component whenever possible.

Commits
-------

0a41f6c7aa [Cache] [5.0] add type hint to cache
This commit is contained in:
Nicolas Grekas 2019-06-30 23:18:56 +02:00
commit 63f4d13be1
14 changed files with 25 additions and 28 deletions

View File

@ -107,7 +107,7 @@ abstract class AbstractAdapter implements AdapterInterface, CacheInterface, Logg
* *
* @return AdapterInterface * @return AdapterInterface
*/ */
public static function createSystemCache($namespace, $defaultLifetime, $version, $directory, LoggerInterface $logger = null) public static function createSystemCache(string $namespace, int $defaultLifetime, string $version, string $directory, LoggerInterface $logger = null)
{ {
$opcache = new PhpFilesAdapter($namespace, $defaultLifetime, $directory, true); $opcache = new PhpFilesAdapter($namespace, $defaultLifetime, $directory, true);
if (null !== $logger) { if (null !== $logger) {
@ -118,7 +118,7 @@ abstract class AbstractAdapter implements AdapterInterface, CacheInterface, Logg
return $opcache; return $opcache;
} }
$apcu = new ApcuAdapter($namespace, (int) $defaultLifetime / 5, $version); $apcu = new ApcuAdapter($namespace, $defaultLifetime / 5, $version);
if ('cli' === \PHP_SAPI && !filter_var(ini_get('apc.enable_cli'), FILTER_VALIDATE_BOOLEAN)) { if ('cli' === \PHP_SAPI && !filter_var(ini_get('apc.enable_cli'), FILTER_VALIDATE_BOOLEAN)) {
$apcu->setLogger(new NullLogger()); $apcu->setLogger(new NullLogger());
} elseif (null !== $logger) { } elseif (null !== $logger) {
@ -128,11 +128,8 @@ abstract class AbstractAdapter implements AdapterInterface, CacheInterface, Logg
return new ChainAdapter([$apcu, $opcache]); return new ChainAdapter([$apcu, $opcache]);
} }
public static function createConnection($dsn, array $options = []) public static function createConnection(string $dsn, array $options = [])
{ {
if (!\is_string($dsn)) {
throw new InvalidArgumentException(sprintf('The %s() method expect argument #1 to be string, %s given.', __METHOD__, \gettype($dsn)));
}
if (0 === strpos($dsn, 'redis:') || 0 === strpos($dsn, 'rediss:')) { if (0 === strpos($dsn, 'redis:') || 0 === strpos($dsn, 'rediss:')) {
return RedisAdapter::createConnection($dsn, $options); return RedisAdapter::createConnection($dsn, $options);
} }

View File

@ -80,7 +80,7 @@ class ApcuAdapter extends AbstractAdapter
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function doClear($namespace) protected function doClear(string $namespace)
{ {
return isset($namespace[0]) && class_exists('APCuIterator', false) && ('cli' !== \PHP_SAPI || filter_var(ini_get('apc.enable_cli'), FILTER_VALIDATE_BOOLEAN)) return isset($namespace[0]) && class_exists('APCuIterator', false) && ('cli' !== \PHP_SAPI || filter_var(ini_get('apc.enable_cli'), FILTER_VALIDATE_BOOLEAN))
? apcu_delete(new \APCuIterator(sprintf('/^%s/', preg_quote($namespace, '/')), APC_ITER_KEY)) ? apcu_delete(new \APCuIterator(sprintf('/^%s/', preg_quote($namespace, '/')), APC_ITER_KEY))
@ -102,7 +102,7 @@ class ApcuAdapter extends AbstractAdapter
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function doSave(array $values, $lifetime) protected function doSave(array $values, int $lifetime)
{ {
try { try {
if (false === $failures = apcu_store($values, null, $lifetime)) { if (false === $failures = apcu_store($values, null, $lifetime)) {

View File

@ -73,7 +73,7 @@ class DoctrineAdapter extends AbstractAdapter
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function doClear($namespace) protected function doClear(string $namespace)
{ {
$namespace = $this->provider->getNamespace(); $namespace = $this->provider->getNamespace();
@ -98,7 +98,7 @@ class DoctrineAdapter extends AbstractAdapter
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function doSave(array $values, $lifetime) protected function doSave(array $values, int $lifetime)
{ {
return $this->provider->saveMultiple($values, $lifetime); return $this->provider->saveMultiple($values, $lifetime);
} }

View File

@ -234,7 +234,7 @@ class MemcachedAdapter extends AbstractAdapter
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function doSave(array $values, $lifetime) protected function doSave(array $values, int $lifetime)
{ {
if (!$values = $this->marshaller->marshall($values, $failed)) { if (!$values = $this->marshaller->marshall($values, $failed)) {
return $failed; return $failed;
@ -300,7 +300,7 @@ class MemcachedAdapter extends AbstractAdapter
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function doClear($namespace) protected function doClear(string $namespace)
{ {
return '' === $namespace && $this->getClient()->flush(); return '' === $namespace && $this->getClient()->flush();
} }

View File

@ -251,7 +251,7 @@ class PdoAdapter extends AbstractAdapter implements PruneableInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function doClear($namespace) protected function doClear(string $namespace)
{ {
$conn = $this->getConnection(); $conn = $this->getConnection();
@ -292,7 +292,7 @@ class PdoAdapter extends AbstractAdapter implements PruneableInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function doSave(array $values, $lifetime) protected function doSave(array $values, int $lifetime)
{ {
if (!$values = $this->marshaller->marshall($values, $failed)) { if (!$values = $this->marshaller->marshall($values, $failed)) {
return $failed; return $failed;

View File

@ -199,7 +199,7 @@ class PhpFilesAdapter extends AbstractAdapter implements PruneableInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function doSave(array $values, $lifetime) protected function doSave(array $values, int $lifetime)
{ {
$ok = true; $ok = true;
$expiry = $lifetime ? time() + $lifetime : 'PHP_INT_MAX'; $expiry = $lifetime ? time() + $lifetime : 'PHP_INT_MAX';
@ -256,7 +256,7 @@ class PhpFilesAdapter extends AbstractAdapter implements PruneableInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function doClear($namespace) protected function doClear(string $namespace)
{ {
$this->values = []; $this->values = [];

View File

@ -63,7 +63,7 @@ class Psr16Adapter extends AbstractAdapter implements PruneableInterface, Resett
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function doClear($namespace) protected function doClear(string $namespace)
{ {
return $this->pool->clear(); return $this->pool->clear();
} }
@ -79,7 +79,7 @@ class Psr16Adapter extends AbstractAdapter implements PruneableInterface, Resett
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function doSave(array $values, $lifetime) protected function doSave(array $values, int $lifetime)
{ {
return $this->pool->setMultiple($values, 0 === $lifetime ? null : $lifetime); return $this->pool->setMultiple($values, 0 === $lifetime ? null : $lifetime);
} }

View File

@ -33,7 +33,7 @@ class CacheDataCollector extends DataCollector implements LateDataCollectorInter
* @param string $name * @param string $name
* @param TraceableAdapter $instance * @param TraceableAdapter $instance
*/ */
public function addInstance($name, TraceableAdapter $instance) public function addInstance(string $name, TraceableAdapter $instance)
{ {
$this->instances[$name] = $instance; $this->instances[$name] = $instance;
} }

View File

@ -148,7 +148,7 @@ class CachePoolPass implements CompilerPassInterface
} }
} }
private function getNamespace($seed, $id) private function getNamespace(string $seed, string $id)
{ {
return substr(str_replace('/', '-', base64_encode(hash('sha256', $id.$seed, true))), 0, 10); return substr(str_replace('/', '-', base64_encode(hash('sha256', $id.$seed, true))), 0, 10);
} }

View File

@ -92,7 +92,7 @@ class DefaultMarshaller implements MarshallerInterface
/** /**
* @internal * @internal
*/ */
public static function handleUnserializeCallback($class) public static function handleUnserializeCallback(string $class)
{ {
throw new \DomainException('Class not found: '.$class); throw new \DomainException('Class not found: '.$class);
} }

View File

@ -61,7 +61,7 @@ trait AbstractAdapterTrait
* *
* @return bool True if item exists in the cache, false otherwise * @return bool True if item exists in the cache, false otherwise
*/ */
abstract protected function doHave($id); abstract protected function doHave(string $id);
/** /**
* Deletes all items in the pool. * Deletes all items in the pool.
@ -70,7 +70,7 @@ trait AbstractAdapterTrait
* *
* @return bool True if the pool was successfully cleared, false otherwise * @return bool True if the pool was successfully cleared, false otherwise
*/ */
abstract protected function doClear($namespace); abstract protected function doClear(string $namespace);
/** /**
* Removes multiple items from the pool. * Removes multiple items from the pool.
@ -89,7 +89,7 @@ trait AbstractAdapterTrait
* *
* @return array|bool The identifiers that failed to be cached or a boolean stating if caching succeeded or not * @return array|bool The identifiers that failed to be cached or a boolean stating if caching succeeded or not
*/ */
abstract protected function doSave(array $values, $lifetime); abstract protected function doSave(array $values, int $lifetime);
/** /**
* {@inheritdoc} * {@inheritdoc}

View File

@ -51,7 +51,7 @@ trait FilesystemCommonTrait
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function doClear($namespace) protected function doClear(string $namespace)
{ {
$ok = true; $ok = true;

View File

@ -91,7 +91,7 @@ trait FilesystemTrait
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function doSave(array $values, $lifetime) protected function doSave(array $values, int $lifetime)
{ {
$expiresAt = $lifetime ? (time() + $lifetime) : 0; $expiresAt = $lifetime ? (time() + $lifetime) : 0;
$values = $this->marshaller->marshall($values, $failed); $values = $this->marshaller->marshall($values, $failed);

View File

@ -332,7 +332,7 @@ trait RedisTrait
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function doClear($namespace) protected function doClear(string $namespace)
{ {
$cleared = true; $cleared = true;
if ($this->redis instanceof \Predis\Client) { if ($this->redis instanceof \Predis\Client) {
@ -399,7 +399,7 @@ trait RedisTrait
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function doSave(array $values, $lifetime) protected function doSave(array $values, int $lifetime)
{ {
if (!$values = $this->marshaller->marshall($values, $failed)) { if (!$values = $this->marshaller->marshall($values, $failed)) {
return $failed; return $failed;