Skip to content

Commit 5d7df54

Browse files
morozovMajkl578
authored andcommitted
[DBAL-3079] Added type hints to query-related method parameters and return values
1 parent 035cd92 commit 5d7df54

24 files changed

+119
-171
lines changed

lib/Doctrine/DBAL/Cache/ResultCacheStatement.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public function fetchColumn($columnIndex = 0)
205205
*
206206
* @return int The number of rows.
207207
*/
208-
public function rowCount()
208+
public function rowCount() : int
209209
{
210210
return $this->statement->rowCount();
211211
}

lib/Doctrine/DBAL/Connection.php

Lines changed: 31 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace Doctrine\DBAL;
44

5+
use Doctrine\DBAL\Driver\ResultStatement;
56
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
7+
use Doctrine\DBAL\Driver\Statement as DriverStatement;
68
use Doctrine\DBAL\Exception\InvalidArgumentException;
79
use Closure;
810
use Exception;
@@ -17,7 +19,6 @@
1719
use Throwable;
1820
use function array_key_exists;
1921
use function array_merge;
20-
use function func_get_args;
2122
use function implode;
2223
use function is_int;
2324
use function is_string;
@@ -856,18 +857,16 @@ public function fetchAll($sql, array $params = [], $types = [])
856857
/**
857858
* Prepares an SQL statement.
858859
*
859-
* @param string $statement The SQL statement to prepare.
860+
* @param string $sql The SQL statement to prepare.
860861
*
861-
* @return \Doctrine\DBAL\Statement The prepared statement.
862-
*
863-
* @throws \Doctrine\DBAL\DBALException
862+
* @throws DBALException
864863
*/
865-
public function prepare($statement)
864+
public function prepare(string $sql) : DriverStatement
866865
{
867866
try {
868-
$stmt = new Statement($statement, $this);
867+
$stmt = new Statement($sql, $this);
869868
} catch (Exception $ex) {
870-
throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $statement);
869+
throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $sql);
871870
}
872871

