Skip to content

Commit

Permalink
Trigger runtime deprecations for legacy API
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus authored and phansys committed Apr 17, 2023
1 parent c21fe23 commit d06e3ad
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 7 deletions.
3 changes: 3 additions & 0 deletions psalm.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
-->
<referencedClass name="Doctrine\DBAL\Driver\ServerInfoAwareConnection"/>
<referencedClass name="Doctrine\DBAL\VersionAwarePlatformDriver"/>
<referencedClass name="Doctrine\DBAL\FetchMode"/>
<!--
TODO: remove in 4.0.0
-->
Expand Down Expand Up @@ -604,6 +605,7 @@
<InvalidArgument>
<errorLevel type="suppress">
<!-- We're testing with invalid input here. -->
<file name="tests/Functional/LegacyAPITest.php"/>
<file name="tests/Platforms/AbstractPlatformTestCase.php"/>
</errorLevel>
</InvalidArgument>
Expand Down Expand Up @@ -748,6 +750,7 @@

<!-- We're checking for invalid input. -->
<directory name="src/Driver/PgSQL"/>
<file name="src/Result.php"/>
</errorLevel>
</RedundantConditionGivenDocblockType>
<RedundantPropertyInitializationCheck>
Expand Down
18 changes: 16 additions & 2 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 2 additions & 0 deletions src/FetchMode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
23 changes: 21 additions & 2 deletions src/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -261,14 +262,23 @@ 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
*
* @throws Exception
*/
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.');
}
Expand All @@ -291,14 +301,23 @@ 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<mixed>
*
* @throws Exception
*/
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.');
}
Expand Down
33 changes: 30 additions & 3 deletions tests/Functional/LegacyAPITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -14,6 +15,8 @@

class LegacyAPITest extends FunctionalTestCase
{
use VerifyDeprecations;

protected function setUp(): void
{
$table = new Table('legacy_table');
Expand All @@ -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']);
}

Expand All @@ -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]);
}

Expand All @@ -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);
}

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

0 comments on commit d06e3ad

Please sign in to comment.