Skip to content

Commit

Permalink
minor #33508 [Cache] Add types to constructors and private/final/inte…
Browse files Browse the repository at this point in the history
…rnal methods (derrabus)

This PR was merged into the 4.4 branch.

Discussion
----------

[Cache] Add types to constructors and private/final/internal methods

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #32179, #33228
| License       | MIT
| Doc PR        | N/A

I'm currently preparing a large PR collecting changes like these. However, the changeset for the Cache component was large enough to justify a dedicated PR, imho.

Commits
-------

919afd2 [Cache] Add types to constructors and private/final/internal methods.
  • Loading branch information
nicolas-grekas committed Sep 9, 2019
2 parents 81ac674 + 919afd2 commit 312cbf9
Show file tree
Hide file tree
Showing 63 changed files with 111 additions and 160 deletions.
2 changes: 1 addition & 1 deletion src/Symfony/Component/Cache/Adapter/ProxyAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ private function generateItems(iterable $items)
}
}

private function getId($key)
private function getId($key): string
{
CacheItem::validateKey($key);

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Cache/CacheItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public static function validateKey($key): string
*
* @internal
*/
public static function log(LoggerInterface $logger = null, $message, $context = [])
public static function log(?LoggerInterface $logger, string $message, array $context = [])
{
if ($logger) {
$logger->warning($message, $context);
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/Cache/LockRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ public static function compute(callable $callback, ItemInterface $item, bool &$s
$logger && $logger->info('Item "{key}" not found while lock was released, now retrying', ['key' => $item->getKey()]);
}
}

return null;
}

private static function open(int $key)
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Cache/Simple/AbstractCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ private function normalizeTtl($ttl)
throw new InvalidArgumentException(sprintf('Expiration date must be an integer, a DateInterval or null, "%s" given', \is_object($ttl) ? \get_class($ttl) : \gettype($ttl)));
}

