Skip to content

Commit fbe98f7

Browse files
authored
Merge pull request #5948 from derrabus/deprecate/fetch-mode
Trigger runtime deprecations for legacy API
2 parents 2ebf3cf + fff363d commit fbe98f7

File tree

5 files changed

+72
-7
lines changed

5 files changed

+72
-7
lines changed

psalm.xml.dist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
-->
5252
<referencedClass name="Doctrine\DBAL\Driver\ServerInfoAwareConnection"/>
5353
<referencedClass name="Doctrine\DBAL\VersionAwarePlatformDriver"/>
54+
<referencedClass name="Doctrine\DBAL\FetchMode"/>
5455
<!--
5556
TODO: remove in 4.0.0
5657
-->
@@ -604,6 +605,7 @@
604605
<InvalidArgument>
605606
<errorLevel type="suppress">
606607
<!-- We're testing with invalid input here. -->
608+
<file name="tests/Functional/LegacyAPITest.php"/>
607609
<file name="tests/Platforms/AbstractPlatformTestCase.php"/>
608610
</errorLevel>
609611
</InvalidArgument>
@@ -748,6 +750,7 @@
748750

749751
<!-- We're checking for invalid input. -->
750752
<directory name="src/Driver/PgSQL"/>
753+
<file name="src/Result.php"/>
751754
</errorLevel>
752755
</RedundantConditionGivenDocblockType>
753756
<RedundantPropertyInitializationCheck>

src/Connection.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1953,20 +1953,34 @@ public function executeUpdate(string $sql, array $params = [], array $types = []
19531953
/**
19541954
* BC layer for a wide-spread use-case of old DBAL APIs
19551955
*
1956-
* @deprecated This API is deprecated and will be removed after 2022
1956+
* @deprecated Use {@see executeQuery()} instead
19571957
*/
19581958
public function query(string $sql): Result
19591959
{
1960+
Deprecation::trigger(
1961+
'doctrine/dbal',
1962+
'https://github.com/doctrine/dbal/pull/4163',
1963+
'%s is deprecated, please use executeQuery() instead.',
1964+
__METHOD__,
1965+
);
1966+
19601967
return $this->executeQuery($sql);
19611968
}
19621969

19631970
/**
19641971
* BC layer for a wide-spread use-case of old DBAL APIs
19651972
*
1966-
* @deprecated This API is deprecated and will be removed after 2022
1973+
* @deprecated please use {@see executeStatement()} instead
19671974
*/
19681975
public function exec(string $sql): int
19691976
{
1977+
Deprecation::trigger(
1978+
'doctrine/dbal',
1979+
'https://github.com/doctrine/dbal/pull/4163',
1980+
'%s is deprecated, please use executeStatement() instead.',
1981+
__METHOD__,
1982+
);
1983+
19701984
return $this->executeStatement($sql);
19711985
}
19721986
}

