diff --git a/.github/workflows/bc_check.yaml b/.github/workflows/bc_check.yaml index 146c787..131ddb5 100644 --- a/.github/workflows/bc_check.yaml +++ b/.github/workflows/bc_check.yaml @@ -17,7 +17,7 @@ jobs: - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: 7.4 + php-version: '8.0' - name: Install Checker run: composer require --dev roave/backward-compatibility-check - name: Check for BC breaks diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 5e9b94a..f464f6a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -13,7 +13,7 @@ jobs: - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: '7.4' + php-version: '8.0' - name: Checkout Code uses: actions/checkout@v2 - name: Install Dependencies @@ -25,12 +25,12 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - symfonyVersion: [ ^4.4, ^5.0 ] + symfonyVersion: [^6.0] steps: - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: '7.4' + php-version: '8.0' - name: Checkout Code uses: actions/checkout@v2 - name: Install Dependencies @@ -44,12 +44,12 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - symfonyVersion: [^4.4, ^5.0] + symfonyVersion: [^6.0] steps: - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: '7.4' + php-version: '8.0' - name: Checkout Code uses: actions/checkout@v2 - name: Install Dependencies @@ -65,7 +65,7 @@ jobs: - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: '7.4' + php-version: '8.0' - name: Checkout Code uses: actions/checkout@v2 - name: Install Dependencies diff --git a/.php_cs.dist b/.php-cs-fixer.dist.php similarity index 95% rename from .php_cs.dist rename to .php-cs-fixer.dist.php index 0bfb0e3..461197e 100644 --- a/.php_cs.dist +++ b/.php-cs-fixer.dist.php @@ -1,6 +1,6 @@ setRules([ '@PSR2' => true, 'array_syntax' => ['syntax' => 'short'], @@ -10,7 +10,7 @@ ], 'cast_spaces' => true, 'class_attributes_separation' => [ - 'elements' => ['method', 'property'], + 'elements' => ['method' => 'one', 'property' => 'one'], ], 'combine_consecutive_unsets' => true, 'compact_nullable_typehint' => true, @@ -70,7 +70,7 @@ 'single_quote' => true, 'standardize_not_equals' => true, 'ternary_operator_spaces' => true, - 'trailing_comma_in_multiline_array' => true, + 'trailing_comma_in_multiline' => true, 'unary_operator_spaces' => true, 'visibility_required' => [ 'elements' => ['property', 'method', 'const'], diff --git a/composer.json b/composer.json index 8d96834..8ce7bbc 100644 --- a/composer.json +++ b/composer.json @@ -5,10 +5,10 @@ "minimum-stability": "stable", "license": "MIT", "require": { - "symfony/framework-bundle": "^4.4 | ^5.0", + "symfony/framework-bundle": "^6.0", "rikudou/psr6-dynamo-db": "^2.0", - "php": "^7.2 | ^8.0", - "symfony/cache": "^4.4 | ^5.0", + "php": "^8.0", + "symfony/cache": "^6.0", "ext-json": "*" }, "autoload": { @@ -17,9 +17,9 @@ } }, "require-dev": { - "friendsofphp/php-cs-fixer": "^2.16", - "phpstan/phpstan": "^0.12.44", - "phpunit/phpunit": "^9.3" + "friendsofphp/php-cs-fixer": "^3.0", + "phpstan/phpstan": "^1.5", + "phpunit/phpunit": "^9.5" }, "autoload-dev": { "psr-4": { diff --git a/phpstan.neon.dist b/phpstan.neon.dist index c1aa00f..a473a4c 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -1,5 +1,4 @@ parameters: - excludes_analyse: - - src/DependencyInjection/Configuration.php - ignoreErrors: - - '#Return type .*? of method .*?DynamoDbCacheAdapter::getItems.*? should be compatible .*#' + excludePaths: + analyse: + - src/DependencyInjection/Configuration.php diff --git a/src/Cache/DynamoDbCacheAdapter.php b/src/Cache/DynamoDbCacheAdapter.php index 73f4ff0..7076580 100644 --- a/src/Cache/DynamoDbCacheAdapter.php +++ b/src/Cache/DynamoDbCacheAdapter.php @@ -16,24 +16,14 @@ final class DynamoDbCacheAdapter implements AdapterInterface, CacheInterface { use CacheTrait; - /** - * @var DynamoDbCache - */ - private $cache; - - /** - * @var SymfonyCacheItemConverter - */ - private $converter; - /** * @param DynamoDbCache $cache * @param SymfonyCacheItemConverter $converter */ - public function __construct(DynamoDbCache $cache, SymfonyCacheItemConverter $converter) - { - $this->cache = $cache; - $this->converter = $converter; + public function __construct( + private DynamoDbCache $cache, + private SymfonyCacheItemConverter $converter + ) { } /** @@ -43,7 +33,7 @@ public function __construct(DynamoDbCache $cache, SymfonyCacheItemConverter $con * * @return CacheItem */ - public function getItem($key) + public function getItem(mixed $key): CacheItem { return $this->converter->convertToCacheItem($this->cache->getItem($key)); } @@ -55,7 +45,7 @@ public function getItem($key) * * @return CacheItem[] */ - public function getItems(array $keys = []) + public function getItems(array $keys = []): iterable { return array_map(function (DynamoCacheItem $item) { return $this->converter->convertToCacheItem($item); @@ -67,7 +57,7 @@ public function getItems(array $keys = []) * * @return bool */ - public function clear(string $prefix = '') + public function clear(string $prefix = ''): bool { return $this->cache->clear(); } @@ -79,7 +69,7 @@ public function clear(string $prefix = '') * * @return bool */ - public function hasItem($key) + public function hasItem(string $key): bool { return $this->cache->hasItem($key); } @@ -91,7 +81,7 @@ public function hasItem($key) * * @return bool */ - public function deleteItem($key) + public function deleteItem(string $key): bool { return $this->cache->deleteItem($key); } @@ -103,7 +93,7 @@ public function deleteItem($key) * * @return bool */ - public function deleteItems(array $keys) + public function deleteItems(array $keys): bool { return $this->cache->deleteItems($keys); } @@ -115,7 +105,7 @@ public function deleteItems(array $keys) * * @return bool */ - public function save(CacheItemInterface $item) + public function save(CacheItemInterface $item): bool { return $this->cache->save($item); } @@ -127,7 +117,7 @@ public function save(CacheItemInterface $item) * * @return bool */ - public function saveDeferred(CacheItemInterface $item) + public function saveDeferred(CacheItemInterface $item): bool { return $this->cache->saveDeferred($item); } @@ -137,7 +127,7 @@ public function saveDeferred(CacheItemInterface $item) * * @return bool */ - public function commit() + public function commit(): bool { return $this->cache->commit(); } diff --git a/src/Converter/SymfonyCacheItemConverter.php b/src/Converter/SymfonyCacheItemConverter.php index f81df68..bb9cc24 100644 --- a/src/Converter/SymfonyCacheItemConverter.php +++ b/src/Converter/SymfonyCacheItemConverter.php @@ -14,20 +14,10 @@ final class SymfonyCacheItemConverter implements CacheItemConverterInterface { - /** - * @var ClockInterface - */ - private $clock; - - /** - * @var CacheItemEncoderInterface - */ - private $encoder; - - public function __construct(ClockInterface $clock, CacheItemEncoderInterface $encoder) - { - $this->clock = $clock; - $this->encoder = $encoder; + public function __construct( + private ClockInterface $clock, + private CacheItemEncoderInterface $encoder, + ) { } public function supports(CacheItemInterface $cacheItem): bool @@ -43,6 +33,8 @@ public function convert(CacheItemInterface $cacheItem): DynamoCacheItem $reflectionExpiry = new ReflectionProperty(CacheItem::class, 'expiry'); $reflectionExpiry->setAccessible(true); $value = $reflectionExpiry->getValue($cacheItem); + assert(is_scalar($value) || $value === null); + if ($value === null) { $expiry = null; } else { @@ -85,7 +77,7 @@ public function convertToCacheItem(DynamoCacheItem $dynamoCacheItem): CacheItem $valueReflection->setValue($item, $dynamoCacheItem->get()); $expiryReflection->setValue( $item, - $expiry ? $expiry->getTimestamp() : null + $expiry?->getTimestamp() ); return $item; diff --git a/src/DependencyInjection/RikudouDynamoDbCacheExtension.php b/src/DependencyInjection/RikudouDynamoDbCacheExtension.php index c170f5f..6244360 100644 --- a/src/DependencyInjection/RikudouDynamoDbCacheExtension.php +++ b/src/DependencyInjection/RikudouDynamoDbCacheExtension.php @@ -44,7 +44,10 @@ private function createDynamoClient(ContainerBuilder $container, array $configs) { if ($configs['client_service']) { $client = $configs['client_service']; + assert(is_string($client)); } else { + assert(is_array($configs['client_config'])); + $service = new Definition(DynamoDbClient::class); $service->addArgument([ 'region' => $configs['client_config']['region'], @@ -77,6 +80,8 @@ private function createCacheClient(ContainerBuilder $container, array $configs, */ private function createDefaultEncoder(ContainerBuilder $container, array $configs): void { + assert(is_array($configs['encoder'])); + $container->removeDefinition('rikudou.dynamo_cache.encoder.default'); $container->setAlias('rikudou.dynamo_cache.encoder.default', $configs['encoder']['service']); } @@ -87,11 +92,19 @@ private function createDefaultEncoder(ContainerBuilder $container, array $config */ private function createParameters(ContainerBuilder $container, array $configs): void { + assert( + is_array($configs['replace_default_adapter']) + || is_scalar($configs['replace_default_adapter']) + || $configs['replace_default_adapter'] === null + ); + $container->setParameter( 'rikudou.dynamo_cache.internal.replace_adapter', $configs['replace_default_adapter'] ); + assert(is_array($configs['encoder'])); + $container->setParameter( 'rikudou.dynamo_cache.json_encoder.encode_flags', $configs['encoder']['json_options']['encode_flags'] @@ -112,6 +125,8 @@ private function createParameters(ContainerBuilder $container, array $configs): */ private function createSessionHandler(ContainerBuilder $container, array $configs): void { + assert(is_array($configs['session'])); + $definition = $container->getDefinition('rikudou.dynamo_cache.session'); $definition->setArgument('$ttl', $configs['session']['ttl']); $definition->setArgument('$prefix', $configs['session']['prefix']); diff --git a/src/Helper/DynamoDbCacheAdapterDecorator.php b/src/Helper/DynamoDbCacheAdapterDecorator.php index e2ddbd6..4827dd8 100644 --- a/src/Helper/DynamoDbCacheAdapterDecorator.php +++ b/src/Helper/DynamoDbCacheAdapterDecorator.php @@ -10,113 +10,78 @@ trait DynamoDbCacheAdapterDecorator { - private $originalAdapter; - - public function __construct(DynamoDbCacheAdapter $originalAdapter) - { - $this->originalAdapter = $originalAdapter; + public function __construct( + private DynamoDbCacheAdapter $originalAdapter + ) { } /** - * @param $key - * * @throws InvalidArgumentException - * - * @return CacheItem */ - public function getItem($key) + public function getItem(mixed $key): CacheItem { return $this->originalAdapter->getItem($key); } /** - * @param array $keys - * * @throws InvalidArgumentException * - * @return CacheItem[] + * @return iterable */ - public function getItems(array $keys = []) + public function getItems(array $keys = []): iterable { return $this->originalAdapter->getItems($keys); } - /** - * @param string $prefix - * - * @return bool - */ - public function clear(string $prefix = '') + public function clear(string $prefix = ''): bool { return $this->originalAdapter->clear($prefix); } /** - * @param $key - * * @throws InvalidArgumentException - * - * @return bool */ - public function hasItem($key) + public function hasItem(string $key): bool { return $this->originalAdapter->hasItem($key); } /** - * @param $key - * * @throws InvalidArgumentException - * - * @return bool */ - public function deleteItem($key) + public function deleteItem(string $key): bool { return $this->originalAdapter->deleteItem($key); } /** - * @param array $keys - * * @throws InvalidArgumentException - * - * @return bool */ - public function deleteItems(array $keys) + public function deleteItems(array $keys): bool { return $this->originalAdapter->deleteItems($keys); } /** - * @param CacheItemInterface $item - * * @throws InvalidArgumentException - * - * @return bool */ - public function save(CacheItemInterface $item) + public function save(CacheItemInterface $item): bool { return $this->originalAdapter->save($item); } /** - * @param CacheItemInterface $item - * * @throws InvalidArgumentException - * - * @return bool */ - public function saveDeferred(CacheItemInterface $item) + public function saveDeferred(CacheItemInterface $item): bool { return $this->originalAdapter->saveDeferred($item); } /** * @throws InvalidArgumentException - * - * @return bool */ - public function commit() + public function commit(): bool { return $this->originalAdapter->commit(); } diff --git a/src/Session/DynamoDbSessionHandler.php b/src/Session/DynamoDbSessionHandler.php index 4999ed3..e654cb5 100644 --- a/src/Session/DynamoDbSessionHandler.php +++ b/src/Session/DynamoDbSessionHandler.php @@ -9,28 +9,16 @@ final class DynamoDbSessionHandler extends AbstractSessionHandler { - /** - * @var DynamoDbCacheAdapter - */ - private $cache; - - /** - * @var string - */ - private $prefix; - /** * @var int */ - private $ttl; + private int $ttl; public function __construct( - DynamoDbCacheAdapter $cache, - string $prefix, + private DynamoDbCacheAdapter $cache, + private string $prefix, ?int $ttl ) { - $this->cache = $cache; - $this->prefix = $prefix; $this->ttl = $ttl ?? (int) ini_get('session.gc_maxlifetime'); } @@ -39,42 +27,34 @@ public function close(): bool return true; } - public function gc($maxlifetime): bool + public function gc(int $max_lifetime): int|false { - return true; + return 0; } /** - * @param string $key - * @param string $val - * * @throws InvalidArgumentException - * - * @return bool */ - public function updateTimestamp($key, $val): bool + public function updateTimestamp(string $id, string $data): bool { - $item = $this->getCacheItem($key); + $item = $this->getCacheItem($id); $item->expiresAfter($this->ttl); return $this->cache->save($item); } - /** - * @param string $sessionId - */ - protected function doRead($sessionId): string + protected function doRead(string $sessionId): string { - return (string) $this->getCacheItem($sessionId)->get(); + $result = $this->getCacheItem($sessionId)->get(); + assert(is_scalar($result) || $result === null); + + return (string) $result; } /** - * @param string $sessionId - * @param string $data - * * @throws InvalidArgumentException */ - protected function doWrite($sessionId, $data): bool + protected function doWrite(string $sessionId, string $data): bool { $item = $this->getCacheItem($sessionId); $item->set($data); @@ -84,11 +64,9 @@ protected function doWrite($sessionId, $data): bool } /** - * @param string $sessionId - * * @throws InvalidArgumentException */ - protected function doDestroy($sessionId): bool + protected function doDestroy(string $sessionId): bool { return $this->cache->deleteItem($this->getCacheKey($sessionId)); } @@ -98,6 +76,9 @@ private function getCacheKey(string $sessionId): string return $this->prefix . $sessionId; } + /** + * @throws InvalidArgumentException + */ private function getCacheItem(string $sessionId): CacheItem { return $this->cache->getItem($this->getCacheKey($sessionId)); diff --git a/tests/Helper/DynamoDbCacheAdapterDecoratorTest.php b/tests/Helper/DynamoDbCacheAdapterDecoratorTest.php index e6aed58..6abf4dc 100644 --- a/tests/Helper/DynamoDbCacheAdapterDecoratorTest.php +++ b/tests/Helper/DynamoDbCacheAdapterDecoratorTest.php @@ -17,15 +17,9 @@ class DynamoDbCacheAdapterDecoratorTest extends AbstractDynamoDbTest { - /** - * @var DynamoDbCacheAdapter - */ - private $originalInstance; + private DynamoDbCacheAdapter $originalInstance; - /** - * @var AdapterInterface|CacheInterface - */ - private $instance; + private AdapterInterface|CacheInterface $instance; protected function setUp(): void { diff --git a/tests/Session/DynamoDbSessionHandlerTest.php b/tests/Session/DynamoDbSessionHandlerTest.php index 78c4dcb..bfc993e 100644 --- a/tests/Session/DynamoDbSessionHandlerTest.php +++ b/tests/Session/DynamoDbSessionHandlerTest.php @@ -68,7 +68,7 @@ public function testClose() public function testGc() { - self::assertTrue($this->instance->gc(1)); + self::assertEquals(0, $this->instance->gc(1)); } public function testDoRead()