private function generateValues(iterable $values, array &$keys, $default)
private function generateValues(iterable $values, array &$keys, $default): iterable
{
try {
foreach ($values as $id => $value) {
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Cache/Simple/ChainCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function getMultiple($keys, $default = null)
return $this->generateItems($this->caches[0]->getMultiple($keys, $miss), 0, $miss, $default);
}

private function generateItems(iterable $values, int $cacheIndex, $miss, $default)
private function generateItems(iterable $values, int $cacheIndex, $miss, $default): iterable
{
$missing = [];
$nextCacheIndex = $cacheIndex + 1;
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Cache/Simple/PhpArrayCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public function setMultiple($values, $ttl = null)
return $saved;
}

private function generateItems(array $keys, $default)
private function generateItems(array $keys, $default): iterable
{
$fallbackKeys = [];

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Cache/Simple/TraceableCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public function getCalls()
}
}

private function start(string $name)
private function start(string $name): TraceableCacheEvent
{
$this->calls[] = $event = new TraceableCacheEvent();
$event->name = $name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ abstract class AbstractRedisAdapterTest extends AdapterTestCase

protected static $redis;

public function createCachePool($defaultLifetime = 0): CacheItemPoolInterface
public function createCachePool(int $defaultLifetime = 0): CacheItemPoolInterface
{
return new RedisAdapter(self::$redis, str_replace('\\', '.', __CLASS__), $defaultLifetime);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ApcuAdapterTest extends AdapterTestCase
'testDefaultLifeTime' => 'Testing expiration slows down the test suite',
];

public function createCachePool($defaultLifetime = 0): CacheItemPoolInterface
public function createCachePool(int $defaultLifetime = 0): CacheItemPoolInterface
{
if (!\function_exists('apcu_fetch') || !filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOLEAN)) {
$this->markTestSkipped('APCu extension is required.');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ArrayAdapterTest extends AdapterTestCase
'testSaveWithoutExpire' => 'Assumes a shared cache which ArrayAdapter is not.',
];

public function createCachePool($defaultLifetime = 0): CacheItemPoolInterface
public function createCachePool(int $defaultLifetime = 0): CacheItemPoolInterface
{
return new ArrayAdapter($defaultLifetime);
}
Expand Down
34 changes: 7 additions & 27 deletions src/Symfony/Component/Cache/Tests/Adapter/ChainAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Symfony\Component\Cache\Tests\Adapter;

use PHPUnit\Framework\MockObject\MockObject;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\Cache\Adapter\AdapterInterface;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
Expand All @@ -26,7 +25,7 @@
*/
class ChainAdapterTest extends AdapterTestCase
{
public function createCachePool($defaultLifetime = 0, $testMethod = null): CacheItemPoolInterface
public function createCachePool(int $defaultLifetime = 0, string $testMethod = null): CacheItemPoolInterface
{
if ('testGetMetadata' === $testMethod) {
return new ChainAdapter([new FilesystemAdapter('', $defaultLifetime)], $defaultLifetime);
Expand Down Expand Up @@ -70,14 +69,9 @@ public function testPrune()
$this->assertFalse($cache->prune());
}

/**
* @return MockObject|PruneableCacheInterface
*/
private function getPruneableMock()
private function getPruneableMock(): AdapterInterface
{
$pruneable = $this
->getMockBuilder(PruneableCacheInterface::class)
->getMock();
$pruneable = $this->createMock([PruneableInterface::class, AdapterInterface::class]);

$pruneable
->expects($this->atLeastOnce())
Expand All @@ -87,14 +81,9 @@ private function getPruneableMock()
return $pruneable;
}

/**
* @return MockObject|PruneableCacheInterface
*/
private function getFailingPruneableMock()
private function getFailingPruneableMock(): AdapterInterface
{
$pruneable = $this
->getMockBuilder(PruneableCacheInterface::class)
->getMock();
$pruneable = $this->createMock([PruneableInterface::class, AdapterInterface::class]);

$pruneable
->expects($this->atLeastOnce())
Expand All @@ -104,17 +93,8 @@ private function getFailingPruneableMock()
return $pruneable;
}

/**
* @return MockObject|AdapterInterface
*/
private function getNonPruneableMock()
private function getNonPruneableMock(): AdapterInterface
{
return $this
->getMockBuilder(AdapterInterface::class)
->getMock();
return $this->createMock(AdapterInterface::class);
}
}

interface PruneableCacheInterface extends PruneableInterface, AdapterInterface
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class DoctrineAdapterTest extends AdapterTestCase
'testClearPrefix' => 'Doctrine cannot clear by prefix',
];

public function createCachePool($defaultLifetime = 0): CacheItemPoolInterface
public function createCachePool(int $defaultLifetime = 0): CacheItemPoolInterface
{
return new DoctrineAdapter(new ArrayCache($defaultLifetime), '', $defaultLifetime);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/
class FilesystemAdapterTest extends AdapterTestCase
{
public function createCachePool($defaultLifetime = 0): CacheItemPoolInterface
public function createCachePool(int $defaultLifetime = 0): CacheItemPoolInterface
{
return new FilesystemAdapter('', $defaultLifetime);
}
Expand All @@ -29,7 +29,7 @@ public static function tearDownAfterClass(): void
self::rmdir(sys_get_temp_dir().'/symfony-cache');
}

public static function rmdir($dir)
public static function rmdir(string $dir)
{
if (!file_exists($dir)) {
return;
Expand All @@ -51,7 +51,7 @@ public static function rmdir($dir)
rmdir($dir);
}

protected function isPruned(CacheItemPoolInterface $cache, $name)
protected function isPruned(CacheItemPoolInterface $cache, string $name): bool
{
$getFileMethod = (new \ReflectionObject($cache))->getMethod('getFile');
$getFileMethod->setAccessible(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class FilesystemTagAwareAdapterTest extends FilesystemAdapterTest
{
use TagAwareTestTrait;

public function createCachePool($defaultLifetime = 0): CacheItemPoolInterface
public function createCachePool(int $defaultLifetime = 0): CacheItemPoolInterface
{
return new FilesystemTagAwareAdapter('', $defaultLifetime);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ abstract class MaxIdLengthAdapter extends AbstractAdapter
{
protected $maxIdLength = 50;

public function __construct($ns)
public function __construct(string $ns)
{
parent::__construct($ns);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static function setUpBeforeClass(): void
}
}

public function createCachePool($defaultLifetime = 0): CacheItemPoolInterface
public function createCachePool(int $defaultLifetime = 0): CacheItemPoolInterface
{
$client = $defaultLifetime ? AbstractAdapter::createConnection('memcached://'.getenv('MEMCACHED_HOST')) : self::$client;

Expand Down Expand Up @@ -73,7 +73,7 @@ public function testBadOptions($name, $value)
MemcachedAdapter::createConnection([], [$name => $value]);
}

public function provideBadOptions()
public function provideBadOptions(): array
{
return [
['foo', 'bar'],
Expand Down Expand Up @@ -109,7 +109,7 @@ public function testOptionSerializer()
/**
* @dataProvider provideServersSetting
*/
public function testServersSetting($dsn, $host, $port)
public function testServersSetting(string $dsn, string $host, int $port)
{
$client1 = MemcachedAdapter::createConnection($dsn);
$client2 = MemcachedAdapter::createConnection([$dsn]);
Expand All @@ -125,7 +125,7 @@ public function testServersSetting($dsn, $host, $port)
$this->assertSame([$expect], array_map($f, $client3->getServerList()));
}

public function provideServersSetting()
public function provideServersSetting(): iterable
{
yield [
'memcached://127.0.0.1/50',
Expand Down Expand Up @@ -166,7 +166,7 @@ public function provideServersSetting()
/**
* @dataProvider provideDsnWithOptions
*/
public function testDsnWithOptions($dsn, array $options, array $expectedOptions)
public function testDsnWithOptions(string $dsn, array $options, array $expectedOptions)
{
$client = MemcachedAdapter::createConnection($dsn, $options);

Expand All @@ -175,7 +175,7 @@ public function testDsnWithOptions($dsn, array $options, array $expectedOptions)
}
}

public function provideDsnWithOptions()
public function provideDsnWithOptions(): iterable
{
if (!class_exists('\Memcached')) {
self::markTestSkipped('Extension memcached required.');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
class NamespacedProxyAdapterTest extends ProxyAdapterTest
{
public function createCachePool($defaultLifetime = 0, $testMethod = null): CacheItemPoolInterface
public function createCachePool(int $defaultLifetime = 0, string $testMethod = null): CacheItemPoolInterface
{
if ('testGetMetadata' === $testMethod) {
return new ProxyAdapter(new FilesystemAdapter(), 'foo', $defaultLifetime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static function tearDownAfterClass(): void
@unlink(self::$dbFile);
}

public function createCachePool($defaultLifetime = 0): CacheItemPoolInterface
public function createCachePool(int $defaultLifetime = 0): CacheItemPoolInterface
{
return new PdoAdapter('sqlite:'.self::$dbFile, 'ns', $defaultLifetime);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static function tearDownAfterClass(): void
@unlink(self::$dbFile);
}

public function createCachePool($defaultLifetime = 0): CacheItemPoolInterface
public function createCachePool(int $defaultLifetime = 0): CacheItemPoolInterface
{
return new PdoAdapter(DriverManager::getConnection(['driver' => 'pdo_sqlite', 'path' => self::$dbFile]), '', $defaultLifetime);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ protected function tearDown(): void
}
}

public function createCachePool($defaultLifetime = 0, $testMethod = null): CacheItemPoolInterface
public function createCachePool(int $defaultLifetime = 0, string $testMethod = null): CacheItemPoolInterface
{
if ('testGetMetadata' === $testMethod || 'testClearPrefix' === $testMethod) {
return new PhpArrayAdapter(self::$file, new FilesystemAdapter());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected function tearDown(): void
}
}

public function createCachePool($defaultLifetime = 0): CacheItemPoolInterface
public function createCachePool(int $defaultLifetime = 0): CacheItemPoolInterface
{
return new PhpArrayAdapter(self::$file, new FilesystemAdapter('php-array-fallback', $defaultLifetime));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static function tearDownAfterClass(): void
FilesystemAdapterTest::rmdir(sys_get_temp_dir().'/symfony-cache');
}

protected function isPruned(CacheItemPoolInterface $cache, $name)
protected function isPruned(CacheItemPoolInterface $cache, string $name): bool
{
$getFileMethod = (new \ReflectionObject($cache))->getMethod('getFile');
$getFileMethod->setAccessible(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protected function setUp(): void
$this->skippedTests['testTagItemExpiry'] = 'Testing expiration slows down the test suite';
}

public function createCachePool($defaultLifetime = 0): CacheItemPoolInterface
public function createCachePool(int $defaultLifetime = 0): CacheItemPoolInterface
{
$this->assertInstanceOf(\Predis\Client::class, self::$redis);
$adapter = new RedisTagAwareAdapter(self::$redis, str_replace('\\', '.', __CLASS__), $defaultLifetime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protected function setUp(): void
$this->skippedTests['testTagItemExpiry'] = 'Testing expiration slows down the test suite';
}

public function createCachePool($defaultLifetime = 0): CacheItemPoolInterface
public function createCachePool(int $defaultLifetime = 0): CacheItemPoolInterface
{
$this->assertInstanceOf(\Predis\Client::class, self::$redis);
$adapter = new RedisTagAwareAdapter(self::$redis, str_replace('\\', '.', __CLASS__), $defaultLifetime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protected function setUp(): void
$this->skippedTests['testTagItemExpiry'] = 'Testing expiration slows down the test suite';
}

public function createCachePool($defaultLifetime = 0): CacheItemPoolInterface
public function createCachePool(int $defaultLifetime = 0): CacheItemPoolInterface
{
$this->assertInstanceOf(\Predis\Client::class, self::$redis);
$adapter = new RedisTagAwareAdapter(self::$redis, str_replace('\\', '.', __CLASS__), $defaultLifetime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ProxyAdapterTest extends AdapterTestCase
'testPrune' => 'ProxyAdapter just proxies',
];

public function createCachePool($defaultLifetime = 0, $testMethod = null): CacheItemPoolInterface
public function createCachePool(int $defaultLifetime = 0, string $testMethod = null): CacheItemPoolInterface
{
if ('testGetMetadata' === $testMethod) {
return new ProxyAdapter(new FilesystemAdapter(), '', $defaultLifetime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Psr16AdapterTest extends AdapterTestCase
'testClearPrefix' => 'SimpleCache cannot clear by prefix',
];

public function createCachePool($defaultLifetime = 0): CacheItemPoolInterface
public function createCachePool(int $defaultLifetime = 0): CacheItemPoolInterface
{
return new Psr16Adapter(new Psr16Cache(new FilesystemAdapter()), '', $defaultLifetime);
}
Expand Down

0 comments on commit 312cbf9

Please sign in to comment.