873872
$stmt->setFetchMode($this->defaultFetchMode);
@@ -881,16 +880,14 @@ public function prepare($statement)
881880
* If the query is parametrized, a prepared statement is used.
882881
* If an SQLLogger is configured, the execution is logged.
883882
*
884-
* @param string $query The SQL query to execute.
885-
* @param array $params The parameters to bind to the query, if any.
886-
* @param array $types The types the previous parameters are in.
887-
* @param \Doctrine\DBAL\Cache\QueryCacheProfile|null $qcp The query cache profile, optional.
888-
*
889-
* @return \Doctrine\DBAL\Driver\Statement The executed statement.
883+
* @param string $query The SQL query to execute.
884+
* @param mixed[] $params The parameters to bind to the query, if any.
885+
* @param (int|string|Type)[] $types The types the previous parameters are in.
886+
* @param QueryCacheProfile|null $qcp The query cache profile, optional.
890887
*
891-
* @throws \Doctrine\DBAL\DBALException
888+
* @throws DBALException
892889
*/
893-
public function executeQuery($query, array $params = [], $types = [], QueryCacheProfile $qcp = null)
890+
public function executeQuery(string $query, array $params = [], $types = [], ?QueryCacheProfile $qcp = null) : ResultStatement
894891
{
895892
if ($qcp !== null) {
896893
return $this->executeCacheQuery($query, $params, $types, $qcp);
@@ -929,16 +926,14 @@ public function executeQuery($query, array $params = [], $types = [], QueryCache
929926
/**
930927
* Executes a caching query.
931928
*
932-
* @param string $query The SQL query to execute.
933-
* @param array $params The parameters to bind to the query, if any.
934-
* @param array $types The types the previous parameters are in.
935-
* @param \Doctrine\DBAL\Cache\QueryCacheProfile $qcp The query cache profile.
936-
*
937-
* @return \Doctrine\DBAL\Driver\ResultStatement
929+
* @param string $query The SQL query to execute.
930+
* @param mixed[] $params The parameters to bind to the query, if any.
931+
* @param (int|string)|Type[] $types The types the previous parameters are in.
932+
* @param QueryCacheProfile $qcp The query cache profile.
938933
*
939-
* @throws \Doctrine\DBAL\Cache\CacheException
934+
* @throws DBALException
940935
*/
941-
public function executeCacheQuery($query, $params, $types, QueryCacheProfile $qcp)
936+
public function executeCacheQuery($query, $params, $types, QueryCacheProfile $qcp) : ResultStatement
942937
{
943938
$resultCache = $qcp->getResultCacheDriver() ?: $this->_config->getResultCacheImpl();
944939
if ( ! $resultCache) {
@@ -995,7 +990,7 @@ public function project($query, array $params, Closure $function)
995990
/**
996991
* {@inheritDoc}
997992
*/
998-
public function query(string $sql)
993+
public function query(string $sql) : ResultStatement
999994
{
1000995
$this->connect();
1001996

@@ -1021,15 +1016,13 @@ public function query(string $sql)
10211016
*
10221017
* This method supports PDO binding types as well as DBAL mapping types.
10231018
*
1024-
* @param string $query The SQL query.
1025-
* @param array $params The query parameters.
1026-
* @param array $types The parameter types.
1027-
*
1028-
* @return int The number of affected rows.
1019+
* @param string $query The SQL query.
1020+
* @param mixed[] $params The query parameters.
1021+
* @param (int|string|Type)[] $types The parameter types.
10291022
*
1030-
* @throws \Doctrine\DBAL\DBALException
1023+
* @throws DBALException
10311024
*/
1032-
public function executeUpdate($query, array $params = [], array $types = [])
1025+
public function executeUpdate(string $query, array $params = [], array $types = []) : int
10331026
{
10341027
$this->connect();
10351028

@@ -1061,15 +1054,9 @@ public function executeUpdate($query, array $params = [], array $types = [])
10611054
}
10621055

10631056
/**
1064-
* Executes an SQL statement and return the number of affected rows.
1065-
*
1066-
* @param string $statement
1067-
*
1068-
* @return int The number of affected rows.
1069-
*
1070-
* @throws \Doctrine\DBAL\DBALException
1057+
* {@inheritDoc}
10711058
*/
1072-
public function exec($statement)
1059+
public function exec(string $statement) : int
10731060
{
10741061
$this->connect();
10751062

@@ -1473,16 +1460,11 @@ public function convertToPHPValue($value, $type)
14731460
* Binds a set of parameters, some or all of which are typed with a PDO binding type
14741461
* or DBAL mapping type, to a given statement.
14751462
*
1476-
* @param \Doctrine\DBAL\Driver\Statement $stmt The statement to bind the values to.
1477-
* @param array $params The map/list of named/positional parameters.
1478-
* @param array $types The parameter types (PDO binding types or DBAL mapping types).
1479-
*
1480-
* @return void
1481-
*
1482-
* @internal Duck-typing used on the $stmt parameter to support driver statements as well as
1483-
* raw PDOStatement instances.
1463+
* @param DriverStatement $stmt The statement to bind the values to.
1464+
* @param mixed[] $params The map/list of named/positional parameters.
1465+
* @param (int|string|Type)[] $types The parameter types.
14841466
*/
1485-
private function _bindTypedValues($stmt, array $params, array $types)
1467+
private function _bindTypedValues(DriverStatement $stmt, array $params, array $types) : void
14861468
{
14871469
// Check whether parameters are positional or named. Mixing is not allowed, just like in PDO.
14881470
if (is_int(key($params))) {

lib/Doctrine/DBAL/Connections/MasterSlaveConnection.php

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22

33
namespace Doctrine\DBAL\Connections;
44

5+
use Doctrine\Common\EventManager;
6+
use Doctrine\DBAL\Configuration;
57
use Doctrine\DBAL\Connection;
68
use Doctrine\DBAL\Driver;
7-
use Doctrine\DBAL\Configuration;
8-
use Doctrine\Common\EventManager;
9+
use Doctrine\DBAL\Driver\ResultStatement;
10+
use Doctrine\DBAL\Driver\Statement;
911
use Doctrine\DBAL\Event\ConnectionEventArgs;
1012
use Doctrine\DBAL\Events;
1113
use function array_rand;
1214
use function count;
13-
use function func_get_args;
1415

1516
/**
1617
* Master-Slave Connection
@@ -86,10 +87,7 @@ class MasterSlaveConnection extends Connection
8687
/**
8788
* Creates Master Slave Connection.
8889
*
89-
* @param array $params
90-
* @param \Doctrine\DBAL\Driver $driver
91-
* @param \Doctrine\DBAL\Configuration|null $config
92-
* @param \Doctrine\Common\EventManager|null $eventManager
90+
* @param mixed[] $params
9391
*
9492
* @throws \InvalidArgumentException
9593
*/
@@ -222,7 +220,7 @@ protected function chooseConnectionConfiguration($connectionName, $params)
222220
/**
223221
* {@inheritDoc}
224222
*/
225-
public function executeUpdate($query, array $params = [], array $types = [])
223+
public function executeUpdate(string $query, array $params = [], array $types = []) : int
226224
{
227225
$this->connect('master');
228226

@@ -305,7 +303,7 @@ public function insert($tableName, array $data, array $types = [])
305303
/**
306304
* {@inheritDoc}
307305
*/
308-
public function exec($statement)
306+
public function exec(string $statement) : int
309307
{
310308
$this->connect('master');
311309

@@ -345,7 +343,7 @@ public function rollbackSavepoint($savepoint)
345343
/**
346344
* {@inheritDoc}
347345
*/
348-
public function query(string $sql)
346+
public function query(string $sql) : ResultStatement
349347
{
350348
$this->connect('master');
351349

@@ -362,10 +360,10 @@ public function query(string $sql)
362360
/**
363361
* {@inheritDoc}
364362
*/
365-
public function prepare($statement)
363+
public function prepare(string $sql) : Statement
366364
{
367365
$this->connect('master');
368366

369-
return parent::prepare($statement);
367+
return parent::prepare($sql);
370368
}
371369
}

lib/Doctrine/DBAL/Driver/Connection.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,15 @@ interface Connection
1717
{
1818
/**
1919
* Prepares a statement for execution and returns a Statement object.
20-
*
21-
* @param string $prepareString
22-
*
23-
* @return Statement
2420
*/
25-
public function prepare($prepareString);
21+
public function prepare(string $sql) : Statement;
2622

2723
/**
2824
* Executes an SQL statement, returning a result set as a Statement object.
2925
*
30-
* @return Statement
31-
*
3226
* @throws DBALException
3327
*/
34-
public function query(string $sql);
28+
public function query(string $sql) : ResultStatement;
3529

3630
/**
3731
* Quotes a string for use in a query.
@@ -46,11 +40,9 @@ public function quote($input, $type = ParameterType::STRING);
4640
/**
4741
* Executes an SQL statement and return the number of affected rows.
4842
*
49-
* @param string $statement
50-
*
51-
* @return int
43+
* @throws DBALException
5244
*/
53-
public function exec($statement);
45+
public function exec(string $statement) : int;
5446

5547
/**
5648
* Returns the ID of the last inserted row or sequence value.

lib/Doctrine/DBAL/Driver/IBMDB2/DB2Connection.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
namespace Doctrine\DBAL\Driver\IBMDB2;
44

55
use Doctrine\DBAL\Driver\Connection;
6+
use Doctrine\DBAL\Driver\ResultStatement;
67
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
8+
use Doctrine\DBAL\Driver\Statement as DriverStatement;
79
use Doctrine\DBAL\ParameterType;
810
use const DB2_AUTOCOMMIT_OFF;
911
use const DB2_AUTOCOMMIT_ON;
@@ -21,7 +23,6 @@
2123
use function db2_rollback;
2224
use function db2_server_info;
2325
use function db2_stmt_errormsg;
24-
use function func_get_args;
2526

2627
class DB2Connection implements Connection, ServerInfoAwareConnection
2728
{
@@ -73,7 +74,7 @@ public function requiresQueryForServerVersion()
7374
/**
7475
* {@inheritdoc}
7576
*/
76-
public function prepare($sql)
77+
public function prepare(string $sql) : DriverStatement
7778
{
7879
$stmt = @db2_prepare($this->_conn, $sql);
7980
if ( ! $stmt) {
@@ -86,7 +87,7 @@ public function prepare($sql)
8687
/**
8788
* {@inheritdoc}
8889
*/
89-
public function query(string $sql)
90+
public function query(string $sql) : ResultStatement
9091
{
9192
$stmt = $this->prepare($sql);
9293
$stmt->execute();
@@ -111,7 +112,7 @@ public function quote($input, $type = ParameterType::STRING)
111112
/**
112113
* {@inheritdoc}
113114
*/
114-
public function exec($statement)
115+
public function exec(string $statement) : int
115116
{
116117
$stmt = @db2_exec($this->_conn, $statement);
117118

lib/Doctrine/DBAL/Driver/IBMDB2/DB2Statement.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
use function db2_num_rows;
2323
use function db2_stmt_error;
2424
use function db2_stmt_errormsg;
25-
use function func_get_args;
26-
use function func_num_args;
2725
use function gettype;
2826
use function is_object;
2927
use function is_string;
@@ -312,7 +310,7 @@ public function fetchColumn($columnIndex = 0)
312310
/**
313311
* {@inheritdoc}
314312
*/
315-
public function rowCount()
313+
public function rowCount() : int
316314
{
317315
return (@db2_num_rows($this->_stmt)) ? : 0;
318316
}

lib/Doctrine/DBAL/Driver/Mysqli/MysqliConnection.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
namespace Doctrine\DBAL\Driver\Mysqli;
44

5-
use Doctrine\DBAL\Driver\Connection as Connection;
5+
use Doctrine\DBAL\Driver\Connection;
66
use Doctrine\DBAL\Driver\PingableConnection;
7+
use Doctrine\DBAL\Driver\ResultStatement;
78
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
9+
use Doctrine\DBAL\Driver\Statement as DriverStatement;
810
use Doctrine\DBAL\ParameterType;
911
use function defined;
1012
use function floor;
11-
use function func_get_args;
1213
use function in_array;
1314
use function ini_get;
1415
use function mysqli_errno;
@@ -121,15 +122,15 @@ public function requiresQueryForServerVersion()
121122
/**
122123
* {@inheritdoc}
123124
*/
124-
public function prepare($prepareString)
125+
public function prepare(string $sql) : DriverStatement
125126
{
126-
return new MysqliStatement($this->_conn, $prepareString);
127+
return new MysqliStatement($this->_conn, $sql);
127128
}
128129

129130
/**
130131
* {@inheritdoc}
131132
*/
132-
public function query(string $sql)
133+
public function query(string $sql) : ResultStatement
133134
{
134135
$stmt = $this->prepare($sql);
135136
$stmt->execute();
@@ -148,7 +149,7 @@ public function quote($input, $type = ParameterType::STRING)
148149
/**
149150
* {@inheritdoc}
150151
*/
151-
public function exec($statement)
152+
public function exec(string $statement) : int
152153
{
153154
if (false === $this->_conn->query($statement)) {
154155
throw new MysqliException($this->_conn->error, $this->_conn->sqlstate, $this->_conn->errno);

lib/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ public function closeCursor()
372372
/**
373373
* {@inheritdoc}
374374
*/
375-
public function rowCount()
375+
public function rowCount() : int
376376
{
377377
if (false === $this->_columnNames) {
378378
return $this->_stmt->affected_rows;

0 commit comments

Comments
 (0)