From d06e3ad0cdc20f3cd6081cd5bfb5f7894ee1e1e9 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Thu, 2 Mar 2023 21:10:33 +0100 Subject: [PATCH] Trigger runtime deprecations for legacy API --- psalm.xml.dist | 3 +++ src/Connection.php | 18 ++++++++++++++-- src/FetchMode.php | 2 ++ src/Result.php | 23 +++++++++++++++++++-- tests/Functional/LegacyAPITest.php | 33 +++++++++++++++++++++++++++--- 5 files changed, 72 insertions(+), 7 deletions(-) diff --git a/psalm.xml.dist b/psalm.xml.dist index 6256e66d48a..0f94410466a 100644 --- a/psalm.xml.dist +++ b/psalm.xml.dist @@ -51,6 +51,7 @@ --> + @@ -604,6 +605,7 @@ + @@ -748,6 +750,7 @@ + diff --git a/src/Connection.php b/src/Connection.php index 6ba123607ab..14dae59b2cb 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -1957,20 +1957,34 @@ public function executeUpdate(string $sql, array $params = [], array $types = [] /** * BC layer for a wide-spread use-case of old DBAL APIs * - * @deprecated This API is deprecated and will be removed after 2022 + * @deprecated Use {@see executeQuery()} instead */ public function query(string $sql): Result { + Deprecation::trigger( + 'doctrine/dbal', + 'https://github.com/doctrine/dbal/pull/4163', + '%s is deprecated, please use executeQuery() instead.', + __METHOD__, + ); + return $this->executeQuery($sql); } /** * BC layer for a wide-spread use-case of old DBAL APIs * - * @deprecated This API is deprecated and will be removed after 2022 + * @deprecated please use {@see executeStatement()} instead */ public function exec(string $sql): int { + Deprecation::trigger( + 'doctrine/dbal', + 'https://github.com/doctrine/dbal/pull/4163', + '%s is deprecated, please use executeStatement() instead.', + __METHOD__, + ); + return $this->executeStatement($sql); } } diff --git a/src/FetchMode.php b/src/FetchMode.php index 4909bc623e3..d80719f2595 100644 --- a/src/FetchMode.php +++ b/src/FetchMode.php @@ -4,6 +4,8 @@ /** * Legacy Class that keeps BC for using the legacy APIs fetch()/fetchAll(). + * + * @deprecated Use the dedicated fetch*() methods for the desired fetch mode instead. */ class FetchMode { diff --git a/src/Result.php b/src/Result.php index 0426fd48f2e..fdc7daa94d6 100644 --- a/src/Result.php +++ b/src/Result.php @@ -7,6 +7,7 @@ use Doctrine\DBAL\Driver\Exception as DriverException; use Doctrine\DBAL\Driver\Result as DriverResult; use Doctrine\DBAL\Exception\NoKeyValue; +use Doctrine\Deprecations\Deprecation; use LogicException; use Traversable; @@ -261,7 +262,9 @@ private function ensureHasKeyValue(): void /** * BC layer for a wide-spread use-case of old DBAL APIs * - * @deprecated This API is deprecated and will be removed after 2022 + * @deprecated Use {@see fetchNumeric()}, {@see fetchAssociative()} or {@see fetchOne()} instead. + * + * @psalm-param FetchMode::* $mode * * @return mixed * @@ -269,6 +272,13 @@ private function ensureHasKeyValue(): void */ public function fetch(int $mode = FetchMode::ASSOCIATIVE) { + Deprecation::trigger( + 'doctrine/dbal', + 'https://github.com/doctrine/dbal/pull/4007', + '%s is deprecated, please use fetchNumeric(), fetchAssociative() or fetchOne() instead.', + __METHOD__, + ); + if (func_num_args() > 1) { throw new LogicException('Only invocations with one argument are still supported by this legacy API.'); } @@ -291,7 +301,9 @@ public function fetch(int $mode = FetchMode::ASSOCIATIVE) /** * BC layer for a wide-spread use-case of old DBAL APIs * - * @deprecated This API is deprecated and will be removed after 2022 + * @deprecated Use {@see fetchAllNumeric()}, {@see fetchAllAssociative()} or {@see fetchFirstColumn()} instead. + * + * @psalm-param FetchMode::* $mode * * @return list * @@ -299,6 +311,13 @@ public function fetch(int $mode = FetchMode::ASSOCIATIVE) */ public function fetchAll(int $mode = FetchMode::ASSOCIATIVE): array { + Deprecation::trigger( + 'doctrine/dbal', + 'https://github.com/doctrine/dbal/pull/4007', + '%s is deprecated, please use fetchAllNumeric(), fetchAllAssociative() or fetchFirstColumn() instead.', + __METHOD__, + ); + if (func_num_args() > 1) { throw new LogicException('Only invocations with one argument are still supported by this legacy API.'); } diff --git a/tests/Functional/LegacyAPITest.php b/tests/Functional/LegacyAPITest.php index 0b7890964fc..6ad4519bda4 100644 --- a/tests/Functional/LegacyAPITest.php +++ b/tests/Functional/LegacyAPITest.php @@ -5,6 +5,7 @@ use Doctrine\DBAL\FetchMode; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Tests\FunctionalTestCase; +use Doctrine\Deprecations\PHPUnit\VerifyDeprecations; use LogicException; use function array_change_key_case; @@ -14,6 +15,8 @@ class LegacyAPITest extends FunctionalTestCase { + use VerifyDeprecations; + protected function setUp(): void { $table = new Table('legacy_table'); @@ -39,7 +42,10 @@ public function testFetchWithAssociativeMode(): void $sql = 'SELECT test_int FROM legacy_table WHERE test_int = 1'; $stmt = $this->connection->executeQuery($sql); - $row = array_change_key_case($stmt->fetch(FetchMode::ASSOCIATIVE), CASE_LOWER); + + $this->expectDeprecationWithIdentifier('https://github.com/doctrine/dbal/pull/4007'); + + $row = array_change_key_case($stmt->fetch(FetchMode::ASSOCIATIVE), CASE_LOWER); self::assertEquals(1, $row['test_int']); } @@ -48,7 +54,10 @@ public function testFetchWithNumericMode(): void $sql = 'SELECT test_int FROM legacy_table WHERE test_int = 1'; $stmt = $this->connection->executeQuery($sql); - $row = $stmt->fetch(FetchMode::NUMERIC); + + $this->expectDeprecationWithIdentifier('https://github.com/doctrine/dbal/pull/4007'); + + $row = $stmt->fetch(FetchMode::NUMERIC); self::assertEquals(1, $row[0]); } @@ -57,7 +66,10 @@ public function testFetchWithColumnMode(): void $sql = 'SELECT test_int FROM legacy_table WHERE test_int = 1'; $stmt = $this->connection->executeQuery($sql); - $row = $stmt->fetch(FetchMode::COLUMN); + + $this->expectDeprecationWithIdentifier('https://github.com/doctrine/dbal/pull/4007'); + + $row = $stmt->fetch(FetchMode::COLUMN); self::assertEquals(1, $row); } @@ -89,6 +101,8 @@ public function testFetchAllWithAssociativeModes(): void $stmt = $this->connection->executeQuery($sql); + $this->expectDeprecationWithIdentifier('https://github.com/doctrine/dbal/pull/4007'); + $rows = $stmt->fetchAll(FetchMode::ASSOCIATIVE); $rows = array_map(static function ($row) { return array_change_key_case($row, CASE_LOWER); @@ -102,6 +116,9 @@ public function testFetchAllWithNumericModes(): void $sql = 'SELECT test_int FROM legacy_table WHERE test_int = 1'; $stmt = $this->connection->executeQuery($sql); + + $this->expectDeprecationWithIdentifier('https://github.com/doctrine/dbal/pull/4007'); + $rows = $stmt->fetchAll(FetchMode::NUMERIC); self::assertEquals([[0 => 1]], $rows); } @@ -111,6 +128,9 @@ public function testFetchAllWithColumnMode(): void $sql = 'SELECT test_int FROM legacy_table WHERE test_int = 1'; $stmt = $this->connection->executeQuery($sql); + + $this->expectDeprecationWithIdentifier('https://github.com/doctrine/dbal/pull/4007'); + $rows = $stmt->fetchAll(FetchMode::COLUMN); self::assertEquals([1], $rows); } @@ -148,12 +168,17 @@ public function testExecuteUpdate(): void $sql = 'SELECT test_string FROM legacy_table'; $stmt = $this->connection->executeQuery($sql); + + $this->expectDeprecationWithIdentifier('https://github.com/doctrine/dbal/pull/4007'); + $rows = $stmt->fetchAll(FetchMode::COLUMN); self::assertEquals(['foo', 'bar'], $rows); } public function testQuery(): void { + $this->expectDeprecationWithIdentifier('https://github.com/doctrine/dbal/pull/4163'); + $stmt = $this->connection->query('SELECT test_string FROM legacy_table WHERE test_int = 1'); $this->assertEquals('foo', $stmt->fetchOne()); @@ -166,6 +191,8 @@ public function testExec(): void 'test_string' => 'bar', ]); + $this->expectDeprecationWithIdentifier('https://github.com/doctrine/dbal/pull/4163'); + $count = $this->connection->exec('DELETE FROM legacy_table WHERE test_int > 1'); $this->assertEquals(1, $count);