diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c0c646e..7f4b3f3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -17,7 +17,7 @@ jobs: strategy: fail-fast: false matrix: - php: ['8.1', '8.2', '8.3', '8.4'] + php: ['8.2', '8.3', '8.4', '8.5'] steps: - name: Checkout @@ -51,8 +51,8 @@ jobs: - name: Install dependencies run: composer install --prefer-dist --no-progress --no-interaction - - name: Coding standards (phpcs) - run: composer cs-ci + - name: Coding standards (PHP-CS-Fixer) + run: vendor/bin/php-cs-fixer fix --dry-run --diff - name: Static analysis (PHPStan) run: composer stan diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php new file mode 100644 index 0000000..396d643 --- /dev/null +++ b/.php-cs-fixer.dist.php @@ -0,0 +1,45 @@ +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(false) + ->setRules([ + '@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' => ['arguments', 'array_destructuring', 'arrays', 'match', 'parameters'], + ], + ]) + ->setFinder($finder) + ->setCacheFile(__DIR__ . '/build/php-cs-fixer.cache'); diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..fd02602 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,16 @@ +# Changelog + +All notable changes to `initphp/database` 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) + +- Requires PHP `^8.2` (was `^8.1`); PHP 8.1 is no longer supported. CI now + covers PHP 8.2, 8.3, 8.4 and 8.5. +- Code style moved to PER-CS (php-cs-fixer, shared `.php-cs-fixer.dist.php`), + replacing PHP_CodeSniffer (PSR-12); the codebase has been reformatted + accordingly. The `cs`, `cs-ci` and `cs-fix` Composer scripts now run + php-cs-fixer, and `cs-check` was added. diff --git a/README.md b/README.md index 323e3a6..a01470d 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ If a feature is documented for InitORM, it works here under the InitPHP namespac ## Requirements -- PHP **8.1** or later +- PHP **8.2** or later - `ext-pdo` and a PDO driver for your target database (`pdo_mysql`, `pdo_pgsql`, `pdo_sqlite`, …) ## Installation @@ -99,7 +99,7 @@ Org-wide guidelines (PSR-12, `declare(strict_types=1)`, Conventional Commits, PH Local checks before opening a PR: ```bash -composer qa # phpcs + phpstan + phpunit +composer qa # php-cs-fixer + phpstan + phpunit composer cs-fix # auto-format ``` diff --git a/composer.json b/composer.json index 6c74ab3..d255b8f 100644 --- a/composer.json +++ b/composer.json @@ -32,15 +32,15 @@ } ], "require": { - "php": "^8.1", + "php": "^8.2", "ext-pdo": "*", "initorm/orm": "^2.0" }, "require-dev": { "ext-pdo_sqlite": "*", + "friendsofphp/php-cs-fixer": "^3.88", "phpstan/phpstan": "^1.12", - "phpunit/phpunit": "^10.5", - "squizlabs/php_codesniffer": "^3.10" + "phpunit/phpunit": "^10.5" }, "suggest": { "ext-pdo_mysql": "Required for MySQL / MariaDB connections.", @@ -60,9 +60,10 @@ "scripts": { "test": "phpunit", "test:coverage": "phpunit --coverage-text --coverage-html=build/coverage", - "cs": "phpcs", - "cs-ci": "phpcs --warning-severity=0", - "cs-fix": "phpcbf", + "cs": "@cs-check", + "cs-ci": "@cs-check", + "cs-check": "php-cs-fixer fix --dry-run --diff", + "cs-fix": "php-cs-fixer fix", "stan": "phpstan analyse", "qa": [ "@cs-ci", diff --git a/phpcs.xml.dist b/phpcs.xml.dist deleted file mode 100644 index 1331e0d..0000000 --- a/phpcs.xml.dist +++ /dev/null @@ -1,16 +0,0 @@ - - - PSR-12 baseline for InitPHP/Database. - - src - tests - - - - - - - - - - diff --git a/src/DB.php b/src/DB.php index 66f5461..fb7f187 100644 --- a/src/DB.php +++ b/src/DB.php @@ -33,9 +33,7 @@ final class DB /** * Prevent instantiation — this class is a pure static facade. */ - private function __construct() - { - } + private function __construct() {} /** * Build a Database from the supplied connection (array or @@ -54,7 +52,7 @@ public static function createImmutable(array|ConnectionInterface $connection): D if (self::$database !== null) { throw new DatabaseException( 'An immutable Database instance has already been set. ' - . 'Call DB::replaceImmutable() to swap it explicitly.' + . 'Call DB::replaceImmutable() to swap it explicitly.', ); } @@ -72,7 +70,7 @@ public static function createImmutable(array|ConnectionInterface $connection): D * @param array|ConnectionInterface|DatabaseInterface|null $connection */ public static function replaceImmutable( - array|ConnectionInterface|DatabaseInterface|null $connection + array|ConnectionInterface|DatabaseInterface|null $connection, ): ?DatabaseInterface { if ($connection === null) { self::$database = null; @@ -107,7 +105,7 @@ public static function getDatabase(): DatabaseInterface { if (self::$database === null) { throw new DatabaseException( - 'No immutable Database instance is configured. Call DB::createImmutable($connection) first.' + 'No immutable Database instance is configured. Call DB::createImmutable($connection) first.', ); } diff --git a/src/Database.php b/src/Database.php index 1de08de..db37e6c 100644 --- a/src/Database.php +++ b/src/Database.php @@ -23,6 +23,4 @@ * stacks; if the wrapper ever needs real behaviour, it should be added to * upstream {@see InitORMDatabase} (or composed, not inherited) instead. */ -class Database extends InitORMDatabase -{ -} +class Database extends InitORMDatabase {} diff --git a/src/Entity.php b/src/Entity.php index f6e4181..6782df2 100644 --- a/src/Entity.php +++ b/src/Entity.php @@ -22,6 +22,4 @@ * bypasses {@see InitORMEntity::__set()} and creates a dynamic property * instead — deprecated in PHP 8.2+, fatal in a future PHP version. */ -class Entity extends InitORMEntity -{ -} +class Entity extends InitORMEntity {} diff --git a/src/Model.php b/src/Model.php index 5b2a07d..a04c5f9 100644 --- a/src/Model.php +++ b/src/Model.php @@ -18,6 +18,4 @@ * * No behaviour lives here on purpose — see the same note on {@see Database}. */ -abstract class Model extends InitORMModel -{ -} +abstract class Model extends InitORMModel {} diff --git a/src/Utils/Datatables/Datatables.php b/src/Utils/Datatables/Datatables.php index 34c1722..0e680b0 100644 --- a/src/Utils/Datatables/Datatables.php +++ b/src/Utils/Datatables/Datatables.php @@ -96,7 +96,7 @@ final class Datatables public function __construct( private readonly DatabaseInterface|ModelInterface $db, ?RequestParser $request = null, - ?Renderer $renderer = null + ?Renderer $renderer = null, ) { $this->request = $request ?? RequestParser::fromGlobals(); $this->renderer = $renderer ?? new Renderer(); diff --git a/src/Utils/Datatables/RequestParser.php b/src/Utils/Datatables/RequestParser.php index 2f1c10b..0b8fe74 100644 --- a/src/Utils/Datatables/RequestParser.php +++ b/src/Utils/Datatables/RequestParser.php @@ -33,9 +33,7 @@ final class RequestParser * from whichever transport(s) the caller chose (query string, form * body, JSON body). */ - public function __construct(private readonly array $payload) - { - } + public function __construct(private readonly array $payload) {} /** * Build a parser from the live request — $_GET + $_POST merged, plus the diff --git a/tests/ReservedKeywordRegressionTest.php b/tests/ReservedKeywordRegressionTest.php index 58193eb..53df17f 100644 --- a/tests/ReservedKeywordRegressionTest.php +++ b/tests/ReservedKeywordRegressionTest.php @@ -46,13 +46,13 @@ protected function setUp(): void title TEXT, "order" INTEGER, "select" TEXT - )' + )', ); $this->connection->getPDO()->exec( "INSERT INTO posts (title, \"order\", \"select\") VALUES ('First', 1, 'a'), ('Second', 2, 'b'), - ('Third', 3, 'c')" + ('Third', 3, 'c')", ); } @@ -129,7 +129,7 @@ public function testCompiledSqlActuallyQuotesTheIdentifier(): void $logs[0]['query'], 'Reserved identifiers must be emitted in their quoted form. If this assertion fails, ' . 'upstream initorm/query-builder stopped escaping identifiers — file a bug there before ' - . 'shipping a release.' + . 'shipping a release.', ); } } diff --git a/tests/Support/SqliteHelper.php b/tests/Support/SqliteHelper.php index a0c770d..2b5f61e 100644 --- a/tests/Support/SqliteHelper.php +++ b/tests/Support/SqliteHelper.php @@ -55,13 +55,13 @@ public static function seedUsers(ConnectionInterface $connection): void email TEXT NOT NULL, active INTEGER NOT NULL DEFAULT 1, score INTEGER - )' + )', ); $pdo->exec( "INSERT INTO users (name, email, active, score) VALUES ('Alice', 'alice@example.com', 1, 42), ('Bob', 'bob@example.com', 0, 13), - ('Carol', 'carol@example.com', 1, 99)" + ('Carol', 'carol@example.com', 1, 99)", ); } @@ -77,7 +77,7 @@ public static function seedPostsForGrouping(ConnectionInterface $connection): vo id INTEGER PRIMARY KEY AUTOINCREMENT, user_id INTEGER NOT NULL, title TEXT NOT NULL - )' + )', ); $pdo->exec( "INSERT INTO posts (user_id, title) VALUES @@ -86,7 +86,7 @@ public static function seedPostsForGrouping(ConnectionInterface $connection): vo (2, 'Bob only'), (3, 'Carol first'), (3, 'Carol second'), - (3, 'Carol third')" + (3, 'Carol third')", ); } } diff --git a/tests/Utils/Datatables/DatatablesTest.php b/tests/Utils/Datatables/DatatablesTest.php index 929d8be..5ab9e50 100644 --- a/tests/Utils/Datatables/DatatablesTest.php +++ b/tests/Utils/Datatables/DatatablesTest.php @@ -169,7 +169,7 @@ public function testRendererTransformsTheReturnedData(): void $datatables ->from('users') ->setColumns('id', 'name') - ->addRender('name', fn (string $name): string => '*' . $name . '*'); + ->addRender('name', fn(string $name): string => '*' . $name . '*'); $rows = $datatables->toArray()['data']; $names = array_column($rows, 'name'); diff --git a/tests/Utils/Datatables/RendererTest.php b/tests/Utils/Datatables/RendererTest.php index 12c7a72..82f8fb8 100644 --- a/tests/Utils/Datatables/RendererTest.php +++ b/tests/Utils/Datatables/RendererTest.php @@ -26,7 +26,7 @@ public function testEmptyRendererIsANoOp(): void public function testRegisteredCallbackTransformsTheTargetColumn(): void { $renderer = new Renderer(); - $renderer->add('name', fn (string $value): string => strtoupper($value)); + $renderer->add('name', fn(string $value): string => strtoupper($value)); $output = $renderer->apply([ ['id' => 1, 'name' => 'Alice'], @@ -41,8 +41,8 @@ public function testRegisteredCallbackTransformsTheTargetColumn(): void public function testCallbackReceivesTheFullRow(): void { $renderer = new Renderer(); - $renderer->add('full_name', static fn (?string $value, array $row): string => - ($row['first'] ?? '') . ' ' . ($row['last'] ?? '')); + $renderer->add('full_name', static fn(?string $value, array $row): string + => ($row['first'] ?? '') . ' ' . ($row['last'] ?? '')); $output = $renderer->apply([ ['first' => 'Ada', 'last' => 'Lovelace', 'full_name' => ''], @@ -54,7 +54,7 @@ public function testCallbackReceivesTheFullRow(): void public function testColumnsWithoutAnyRendererPassThrough(): void { $renderer = new Renderer(); - $renderer->add('only_this', fn ($v) => 'X'); + $renderer->add('only_this', fn($v) => 'X'); $output = $renderer->apply([['only_this' => 'a', 'untouched' => 'b']]); @@ -65,7 +65,7 @@ public function testColumnsWithoutAnyRendererPassThrough(): void public function testNonAssociativeRowsArePassedThroughUnchanged(): void { $renderer = new Renderer(); - $renderer->add('name', fn ($v) => 'X'); + $renderer->add('name', fn($v) => 'X'); $object = (object) ['name' => 'Alice']; $output = $renderer->apply([$object]); @@ -76,8 +76,8 @@ public function testNonAssociativeRowsArePassedThroughUnchanged(): void public function testReAddingAColumnOverwritesThePreviousRenderer(): void { $renderer = new Renderer(); - $renderer->add('name', fn ($v) => 'first'); - $renderer->add('name', fn ($v) => 'second'); + $renderer->add('name', fn($v) => 'first'); + $renderer->add('name', fn($v) => 'second'); $output = $renderer->apply([['name' => 'whatever']]); diff --git a/tests/Utils/Datatables/RequestParserTest.php b/tests/Utils/Datatables/RequestParserTest.php index 4340832..91beb54 100644 --- a/tests/Utils/Datatables/RequestParserTest.php +++ b/tests/Utils/Datatables/RequestParserTest.php @@ -90,7 +90,7 @@ public function testOrderEntriesAreNormalisedToAscOrDescPairs(): void self::assertSame( [[2, 'ASC'], [1, 'DESC'], [3, 'DESC']], - $parser->orders() + $parser->orders(), ); }