From 4810f15bdcac642f37ab8f5ca1fd412bbfa9ede9 Mon Sep 17 00:00:00 2001 From: Valentin Clavreul Date: Wed, 21 Sep 2022 15:29:56 +0200 Subject: [PATCH] feat(php8): drop PHP 7.4 & Symfony 4.4 compatibility (#4) * feat(stale cache): use PHP8 syntax and ensure compatibility with Symfony 6 * feat(stale cache): installed rector to apply more PHP8 features * feat(stale cache): enforce compatibility with symfony/cache-contracts ^3 only, since return values have changed * feat(stale cache): applied rector rules after rebase * feat(stale cache): applied rector rules after rebase & removed php 7.4 from CI * feat(stale cache): dump Symfony 4.4 * feat(stale cache): allow symfony cache contract ^2 --- .github/workflows/ci.yaml | 7 +-- composer.json | 16 +++---- rector.php | 15 ++++--- src/Cache/Stale.php | 28 +++--------- src/Event/StaleCacheUsage.php | 11 ++--- .../Mock/UnavailableResourceExceptionMock.php | 5 +-- tests/Unit/Cache/StaleTest.php | 45 ++++++++----------- 7 files changed, 48 insertions(+), 79 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 8f6a36e..dee818a 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -7,12 +7,9 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php-version: [ '7.4', '8.0', '8.1' ] - symfony-version: [ '4.4.*', '5.4.*', '6.*' ] + php-version: [ '8.0', '8.1' ] + symfony-version: [ '5.4.*', '6.*' ] composer-options: [ '', '--prefer-lowest' ] - exclude: - - php-version: '7.4' - symfony-version: '6.*' fail-fast: false steps: - uses: actions/checkout@master diff --git a/composer.json b/composer.json index c22b5b2..8f29656 100644 --- a/composer.json +++ b/composer.json @@ -26,17 +26,17 @@ } ], "require": { - "php": "^7.4 || ^8.0", + "php": "^8.0", "psr/event-dispatcher": "^1.0", "psr/log": "^1 || ^2 || ^3", - "symfony/dependency-injection": "^4.4 || ^5.4 || ^6.0" , - "symfony/http-kernel": "^4.4 || ^5.4 || ^6.0", - "symfony/cache-contracts": "^1.0 || ^2.0", - "symfony/cache": "^4.4 || ^5.4 || ^6.0", - "symfony/config": "^4.4 || ^5.4 || ^6.0" + "symfony/dependency-injection": "^5.4 || ^6.0" , + "symfony/http-kernel": "^5.4 || ^6.0", + "symfony/cache-contracts": "^2.0 || ^3.0", + "symfony/cache": "^5.4 || ^6.0", + "symfony/config": "^5.4 || ^6.0" }, "require-dev": { - "symfony/var-dumper": "^4.4 || ^5.4 || ^6.0", + "symfony/var-dumper": "^5.4 || ^6.0", "phpunit/phpunit": "^9.5", "phpspec/prophecy-phpunit": "^2.0", "m6web/php-cs-fixer-config": "^2.0", @@ -44,6 +44,6 @@ "rector/rector": "^0.13.8", "symfony/polyfill-php80": "^1.16.0", "friendsofphp/php-cs-fixer": "^3.4.0", - "symfony/flex": "^1.19 || ^2.2" + "symfony/flex": "^2.2" } } diff --git a/rector.php b/rector.php index a428f91..8f02b99 100644 --- a/rector.php +++ b/rector.php @@ -2,18 +2,19 @@ declare(strict_types=1); -use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector; use Rector\Config\RectorConfig; use Rector\Set\ValueObject\LevelSetList; +use Rector\Symfony\Set\SymfonySetList; return static function (RectorConfig $rectorConfig): void { $rectorConfig->paths([ - __DIR__ . '/src' + __DIR__.'/src', + __DIR__.'/tests', ]); - $rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class); - - $rectorConfig->sets([ - LevelSetList::UP_TO_PHP_74 - ]); + $rectorConfig->import(LevelSetList::UP_TO_PHP_80); + $rectorConfig->import(SymfonySetList::SYMFONY_60); + $rectorConfig->import(SymfonySetList::SYMFONY_CODE_QUALITY); + $rectorConfig->import(SymfonySetList::SYMFONY_CONSTRUCTOR_INJECTION); + $rectorConfig->import(SymfonySetList::SYMFONY_STRICT); }; diff --git a/src/Cache/Stale.php b/src/Cache/Stale.php index 328685c..882b222 100644 --- a/src/Cache/Stale.php +++ b/src/Cache/Stale.php @@ -15,32 +15,20 @@ class Stale implements TagAwareCacheInterface { - private CacheInterface $internalCache; - - private EventDispatcherInterface $dispatcher; - - private int $maxStale; - - private ?LoggerInterface $logger; - private ?int $defaultLifetime = null; public function __construct( - CacheInterface $internalCache, - EventDispatcherInterface $dispatcher, - int $maxStale, - ?LoggerInterface $logger = null + private CacheInterface $internalCache, + private EventDispatcherInterface $dispatcher, + private int $maxStale, + private ?LoggerInterface $logger = null ) { - $this->internalCache = $internalCache; - $this->dispatcher = $dispatcher; - $this->maxStale = $maxStale; - $this->logger = $logger; } /** * @param array|null $metadata */ - public function get(string $key, callable $callback, float $beta = null, array &$metadata = null) + public function get(string $key, callable $callback, float $beta = null, array &$metadata = null): mixed { $isHit = true; @@ -83,7 +71,7 @@ public function get(string $key, callable $callback, float $beta = null, array & $this->dispatcher->dispatch(new StaleCacheUsage($exception, $key)); } catch (\Throwable $throwable) { $this->logDebugMessage( - sprintf('Exception %s do not allow stale cache, it will be rethrown', get_class($throwable)), + sprintf('Exception %s do not allow stale cache, it will be rethrown', $throwable::class), $key, $throwable ); @@ -166,8 +154,6 @@ private function shouldTriggerEarlyCacheExpiry(?array $metadata, ?float $beta): private function logDebugMessage(string $message, string $cacheKey, ?\Throwable $throwable = null): void { - if ($this->logger) { - $this->logger->debug($message, ['cache_key' => $cacheKey, 'exception' => $throwable]); - } + $this->logger?->debug($message, ['cache_key' => $cacheKey, 'exception' => $throwable]); } } diff --git a/src/Event/StaleCacheUsage.php b/src/Event/StaleCacheUsage.php index cc5c9e9..e7bcd6d 100644 --- a/src/Event/StaleCacheUsage.php +++ b/src/Event/StaleCacheUsage.php @@ -9,13 +9,10 @@ class StaleCacheUsage extends Event { - private UnavailableResourceException $exception; - private string $cacheKey; - - public function __construct(UnavailableResourceException $exception, string $cacheKey) - { - $this->exception = $exception; - $this->cacheKey = $cacheKey; + public function __construct( + private UnavailableResourceException $exception, + private string $cacheKey + ) { } public function getException(): UnavailableResourceException diff --git a/tests/Mock/UnavailableResourceExceptionMock.php b/tests/Mock/UnavailableResourceExceptionMock.php index 77cde9f..a48fa63 100644 --- a/tests/Mock/UnavailableResourceExceptionMock.php +++ b/tests/Mock/UnavailableResourceExceptionMock.php @@ -8,11 +8,8 @@ class UnavailableResourceExceptionMock extends \Exception implements UnavailableResourceException { - private bool $allowStaleCacheUsage; - - public function __construct(bool $allowStaleCacheUsage) + public function __construct(private bool $allowStaleCacheUsage) { - $this->allowStaleCacheUsage = $allowStaleCacheUsage; } public function allowStaleCacheUsage(): bool diff --git a/tests/Unit/Cache/StaleTest.php b/tests/Unit/Cache/StaleTest.php index a4a6658..9885456 100644 --- a/tests/Unit/Cache/StaleTest.php +++ b/tests/Unit/Cache/StaleTest.php @@ -23,14 +23,11 @@ class StaleTest extends TestCase private const DEFAULT_MAX_STALE = 1800; - /** @var ObjectProphecy|TagAwareCacheInterface */ - private ObjectProphecy $internalCache; + private ObjectProphecy|TagAwareCacheInterface $internalCache; - /** @var ObjectProphecy|EventDispatcherInterface */ - private ObjectProphecy $eventDispatcher; + private ObjectProphecy|EventDispatcherInterface $eventDispatcher; - /** @var ObjectProphecy|LoggerInterface */ - private ObjectProphecy $logger; + private ObjectProphecy|LoggerInterface $logger; private Stale $testedInstance; @@ -51,10 +48,10 @@ public function setUp(): void /** * @dataProvider provideValidCallback */ - public function testGetNewItem($value, callable $callback): void + public function testGetNewItem(mixed $value, callable $callback): void { $key = uniqid('key_', true); - $beta = (float) rand(1, 10); + $beta = (float) random_int(1, 10); $initialExpiry = \DateTimeImmutable::createFromFormat('U.u', (string) microtime(true)) ->modify('+1 hour'); $cacheItem = new CacheItem(); @@ -89,11 +86,11 @@ public function testGetNewItem($value, callable $callback): void /** * @dataProvider provideValidCallback */ - public function testGetItemHitAndForceRefresh($newValue, callable $callback) + public function testGetItemHitAndForceRefresh(mixed $newValue, callable $callback) { $key = uniqid('key_', true); $oldValue = uniqid('old_value_', true); - $beta = (float) rand(1, 10); + $beta = (float) random_int(1, 10); $initialExpiry = \DateTimeImmutable::createFromFormat('U.u', (string) microtime(true)) ->modify('+1 hour'); $cacheItem = new CacheItem(); @@ -154,10 +151,8 @@ public function testGetItemHitAndUseStaleMode(): void { $key = uniqid('key_', true); $value = uniqid('value_', true); - $callback = function () { - throw new UnavailableResourceExceptionMock(true); - }; - $beta = (float) rand(1, 10); + $callback = fn () => throw new UnavailableResourceExceptionMock(true); + $beta = (float) random_int(1, 10); $initialExpiry = \DateTimeImmutable::createFromFormat('U.u', (string) microtime(true)) ->modify('+1 hour'); $cacheItem = new CacheItem(); @@ -198,7 +193,7 @@ public function testGetItemHitAndFailsAndNotAllowedToUseStaleMode(): void { $key = uniqid('key_', true); $value = uniqid('value_', true); - $beta = (float) rand(1, 10); + $beta = (float) random_int(1, 10); $initialExpiry = \DateTimeImmutable::createFromFormat('U.u', (string) microtime(true)) ->modify('+1 hour'); $cacheItem = new CacheItem(); @@ -240,7 +235,7 @@ public function testGetItemHitAndFailsAndCannotUseStaleMode(): void { $key = uniqid('key_', true); $value = uniqid('value_', true); - $beta = (float) rand(1, 10); + $beta = (float) random_int(1, 10); $initialExpiry = \DateTimeImmutable::createFromFormat('U.u', (string) microtime(true)) ->modify('+1 hour'); $cacheItem = new CacheItem(); @@ -287,7 +282,7 @@ public function testGetItemHitWithoutStaleMode(array $metadata): void $key = uniqid('key_', true); $value = uniqid('value_', true); $callback = fn () => self::fail('The passed callback should not be called'); - $beta = (float) rand(1, 10); + $beta = (float) random_int(1, 10); $metadataArgument = Argument::any(); $this->internalCache->get($key, Argument::any(), 0, $metadataArgument) @@ -323,10 +318,8 @@ public function provideMetadataNotInStale(): iterable public function testGetItemMissWithFailingCallback(): void { $key = uniqid('key_', true); - $callback = function () { - throw new UnavailableResourceExceptionMock(true); - }; - $beta = (float) rand(1, 10); + $callback = fn () => throw new UnavailableResourceExceptionMock(true); + $beta = (float) random_int(1, 10); $metadataArgument = Argument::any(); $this->internalCache->get($key, Argument::any(), 0, $metadataArgument) @@ -353,12 +346,12 @@ public function testGetItemMissWithFailingCallback(): void public function testGetItemWithDefaultLifetime() { - $defaultLifetime = rand(100, 200); + $defaultLifetime = random_int(100, 200); $key = uniqid('key_', true); $value = uniqid('value_', true); $callback = fn (ItemInterface $item) => $value; - $beta = (float) rand(1, 10); + $beta = (float) random_int(1, 10); $cacheItem = new CacheItem(); @@ -405,7 +398,7 @@ public function testGetItemHitWithEarlyExpiry(int $ctime, float $beta) $oldValue = uniqid('old_value_', true); $newValue = uniqid('value_', true); $callback = fn () => $newValue; - $beta = (float) rand(1, 10); + $beta = (float) random_int(1, 10); $initialExpiry = \DateTimeImmutable::createFromFormat('U.u', (string) microtime(true)) ->modify('+1 hour'); $cacheItem = new CacheItem(); @@ -500,8 +493,6 @@ private static function assertCacheItemExpiryBetween(float $expiryMin, float $ex private static function getCacheItemExpiry(CacheItem $cacheItem) { - return (\Closure::bind(function (CacheItem $item) { - return $item->expiry; - }, null, CacheItem::class))($cacheItem); + return (\Closure::bind(fn (CacheItem $item) => $item->expiry, null, CacheItem::class))($cacheItem); } }