From 34db58dbf62a6a442f45c46542fa1ede1f64d096 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Muhammet=20=C5=9Eafak?= Date: Sun, 22 Nov 2026 13:27:18 +0300 Subject: [PATCH] build!: require PHP ^8.2 and adopt PER-CS Raise the minimum PHP version to 8.2, run CI on PHP 8.2-8.5, adopt the shared php-cs-fixer configuration (PER-CS) and reformat the sources. --- .github/workflows/ci.yml | 6 +-- .php-cs-fixer.dist.php | 47 ++++++++++++++-------- CHANGELOG.md | 10 +++++ README.md | 4 +- composer.json | 4 +- src/BaseHandler.php | 12 +++--- src/Cache.php | 2 +- src/Exception/CacheException.php | 4 +- src/Exception/InvalidArgumentException.php | 4 +- src/Handler/File.php | 6 +-- src/Handler/Memcache.php | 2 +- src/Handler/PDO.php | 4 +- src/Handler/Redis.php | 4 +- src/Handler/Wincache.php | 2 +- tests/Fixtures/ArrayHandler.php | 2 +- tests/Fixtures/ProbeHandler.php | 4 +- tests/Fixtures/RejectingHandler.php | 2 +- tests/Fixtures/UnsupportedHandler.php | 2 +- tests/Handler/FileHandlerTest.php | 2 +- tests/Handler/PdoHandlerTest.php | 4 +- 20 files changed, 73 insertions(+), 54 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index da16b6d..9c7178c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -33,7 +33,7 @@ jobs: - name: Install dependencies run: composer install --prefer-dist --no-progress --no-interaction - name: Check coding standards - run: composer cs-check + run: vendor/bin/php-cs-fixer fix --dry-run --diff stan: name: PHPStan @@ -58,10 +58,10 @@ jobs: strategy: fail-fast: false matrix: - php: ["8.0", "8.1", "8.2", "8.3", "8.4"] + php: ["8.2", "8.3", "8.4", "8.5"] deps: ["highest"] include: - - php: "8.0" + - php: "8.2" deps: "lowest" steps: - uses: actions/checkout@v4 diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index 87406a8..396d643 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -2,31 +2,44 @@ declare(strict_types=1); +/* + * Shared php-cs-fixer configuration for every InitPHP package (ADR-005). + * + * This file is identical in all repositories as `.php-cs-fixer.dist.php`. + * The only package-specific part is the two lines below; scripts/check-cs-config.sh + * ignores exactly those two lines when it compares a package with this template. + * Keep each of them on a single line. + */ +$excludeDirs = []; +$excludePaths = []; + $finder = PhpCsFixer\Finder::create() ->in([__DIR__ . '/src', __DIR__ . '/tests']) ->name('*.php'); +foreach ($excludeDirs as $excludeDir) { + $finder->exclude($excludeDir); +} + +foreach ($excludePaths as $excludePath) { + $finder->notPath($excludePath); +} + return (new PhpCsFixer\Config()) - ->setRiskyAllowed(true) + ->setRiskyAllowed(false) ->setRules([ - '@PSR12' => true, - '@PSR12:risky' => true, - '@PHP80Migration' => true, - '@PHP80Migration:risky' => true, - 'array_syntax' => ['syntax' => 'short'], - 'declare_strict_types' => true, - 'native_function_invocation' => [ - 'include' => ['@compiler_optimized'], - 'scope' => 'namespaced', - 'strict' => true, - ], - 'no_unused_imports' => true, - 'ordered_imports' => [ - 'imports_order' => ['class', 'function', 'const'], + '@PER-CS' => true, + '@PHP8x2Migration' => true, + 'array_syntax' => ['syntax' => 'short'], + 'no_unused_imports' => true, + 'ordered_imports' => [ + 'imports_order' => ['class', 'function', 'const'], 'sort_algorithm' => 'alpha', ], - 'single_quote' => true, - 'trailing_comma_in_multiline' => ['elements' => ['arrays']], + 'single_quote' => true, + 'trailing_comma_in_multiline' => [ + 'elements' => ['arguments', 'array_destructuring', 'arrays', 'match', 'parameters'], + ], ]) ->setFinder($finder) ->setCacheFile(__DIR__ . '/build/php-cs-fixer.cache'); diff --git a/CHANGELOG.md b/CHANGELOG.md index e567ec7..c032861 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,16 @@ All notable changes to `initphp/cache` are documented here. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] + +### Changed + +- **BREAKING:** Raised the minimum PHP version to 8.2 (`"php": "^8.2"`). The + CI matrix now covers PHP 8.2, 8.3, 8.4 and 8.5. +- **BREAKING:** The code style moved to PER-CS, enforced by the shared + PHP-CS-Fixer configuration (`.php-cs-fixer.dist.php`). The source was + reformatted accordingly; there are no behaviour changes. + ## [1.0.0] The first stable release: a modern PHP 8 codebase, real bug fixes, full diff --git a/README.md b/README.md index 2f111aa..cd45a18 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ Redis, Memcache(d) and WinCache. ## Requirements -- PHP 8.0 or higher +- PHP 8.2 or higher - [`psr/simple-cache`](https://packagist.org/packages/psr/simple-cache) `^3.0` Each handler may need its own PHP extension: @@ -122,7 +122,7 @@ configuration and the exact semantics of TTLs, keys and counters. ## Contributing Bug reports and pull requests are welcome. CI runs PHP-CS-Fixer, PHPStan (max -level) and PHPUnit across PHP 8.0–8.4, plus a Redis/Memcached integration job. +level) and PHPUnit across PHP 8.2–8.5, plus a Redis/Memcached integration job. Run the same static bundle locally with: ```bash diff --git a/composer.json b/composer.json index 61f2ee6..a2f60ae 100644 --- a/composer.json +++ b/composer.json @@ -29,13 +29,13 @@ "docs": "https://github.com/InitPHP/Cache/tree/main/docs" }, "require": { - "php": ">=8.0", + "php": "^8.2", "psr/simple-cache": "^3.0" }, "require-dev": { "phpunit/phpunit": "^9.6", "phpstan/phpstan": "^1.12 || ^2.1", - "friendsofphp/php-cs-fixer": "^3.65" + "friendsofphp/php-cs-fixer": "^3.95" }, "suggest": { "ext-pdo": "To use the PDO handler.", diff --git a/src/BaseHandler.php b/src/BaseHandler.php index 4733a4b..bc2fe3e 100644 --- a/src/BaseHandler.php +++ b/src/BaseHandler.php @@ -77,13 +77,13 @@ public function __construct(array $options = []) if (!$this->isSupported()) { throw new CacheException(\sprintf( 'The "%s" cache handler is not supported in this environment; the required extension is missing or disabled.', - static::class + static::class, )); } $this->options = array_merge( self::DEFAULT_OPTIONS, array_change_key_case($this->handlerOptions), - array_change_key_case($options) + array_change_key_case($options), ); } @@ -145,7 +145,7 @@ abstract public function get(string $key, mixed $default = null): mixed; /** * @inheritDoc */ - abstract public function set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool; + abstract public function set(string $key, mixed $value, int|DateInterval|null $ttl = null): bool; /** * @inheritDoc @@ -209,7 +209,7 @@ public function getMultiple(iterable $keys, mixed $default = null): iterable * * @param iterable $values */ - public function setMultiple(iterable $values, null|int|DateInterval $ttl = null): bool + public function setMultiple(iterable $values, int|DateInterval|null $ttl = null): bool { $result = true; foreach ($values as $key => $value) { @@ -254,7 +254,7 @@ protected function validateKey(string $key): string throw new InvalidArgumentException(\sprintf( 'The cache key "%s" contains a reserved character. The following are reserved: %s', $key, - self::RESERVED_CHARACTERS + self::RESERVED_CHARACTERS, )); } @@ -285,7 +285,7 @@ protected function name(string $key): string * @param null|int|DateInterval $ttl * @return int|null */ - protected function ttlToSeconds(null|int|DateInterval $ttl): ?int + protected function ttlToSeconds(int|DateInterval|null $ttl): ?int { if ($ttl === null) { return null; diff --git a/src/Cache.php b/src/Cache.php index 952c428..c1d8d0f 100644 --- a/src/Cache.php +++ b/src/Cache.php @@ -57,7 +57,7 @@ public static function create(string|CacheInterface $handler, array $options = [ throw new CacheException(\sprintf( 'The cache handler must implement %s.', - CacheInterface::class + CacheInterface::class, )); } } diff --git a/src/Exception/CacheException.php b/src/Exception/CacheException.php index 5cfc7a5..4954c2f 100644 --- a/src/Exception/CacheException.php +++ b/src/Exception/CacheException.php @@ -25,6 +25,4 @@ * Implements {@see PsrCacheException} so that, per PSR-16, every exception the * library throws can be caught through the PSR interface. */ -class CacheException extends Exception implements PsrCacheException -{ -} +class CacheException extends Exception implements PsrCacheException {} diff --git a/src/Exception/InvalidArgumentException.php b/src/Exception/InvalidArgumentException.php index 60df240..75c8eeb 100644 --- a/src/Exception/InvalidArgumentException.php +++ b/src/Exception/InvalidArgumentException.php @@ -25,6 +25,4 @@ * Extends the SPL {@see PhpInvalidArgumentException} and implements the PSR-16 * {@see PsrInvalidArgumentException}, so it can be caught either way. */ -class InvalidArgumentException extends PhpInvalidArgumentException implements PsrInvalidArgumentException -{ -} +class InvalidArgumentException extends PhpInvalidArgumentException implements PsrInvalidArgumentException {} diff --git a/src/Handler/File.php b/src/Handler/File.php index 23e8b66..5fde1b4 100644 --- a/src/Handler/File.php +++ b/src/Handler/File.php @@ -58,7 +58,7 @@ class File extends BaseHandler */ protected array $handlerOptions = [ 'path' => null, - 'mode' => 0640, + 'mode' => 0o640, ]; /** @@ -74,7 +74,7 @@ public function get(string $key, mixed $default = null): mixed /** * @inheritDoc */ - public function set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool + public function set(string $key, mixed $value, int|DateInterval|null $ttl = null): bool { $name = $this->name($key); $seconds = $this->ttlToSeconds($ttl); @@ -147,7 +147,7 @@ private function write(string $path, string $content): bool if (@file_put_contents($path, $content) === false) { return false; } - $mode = $this->getOption('mode', 0640); + $mode = $this->getOption('mode', 0o640); if (\is_int($mode)) { @chmod($path, $mode); } diff --git a/src/Handler/Memcache.php b/src/Handler/Memcache.php index 9b59719..6e2d30f 100644 --- a/src/Handler/Memcache.php +++ b/src/Handler/Memcache.php @@ -84,7 +84,7 @@ public function get(string $key, mixed $default = null): mixed /** * @inheritDoc */ - public function set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool + public function set(string $key, mixed $value, int|DateInterval|null $ttl = null): bool { $name = $this->name($key); $seconds = $this->ttlToSeconds($ttl); diff --git a/src/Handler/PDO.php b/src/Handler/PDO.php index 713ab78..5b0ffb4 100644 --- a/src/Handler/PDO.php +++ b/src/Handler/PDO.php @@ -89,7 +89,7 @@ public function get(string $key, mixed $default = null): mixed /** * @inheritDoc */ - public function set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool + public function set(string $key, mixed $value, int|DateInterval|null $ttl = null): bool { $name = $this->name($key); $seconds = $this->ttlToSeconds($ttl); @@ -176,7 +176,7 @@ protected function getPDO(): PdoConnection [ PdoConnection::ATTR_ERRMODE => PdoConnection::ERRMODE_EXCEPTION, PdoConnection::ATTR_DEFAULT_FETCH_MODE => PdoConnection::FETCH_ASSOC, - ] + ], ); } catch (PDOException $e) { throw new CacheException('PDO cache connection error: ' . $e->getMessage(), 0, $e); diff --git a/src/Handler/Redis.php b/src/Handler/Redis.php index bd40076..3f893e4 100644 --- a/src/Handler/Redis.php +++ b/src/Handler/Redis.php @@ -86,7 +86,7 @@ public function get(string $key, mixed $default = null): mixed /** * @inheritDoc */ - public function set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool + public function set(string $key, mixed $value, int|DateInterval|null $ttl = null): bool { $name = $this->name($key); $seconds = $this->ttlToSeconds($ttl); @@ -150,7 +150,7 @@ protected function getRedis(): RedisExtension $connected = $redis->connect( $this->optionString('host', '127.0.0.1'), $this->optionInt('port', 6379), - $this->optionFloat('timeout', 0.0) + $this->optionFloat('timeout', 0.0), ); if (!$connected) { throw new CacheException('Redis cache connection failed.'); diff --git a/src/Handler/Wincache.php b/src/Handler/Wincache.php index b1f1507..e90d6db 100644 --- a/src/Handler/Wincache.php +++ b/src/Handler/Wincache.php @@ -59,7 +59,7 @@ public function get(string $key, mixed $default = null): mixed /** * @inheritDoc */ - public function set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool + public function set(string $key, mixed $value, int|DateInterval|null $ttl = null): bool { $name = $this->name($key); $seconds = $this->ttlToSeconds($ttl); diff --git a/tests/Fixtures/ArrayHandler.php b/tests/Fixtures/ArrayHandler.php index dc27220..2c16793 100644 --- a/tests/Fixtures/ArrayHandler.php +++ b/tests/Fixtures/ArrayHandler.php @@ -31,7 +31,7 @@ public function get(string $key, mixed $default = null): mixed return $this->store[$name]['value']; } - public function set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool + public function set(string $key, mixed $value, int|DateInterval|null $ttl = null): bool { $name = $this->name($key); $seconds = $this->ttlToSeconds($ttl); diff --git a/tests/Fixtures/ProbeHandler.php b/tests/Fixtures/ProbeHandler.php index ca7992d..301ec85 100644 --- a/tests/Fixtures/ProbeHandler.php +++ b/tests/Fixtures/ProbeHandler.php @@ -18,7 +18,7 @@ public function get(string $key, mixed $default = null): mixed return $default; } - public function set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool + public function set(string $key, mixed $value, int|DateInterval|null $ttl = null): bool { return true; } @@ -48,7 +48,7 @@ public function exposeName(string $key): string return $this->name($key); } - public function exposeTtlToSeconds(null|int|DateInterval $ttl): ?int + public function exposeTtlToSeconds(int|DateInterval|null $ttl): ?int { return $this->ttlToSeconds($ttl); } diff --git a/tests/Fixtures/RejectingHandler.php b/tests/Fixtures/RejectingHandler.php index 4e04bfc..9dfb38a 100644 --- a/tests/Fixtures/RejectingHandler.php +++ b/tests/Fixtures/RejectingHandler.php @@ -18,7 +18,7 @@ public function get(string $key, mixed $default = null): mixed return $default; } - public function set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool + public function set(string $key, mixed $value, int|DateInterval|null $ttl = null): bool { return false; } diff --git a/tests/Fixtures/UnsupportedHandler.php b/tests/Fixtures/UnsupportedHandler.php index ac31dbd..76b4528 100644 --- a/tests/Fixtures/UnsupportedHandler.php +++ b/tests/Fixtures/UnsupportedHandler.php @@ -18,7 +18,7 @@ public function get(string $key, mixed $default = null): mixed return $default; } - public function set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool + public function set(string $key, mixed $value, int|DateInterval|null $ttl = null): bool { return false; } diff --git a/tests/Handler/FileHandlerTest.php b/tests/Handler/FileHandlerTest.php index 9d9b74a..255d3a9 100644 --- a/tests/Handler/FileHandlerTest.php +++ b/tests/Handler/FileHandlerTest.php @@ -27,7 +27,7 @@ final class FileHandlerTest extends AbstractCacheContractTestCase protected function setUp(): void { $this->dir = sys_get_temp_dir() . '/initphp-cache-' . uniqid('', true); - mkdir($this->dir, 0777, true); + mkdir($this->dir, 0o777, true); parent::setUp(); } diff --git a/tests/Handler/PdoHandlerTest.php b/tests/Handler/PdoHandlerTest.php index a065c5d..5c9ca92 100644 --- a/tests/Handler/PdoHandlerTest.php +++ b/tests/Handler/PdoHandlerTest.php @@ -30,7 +30,7 @@ protected function setUp(): void $this->raw = new PDO('sqlite:' . $this->dbFile); $this->raw->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $this->raw->exec( - 'CREATE TABLE cache (name VARCHAR(255) NOT NULL, ttl INTEGER NULL DEFAULT NULL, data TEXT NOT NULL, UNIQUE(name))' + 'CREATE TABLE cache (name VARCHAR(255) NOT NULL, ttl INTEGER NULL DEFAULT NULL, data TEXT NOT NULL, UNIQUE(name))', ); parent::setUp(); } @@ -65,7 +65,7 @@ public function testConnectionErrorThrowsCacheException(): void public function testExpiredRowIsTreatedAsMissAndDeleted(): void { $this->raw->exec( - "INSERT INTO cache (name, ttl, data) VALUES ('cache_old', " . (time() - 60) . ", '" . serialize('v') . "')" + "INSERT INTO cache (name, ttl, data) VALUES ('cache_old', " . (time() - 60) . ", '" . serialize('v') . "')", ); self::assertNull($this->cache->get('old'));