Skip to content

Commit

Permalink
feat(php8): drop PHP 7.4 & Symfony 4.4 compatibility (#4)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
shavounet authored Sep 21, 2022
1 parent 317ba3d commit 4810f15
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 79 deletions.
7 changes: 2 additions & 5 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,24 @@
}
],
"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",
"phpstan/phpstan": "^1.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"
}
}
15 changes: 8 additions & 7 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
28 changes: 7 additions & 21 deletions src/Cache/Stale.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string,mixed>|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;

Expand Down Expand Up @@ -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
);

Expand Down Expand Up @@ -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]);
}
}
11 changes: 4 additions & 7 deletions src/Event/StaleCacheUsage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions tests/Mock/UnavailableResourceExceptionMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 18 additions & 27 deletions tests/Unit/Cache/StaleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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();

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 4810f15

Please sign in to comment.