Skip to content

Commit

Permalink
Apply PHP 7.4 syntax and typed property
Browse files Browse the repository at this point in the history
Signed-off-by: Abdul Malik Ikhsan <samsonasik@gmail.com>
  • Loading branch information
samsonasik committed Aug 11, 2022
1 parent 458095e commit ee55742
Show file tree
Hide file tree
Showing 33 changed files with 99 additions and 106 deletions.
2 changes: 1 addition & 1 deletion psalm-baseline.xml
Expand Up @@ -928,7 +928,7 @@
<code>$event</code>
</MissingClosureParamType>
<MissingClosureReturnType occurrences="1">
<code>function ($event) use ($retVal) {</code>
<code>static function ($event) use ($retVal) {</code>
</MissingClosureReturnType>
<MixedArgument occurrences="1">
<code>$success</code>
Expand Down
Expand Up @@ -44,10 +44,9 @@ final class DeprecatedStorageFactoryConfigurationCheckCommand extends Command
. ' https://docs.laminas.dev/laminas-cache/storage/adapter/#quick-start';

/** @var ArrayAccess<string,mixed> */
private $projectConfiguration;
private ArrayAccess $projectConfiguration;

/** @var DeprecatedSchemaDetectorInterface */
private $deprecatedSchemaDetector;
private DeprecatedSchemaDetectorInterface $deprecatedSchemaDetector;

public function __construct(
ArrayAccess $projectConfiguration,
Expand Down
2 changes: 1 addition & 1 deletion src/Pattern/CaptureCache.php
Expand Up @@ -40,7 +40,7 @@ public function start($pageId = null)
$pageId = $this->detectPageId();
}

ob_start(function ($content) use ($pageId) {
ob_start(function ($content) use ($pageId): bool {
$this->set($content, $pageId);

// http://php.net/manual/function.ob-start.php
Expand Down
15 changes: 4 additions & 11 deletions src/Psr/CacheItemPool/CacheItem.php
Expand Up @@ -16,10 +16,8 @@ final class CacheItem implements CacheItemInterface
{
/**
* Cache key
*
* @var string
*/
private $key;
private string $key;

/**
* Cache value
Expand All @@ -30,20 +28,15 @@ final class CacheItem implements CacheItemInterface

/**
* True if the cache item lookup resulted in a cache hit or if they item is deferred or successfully saved
*
* @var bool
*/
private $isHit = false;
private bool $isHit = false;

/**
* Timestamp item will expire at if expiresAt() called, null otherwise
*
* @var int|null
*/
private $expiration;
private ?int $expiration = null;

/** @var DateTimeZone */
private $utc;
private DateTimeZone $utc;

/**
* @param string $key
Expand Down
5 changes: 2 additions & 3 deletions src/Psr/CacheItemPool/CacheItemPoolDecorator.php
Expand Up @@ -41,11 +41,10 @@ class CacheItemPoolDecorator implements CacheItemPoolInterface
use MaximumKeyLengthTrait;
use SerializationTrait;

/** @var StorageInterface */
private $storage;
private StorageInterface $storage;

/** @var array<string,CacheItem> */
private $deferred = [];
private array $deferred = [];

/**
* PSR-6 requires that all implementing libraries support TTL so the given storage adapter must also support static
Expand Down
13 changes: 6 additions & 7 deletions src/Psr/SimpleCache/SimpleCacheDecorator.php
Expand Up @@ -42,22 +42,21 @@ class SimpleCacheDecorator implements SimpleCacheInterface
*/
public const INVALID_KEY_CHARS = ':@{}()/\\';

/** @var bool */
private $providesPerItemTtl = true;
private bool $providesPerItemTtl = true;

/** @var StorageInterface */
private $storage;
private StorageInterface $storage;

/**
* Reference used by storage when calling getItem() to indicate status of
* operation.
*
* @var null|bool
*
* Use ?bool type make it unserialized, even with null initialized
*/
private $success;

/** @var DateTimeZone */
private $utc;
private DateTimeZone $utc;

public function __construct(StorageInterface $storage)
{
Expand Down Expand Up @@ -92,7 +91,7 @@ public function get($key, $default = null)
throw static::translateThrowable($e);
}

$result = $result ?? $default;
$result ??= $default;
return $this->success ? $result : $default;
}

Expand Down
7 changes: 3 additions & 4 deletions src/Service/StorageAdapterFactory.php
Expand Up @@ -6,6 +6,7 @@

use InvalidArgumentException;
use Laminas\Cache\Exception;
use Laminas\Cache\Service\StoragePluginFactoryInterface;
use Laminas\Cache\Storage\PluginAwareInterface;
use Laminas\Cache\Storage\StorageInterface;
use Laminas\ServiceManager\PluginManagerInterface;
Expand All @@ -23,11 +24,9 @@ final class StorageAdapterFactory implements StorageAdapterFactoryInterface
{
public const DEFAULT_PLUGIN_PRIORITY = 1;

/** @var PluginManagerInterface */
private $adapters;
private PluginManagerInterface $adapters;

/** @var StoragePluginFactoryInterface */
private $pluginFactory;
private StoragePluginFactoryInterface $pluginFactory;

public function __construct(PluginManagerInterface $adapters, StoragePluginFactoryInterface $pluginFactory)
{
Expand Down
3 changes: 1 addition & 2 deletions src/Service/StoragePluginFactory.php
Expand Up @@ -14,8 +14,7 @@

final class StoragePluginFactory implements StoragePluginFactoryInterface
{
/** @var PluginManagerInterface */
private $plugins;
private PluginManagerInterface $plugins;

public function __construct(PluginManagerInterface $plugins)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Storage/Adapter/AdapterOptions.php
Expand Up @@ -288,7 +288,7 @@ protected function normalizeTtl(&$ttl)
public function toArray()
{
$array = [];
$transform = function ($letters) {
$transform = static function ($letters): string {
$letter = array_shift($letters);
return '_' . strtolower($letter);
};
Expand Down
2 changes: 1 addition & 1 deletion src/Storage/PluginManager.php
Expand Up @@ -64,7 +64,7 @@ final class PluginManager extends AbstractPluginManager
*/
public function build($name, ?array $options = null)
{
$options = $options ?? [];
$options ??= [];
/** @psalm-suppress MixedAssignment */
$plugin = parent::build($name);
if ($options !== [] && $plugin instanceof PluginInterface) {
Expand Down
2 changes: 1 addition & 1 deletion test/Pattern/CallbackCacheTest.php
Expand Up @@ -79,7 +79,7 @@ public function testGenerateKey(): void

$generatedKey = $this->pattern->generateKey($callback, $args);
$usedKey = null;
$this->storage->getEventManager()->attach('setItem.pre', function ($event) use (&$usedKey) {
$this->storage->getEventManager()->attach('setItem.pre', static function ($event) use (&$usedKey): void {
$params = $event->getParams();
$usedKey = $params['key'];
});
Expand Down
2 changes: 1 addition & 1 deletion test/Pattern/ObjectCacheTest.php
Expand Up @@ -71,7 +71,7 @@ public function testGenerateKey(): void

$generatedKey = $this->pattern->generateKey('emptyMethod', $args);
$usedKey = null;
$this->storage->getEventManager()->attach('setItem.pre', function ($event) use (&$usedKey) {
$this->storage->getEventManager()->attach('setItem.pre', static function ($event) use (&$usedKey): void {
$params = $event->getParams();
$usedKey = $params['key'];
});
Expand Down
38 changes: 32 additions & 6 deletions test/Psr/CacheItemPool/CacheItemPoolDecoratorTest.php
Expand Up @@ -35,11 +35,10 @@ final class CacheItemPoolDecoratorTest extends TestCase
/** @var StorageInterface&FlushableInterface&MockObject */
private $storage;

/** @var CacheItemPoolDecorator */
private $adapter;
private ?CacheItemPoolDecorator $adapter;

/** @var array<string,bool|string> */
private $requiredTypes = [
private array $requiredTypes = [
'NULL' => true,
'boolean' => true,
'integer' => true,
Expand Down Expand Up @@ -163,6 +162,8 @@ public function testGetDeferredItem(): void
->with('foo')
->willReturnOnConsecutiveCalls(null);

assert($this->adapter instanceof CacheItemPoolDecorator);

$adapter = $this->adapter;
$item = $adapter->getItem('foo');
$item->set('bar');
Expand Down Expand Up @@ -240,6 +241,8 @@ public function testGetDeferredItems(): void
->with($keys)
->willReturn([]);

assert($this->adapter instanceof CacheItemPoolDecorator);

$adapter = $this->adapter;
$items = $adapter->getItems($keys);
foreach ($items as $item) {
Expand Down Expand Up @@ -275,6 +278,8 @@ public function testGetItemsInvalidKeyThrowsException(): void
{
$this->expectException(InvalidArgumentException::class);
$keys = ['ok'] + $this->getInvalidKeys();

assert($this->adapter instanceof CacheItemPoolDecorator);
$this->adapter->getItems($keys);
}

Expand Down Expand Up @@ -412,6 +417,8 @@ public function testSaveForeignItemThrowsException(): void
{
$this->expectException(InvalidArgumentException::class);
$item = $this->createMock(CacheItemInterface::class);

assert($this->adapter instanceof CacheItemPoolDecorator);
$this->adapter->save($item);
}

Expand Down Expand Up @@ -490,6 +497,8 @@ public function testHasDeferredItemReturnsTrue(): void
->with('foo')
->willReturn(null);

assert($this->adapter instanceof CacheItemPoolDecorator);

$adapter = $this->adapter;
$item = $adapter->getItem('foo');
$adapter->saveDeferred($item);
Expand All @@ -511,6 +520,8 @@ public function testHasExpiredDeferredItemReturnsFalse(): void
->with('foo')
->willReturn(false);

assert($this->adapter instanceof CacheItemPoolDecorator);

$adapter = $this->adapter;
$item = $adapter->getItem('foo');
$item->set('bar');
Expand All @@ -526,6 +537,7 @@ public function testHasExpiredDeferredItemReturnsFalse(): void
public function testHasItemInvalidKeyThrowsException($key)
{
$this->expectException(InvalidArgumentException::class);
assert($this->adapter instanceof CacheItemPoolDecorator);
$this->adapter->hasItem($key);
}

Expand Down Expand Up @@ -714,6 +726,7 @@ public function testDeleteDeferredItem(): void
public function testDeleteItemInvalidKeyThrowsException($key)
{
$this->expectException(InvalidArgumentException::class);
assert($this->adapter instanceof CacheItemPoolDecorator);
$this->adapter->deleteItem($key);
}

Expand Down Expand Up @@ -760,6 +773,8 @@ public function testDeleteDeferredItems(): void
->withConsecutive(['foo'], ['bar'])
->willReturn(false);

assert($this->adapter instanceof CacheItemPoolDecorator);

$adapter = $this->adapter;
foreach ($keys as $key) {
$item = $adapter->getItem($key);
Expand All @@ -777,6 +792,7 @@ public function testDeleteItemsInvalidKeyThrowsException(): void
{
$this->expectException(InvalidArgumentException::class);
$keys = ['ok'] + $this->getInvalidKeys();
assert($this->adapter instanceof CacheItemPoolDecorator);
$this->adapter->deleteItems($keys);
}

Expand All @@ -803,6 +819,8 @@ public function testDeleteItemsInvalidArgumentExceptionRethrown(): void

public function testSaveDeferredReturnsTrue(): void
{
assert($this->adapter instanceof CacheItemPoolDecorator);

$adapter = $this->adapter;
$item = $adapter->getItem('foo');
self::assertTrue($adapter->saveDeferred($item));
Expand All @@ -812,6 +830,7 @@ public function testSaveDeferredForeignItemThrowsException(): void
{
$this->expectException(InvalidArgumentException::class);
$item = $this->createMock(CacheItemInterface::class);
assert($this->adapter instanceof CacheItemPoolDecorator);
$this->adapter->saveDeferred($item);
}

Expand All @@ -832,6 +851,7 @@ public function testCommitReturnsTrue(): void

public function testCommitEmptyReturnsTrue(): void
{
assert($this->adapter instanceof CacheItemPoolDecorator);
self::assertTrue($this->adapter->commit());
}

Expand All @@ -843,6 +863,8 @@ public function testCommitRuntimeExceptionReturnsFalse(): void
->method('setItems')
->willThrowException(new Exception\RuntimeException());

assert($this->adapter instanceof CacheItemPoolDecorator);

$adapter = $this->adapter;
$item = $adapter->getItem('foo');
$adapter->saveDeferred($item);
Expand All @@ -855,9 +877,7 @@ public function testCommitRuntimeExceptionReturnsFalse(): void
*/
public function invalidKeyProvider(): array
{
return array_map(function ($v) {
return [$v];
}, $this->getInvalidKeys());
return array_map(static fn($v) => [$v], $this->getInvalidKeys());
}

/**
Expand Down Expand Up @@ -887,6 +907,7 @@ private function getAdapter(StorageInterface $storage): CacheItemPoolDecorator
protected function tearDown(): void
{
try {
assert($this->adapter instanceof CacheItemPoolDecorator);
$this->adapter->clear();
} catch (Throwable $throwable) {
/** Cleanup deferred items as {@see CacheItemPoolDecorator::__destruct} is gonna try to store them. */
Expand All @@ -909,6 +930,7 @@ public function testCanHandleRemoveItemsReturningNonArray(): void
->with(['foo'])
->willReturn(null);

assert($this->adapter instanceof CacheItemPoolDecorator);
self::assertFalse($this->adapter->deleteItems(['foo']));
}

Expand All @@ -931,6 +953,7 @@ public function testWillVerifyKeyExistenceByUsingHasItemsWhenDeletionWasNotSucce
->with(['foo'])
->willReturn(['foo' => $exists]);

assert($this->adapter instanceof CacheItemPoolDecorator);
self::assertEquals($successful, $this->adapter->deleteItems(['foo']));
}

Expand Down Expand Up @@ -1039,6 +1062,9 @@ public function testKeepsDeferredItemsWhenCommitFails(): void
$failedItem1 = new CacheItem('keyOfFailedItem1', 'foo', false);
$failedItem2 = new CacheItem('keyOfFailedItem2', 'foo', false);
$succeededItem = new CacheItem('keyOfSucceededItem', 'foo', false);

assert($this->adapter instanceof CacheItemPoolDecorator);

$this->adapter->saveDeferred($failedItem1);
$this->adapter->saveDeferred($failedItem2);
$this->adapter->saveDeferred($succeededItem);
Expand Down
3 changes: 1 addition & 2 deletions test/Psr/CacheItemPool/CacheItemTest.php
Expand Up @@ -14,8 +14,7 @@

class CacheItemTest extends TestCase
{
/** @var string */
private $tz;
private string $tz;

protected function setUp(): void
{
Expand Down

0 comments on commit ee55742

Please sign in to comment.