src/FetchMode.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
/**
66
* Legacy Class that keeps BC for using the legacy APIs fetch()/fetchAll().
7+
*
8+
* @deprecated Use the dedicated fetch*() methods for the desired fetch mode instead.
79
*/
810
class FetchMode
911
{

src/Result.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Doctrine\DBAL\Driver\Exception as DriverException;
88
use Doctrine\DBAL\Driver\Result as DriverResult;
99
use Doctrine\DBAL\Exception\NoKeyValue;
10+
use Doctrine\Deprecations\Deprecation;
1011
use LogicException;
1112
use Traversable;
1213

@@ -261,14 +262,23 @@ private function ensureHasKeyValue(): void
261262
/**
262263
* BC layer for a wide-spread use-case of old DBAL APIs
263264
*
264-
* @deprecated This API is deprecated and will be removed after 2022
265+
* @deprecated Use {@see fetchNumeric()}, {@see fetchAssociative()} or {@see fetchOne()} instead.
266+
*
267+
* @psalm-param FetchMode::* $mode
265268
*
266269
* @return mixed
267270
*
268271
* @throws Exception
269272
*/
270273
public function fetch(int $mode = FetchMode::ASSOCIATIVE)
271274
{
275+
Deprecation::trigger(
276+
'doctrine/dbal',
277+
'https://github.com/doctrine/dbal/pull/4007',
278+
'%s is deprecated, please use fetchNumeric(), fetchAssociative() or fetchOne() instead.',
279+
__METHOD__,
280+
);
281+
272282
if (func_num_args() > 1) {
273283
throw new LogicException('Only invocations with one argument are still supported by this legacy API.');
274284
}
@@ -291,14 +301,23 @@ public function fetch(int $mode = FetchMode::ASSOCIATIVE)
291301
/**
292302
* BC layer for a wide-spread use-case of old DBAL APIs
293303
*
294-
* @deprecated This API is deprecated and will be removed after 2022
304+
* @deprecated Use {@see fetchAllNumeric()}, {@see fetchAllAssociative()} or {@see fetchFirstColumn()} instead.
305+
*
306+
* @psalm-param FetchMode::* $mode
295307
*
296308
* @return list<mixed>
297309
*
298310
* @throws Exception
299311
*/
300312
public function fetchAll(int $mode = FetchMode::ASSOCIATIVE): array
301313
{
314+
Deprecation::trigger(
315+
'doctrine/dbal',
316+
'https://github.com/doctrine/dbal/pull/4007',
317+
'%s is deprecated, please use fetchAllNumeric(), fetchAllAssociative() or fetchFirstColumn() instead.',
318+
__METHOD__,
319+
);
320+
302321
if (func_num_args() > 1) {
303322
throw new LogicException('Only invocations with one argument are still supported by this legacy API.');
304323
}

tests/Functional/LegacyAPITest.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Doctrine\DBAL\FetchMode;
66
use Doctrine\DBAL\Schema\Table;
77
use Doctrine\DBAL\Tests\FunctionalTestCase;
8+
use Doctrine\Deprecations\PHPUnit\VerifyDeprecations;
89
use LogicException;
910

1011
use function array_change_key_case;
@@ -14,6 +15,8 @@
1415

1516
class LegacyAPITest extends FunctionalTestCase
1617
{
18+
use VerifyDeprecations;
19+
1720
protected function setUp(): void
1821
{
1922
$table = new Table('legacy_table');
@@ -39,7 +42,10 @@ public function testFetchWithAssociativeMode(): void
3942
$sql = 'SELECT test_int FROM legacy_table WHERE test_int = 1';
4043

4144
$stmt = $this->connection->executeQuery($sql);
42-
$row = array_change_key_case($stmt->fetch(FetchMode::ASSOCIATIVE), CASE_LOWER);
45+
46+
$this->expectDeprecationWithIdentifier('https://github.com/doctrine/dbal/pull/4007');
47+
48+
$row = array_change_key_case($stmt->fetch(FetchMode::ASSOCIATIVE), CASE_LOWER);
4349
self::assertEquals(1, $row['test_int']);
4450
}
4551

@@ -48,7 +54,10 @@ public function testFetchWithNumericMode(): void
4854
$sql = 'SELECT test_int FROM legacy_table WHERE test_int = 1';
4955

5056
$stmt = $this->connection->executeQuery($sql);
51-
$row = $stmt->fetch(FetchMode::NUMERIC);
57+
58+
$this->expectDeprecationWithIdentifier('https://github.com/doctrine/dbal/pull/4007');
59+
60+
$row = $stmt->fetch(FetchMode::NUMERIC);
5261
self::assertEquals(1, $row[0]);
5362
}
5463

@@ -57,7 +66,10 @@ public function testFetchWithColumnMode(): void
5766
$sql = 'SELECT test_int FROM legacy_table WHERE test_int = 1';
5867

5968
$stmt = $this->connection->executeQuery($sql);
60-
$row = $stmt->fetch(FetchMode::COLUMN);
69+
70+
$this->expectDeprecationWithIdentifier('https://github.com/doctrine/dbal/pull/4007');
71+
72+
$row = $stmt->fetch(FetchMode::COLUMN);
6173
self::assertEquals(1, $row);
6274
}
6375

@@ -89,6 +101,8 @@ public function testFetchAllWithAssociativeModes(): void
89101

90102
$stmt = $this->connection->executeQuery($sql);
91103

104+
$this->expectDeprecationWithIdentifier('https://github.com/doctrine/dbal/pull/4007');
105+
92106
$rows = $stmt->fetchAll(FetchMode::ASSOCIATIVE);
93107
$rows = array_map(static function ($row) {
94108
return array_change_key_case($row, CASE_LOWER);
@@ -102,6 +116,9 @@ public function testFetchAllWithNumericModes(): void
102116
$sql = 'SELECT test_int FROM legacy_table WHERE test_int = 1';
103117

104118
$stmt = $this->connection->executeQuery($sql);
119+
120+
$this->expectDeprecationWithIdentifier('https://github.com/doctrine/dbal/pull/4007');
121+
105122
$rows = $stmt->fetchAll(FetchMode::NUMERIC);
106123
self::assertEquals([[0 => 1]], $rows);
107124
}
@@ -111,6 +128,9 @@ public function testFetchAllWithColumnMode(): void
111128
$sql = 'SELECT test_int FROM legacy_table WHERE test_int = 1';
112129

113130
$stmt = $this->connection->executeQuery($sql);
131+
132+
$this->expectDeprecationWithIdentifier('https://github.com/doctrine/dbal/pull/4007');
133+
114134
$rows = $stmt->fetchAll(FetchMode::COLUMN);
115135
self::assertEquals([1], $rows);
116136
}
@@ -148,12 +168,17 @@ public function testExecuteUpdate(): void
148168
$sql = 'SELECT test_string FROM legacy_table';
149169

150170
$stmt = $this->connection->executeQuery($sql);
171+
172+
$this->expectDeprecationWithIdentifier('https://github.com/doctrine/dbal/pull/4007');
173+
151174
$rows = $stmt->fetchAll(FetchMode::COLUMN);
152175
self::assertEquals(['foo', 'bar'], $rows);
153176
}
154177

155178
public function testQuery(): void
156179
{
180+
$this->expectDeprecationWithIdentifier('https://github.com/doctrine/dbal/pull/4163');
181+
157182
$stmt = $this->connection->query('SELECT test_string FROM legacy_table WHERE test_int = 1');
158183

159184
$this->assertEquals('foo', $stmt->fetchOne());
@@ -166,6 +191,8 @@ public function testExec(): void
166191
'test_string' => 'bar',
167192
]);
168193

194+
$this->expectDeprecationWithIdentifier('https://github.com/doctrine/dbal/pull/4163');
195+
169196
$count = $this->connection->exec('DELETE FROM legacy_table WHERE test_int > 1');
170197

171198
$this->assertEquals(1, $count);

0 commit comments

Comments
 (0)