Skip to content

Commit

Permalink
[DBAL-3079] Added type hints to query-related method parameters and r…
Browse files Browse the repository at this point in the history
…eturn values
  • Loading branch information
morozov committed Jul 2, 2018
1 parent b94e887 commit f8079e5
Show file tree
Hide file tree
Showing 24 changed files with 116 additions and 166 deletions.
2 changes: 1 addition & 1 deletion lib/Doctrine/DBAL/Cache/ResultCacheStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public function fetchColumn($columnIndex = 0)
*
* @return int The number of rows.
*/
public function rowCount()
public function rowCount() : int
{
return $this->statement->rowCount();
}
Expand Down
74 changes: 29 additions & 45 deletions lib/Doctrine/DBAL/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use function assert;
use function array_key_exists;
use function array_merge;
use function func_get_args;
use function implode;
use function is_int;
use function is_string;
Expand Down Expand Up @@ -861,18 +860,16 @@ public function fetchAll($sql, array $params = [], $types = [])
/**
* Prepares an SQL statement.
*
* @param string $statement The SQL statement to prepare.
* @param string $sql The SQL statement to prepare.
*
* @return DriverStatement The prepared statement.
*
* @throws \Doctrine\DBAL\DBALException
* @throws DBALException
*/
public function prepare($statement)
public function prepare(string $sql) : DriverStatement
{
try {
$stmt = new Statement($statement, $this);
$stmt = new Statement($sql, $this);
} catch (Exception $ex) {
throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $statement);
throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $sql);
}

$stmt->setFetchMode($this->defaultFetchMode);
Expand All @@ -886,16 +883,16 @@ public function prepare($statement)
* If the query is parametrized, a prepared statement is used.
* If an SQLLogger is configured, the execution is logged.
*
* @param string $query The SQL query to execute.
* @param array $params The parameters to bind to the query, if any.
* @param array $types The types the previous parameters are in.
* @param \Doctrine\DBAL\Cache\QueryCacheProfile|null $qcp The query cache profile, optional.
* @param string $query The SQL query to execute.
* @param mixed[] $params The parameters to bind to the query, if any.
* @param (int|string|Type)[] $types The types the previous parameters are in.
* @param QueryCacheProfile|null $qcp The query cache profile, optional.
*
* @return ResultStatement The executed statement.
*
* @throws \Doctrine\DBAL\DBALException
* @throws DBALException
*/
public function executeQuery($query, array $params = [], $types = [], QueryCacheProfile $qcp = null)
public function executeQuery(string $query, array $params = [], $types = [], ?QueryCacheProfile $qcp = null) : ResultStatement
{
if ($qcp !== null) {
return $this->executeCacheQuery($query, $params, $types, $qcp);
Expand Down Expand Up @@ -934,16 +931,16 @@ public function executeQuery($query, array $params = [], $types = [], QueryCache
/**
* Executes a caching query.
*
* @param string $query The SQL query to execute.
* @param array $params The parameters to bind to the query, if any.
* @param array $types The types the previous parameters are in.
* @param \Doctrine\DBAL\Cache\QueryCacheProfile $qcp The query cache profile.
* @param string $query The SQL query to execute.
* @param mixed[] $params The parameters to bind to the query, if any.
* @param (int|string)|Type[] $types The types the previous parameters are in.
* @param QueryCacheProfile $qcp The query cache profile.
*
* @return ResultStatement
*
* @throws \Doctrine\DBAL\Cache\CacheException
* @throws DBALException
*/
public function executeCacheQuery($query, $params, $types, QueryCacheProfile $qcp)
public function executeCacheQuery($query, $params, $types, QueryCacheProfile $qcp) : ResultStatement
{
$resultCache = $qcp->getResultCacheDriver() ?: $this->_config->getResultCacheImpl();
if ( ! $resultCache) {
Expand Down Expand Up @@ -1000,7 +997,7 @@ public function project($query, array $params, Closure $function)
/**
* {@inheritDoc}
*/
public function query(string $sql)
public function query(string $sql) : ResultStatement
{
$this->connect();

Expand All @@ -1026,15 +1023,13 @@ public function query(string $sql)
*
* This method supports PDO binding types as well as DBAL mapping types.
*
* @param string $query The SQL query.
* @param array $params The query parameters.
* @param array $types The parameter types.
*
* @return int The number of affected rows.
* @param string $query The SQL query.
* @param mixed[] $params The query parameters.
* @param (int|string|Type)[] $types The parameter types.
*
* @throws \Doctrine\DBAL\DBALException
* @throws DBALException
*/
public function executeUpdate($query, array $params = [], array $types = [])
public function executeUpdate(string $query, array $params = [], array $types = []) : int
{
$this->connect();

Expand Down Expand Up @@ -1066,15 +1061,9 @@ public function executeUpdate($query, array $params = [], array $types = [])
}

/**
* Executes an SQL statement and return the number of affected rows.
*
* @param string $statement
*
* @return int The number of affected rows.
*
* @throws \Doctrine\DBAL\DBALException
* {@inheritDoc}
*/
public function exec($statement)
public function exec(string $statement) : int
{
$this->connect();

Expand Down Expand Up @@ -1478,16 +1467,11 @@ public function convertToPHPValue($value, $type)
* Binds a set of parameters, some or all of which are typed with a PDO binding type
* or DBAL mapping type, to a given statement.
*
* @param \Doctrine\DBAL\Driver\Statement $stmt The statement to bind the values to.
* @param array $params The map/list of named/positional parameters.
* @param array $types The parameter types (PDO binding types or DBAL mapping types).
*
* @return void
*
* @internal Duck-typing used on the $stmt parameter to support driver statements as well as
* raw PDOStatement instances.
* @param DriverStatement $stmt The statement to bind the values to.
* @param mixed[] $params The map/list of named/positional parameters.
* @param (int|string|Type)[] $types The parameter types.
*/
private function _bindTypedValues($stmt, array $params, array $types)
private function _bindTypedValues(DriverStatement $stmt, array $params, array $types) : void
{
// Check whether parameters are positional or named. Mixing is not allowed, just like in PDO.
if (is_int(key($params))) {
Expand Down
22 changes: 10 additions & 12 deletions lib/Doctrine/DBAL/Connections/MasterSlaveConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

namespace Doctrine\DBAL\Connections;

use Doctrine\Common\EventManager;
use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\Connection as DriverConnection;
use Doctrine\DBAL\Configuration;
use Doctrine\Common\EventManager;
use Doctrine\DBAL\Driver\ResultStatement;
use Doctrine\DBAL\Driver\Statement;
use Doctrine\DBAL\Event\ConnectionEventArgs;
use Doctrine\DBAL\Events;
use function array_rand;
use function count;
use function func_get_args;

/**
* Master-Slave Connection
Expand Down Expand Up @@ -87,10 +88,7 @@ class MasterSlaveConnection extends Connection
/**
* Creates Master Slave Connection.
*
* @param array $params
* @param \Doctrine\DBAL\Driver $driver
* @param \Doctrine\DBAL\Configuration|null $config
* @param \Doctrine\Common\EventManager|null $eventManager
* @param mixed[] $params
*
* @throws \InvalidArgumentException
*/
Expand Down Expand Up @@ -223,7 +221,7 @@ protected function chooseConnectionConfiguration($connectionName, $params)
/**
* {@inheritDoc}
*/
public function executeUpdate($query, array $params = [], array $types = [])
public function executeUpdate(string $query, array $params = [], array $types = []) : int
{
$this->connect('master');

Expand Down Expand Up @@ -306,7 +304,7 @@ public function insert($tableName, array $data, array $types = [])
/**
* {@inheritDoc}
*/
public function exec($statement)
public function exec(string $statement) : int
{
$this->connect('master');

Expand Down Expand Up @@ -346,7 +344,7 @@ public function rollbackSavepoint($savepoint)
/**
* {@inheritDoc}
*/
public function query(string $sql)
public function query(string $sql) : ResultStatement
{
$this->connect('master');

Expand All @@ -363,10 +361,10 @@ public function query(string $sql)
/**
* {@inheritDoc}
*/
public function prepare($statement)
public function prepare(string $sql) : Statement
{
$this->connect('master');

return parent::prepare($statement);
return parent::prepare($sql);
}
}
16 changes: 4 additions & 12 deletions lib/Doctrine/DBAL/Driver/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,15 @@ interface Connection
{
/**
* Prepares a statement for execution and returns a Statement object.
*
* @param string $prepareString
*
* @return Statement
*/
public function prepare($prepareString);
public function prepare(string $sql) : Statement;

/**
* Executes an SQL statement, returning a result set as a Statement object.
*
* @return Statement
*
* @throws DBALException
*/
public function query(string $sql);
public function query(string $sql) : ResultStatement;

/**
* Quotes a string for use in a query.
Expand All @@ -46,11 +40,9 @@ public function quote($input, $type = ParameterType::STRING);
/**
* Executes an SQL statement and return the number of affected rows.
*
* @param string $statement
*
* @return int
* @throws DBALException
*/
public function exec($statement);
public function exec(string $statement) : int;

/**
* Returns the ID of the last inserted row or sequence value.
Expand Down
9 changes: 5 additions & 4 deletions lib/Doctrine/DBAL/Driver/IBMDB2/DB2Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Doctrine\DBAL\Driver\IBMDB2;

use Doctrine\DBAL\Driver\Connection;
use Doctrine\DBAL\Driver\ResultStatement;
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
use Doctrine\DBAL\Driver\Statement as DriverStatement;
use Doctrine\DBAL\ParameterType;
use stdClass;
use const DB2_AUTOCOMMIT_OFF;
Expand All @@ -22,7 +24,6 @@
use function db2_rollback;
use function db2_server_info;
use function db2_stmt_errormsg;
use function func_get_args;

class DB2Connection implements Connection, ServerInfoAwareConnection
{
Expand Down Expand Up @@ -75,7 +76,7 @@ public function requiresQueryForServerVersion()
/**
* {@inheritdoc}
*/
public function prepare($sql)
public function prepare(string $sql) : DriverStatement
{
$stmt = @db2_prepare($this->_conn, $sql);
if ( ! $stmt) {
Expand All @@ -88,7 +89,7 @@ public function prepare($sql)
/**
* {@inheritdoc}
*/
public function query(string $sql)
public function query(string $sql) : ResultStatement
{
$stmt = $this->prepare($sql);
$stmt->execute();
Expand All @@ -113,7 +114,7 @@ public function quote($input, $type = ParameterType::STRING)
/**
* {@inheritdoc}
*/
public function exec($statement)
public function exec(string $statement) : int
{
$stmt = @db2_exec($this->_conn, $statement);

Expand Down
4 changes: 1 addition & 3 deletions lib/Doctrine/DBAL/Driver/IBMDB2/DB2Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
use function db2_num_rows;
use function db2_stmt_error;
use function db2_stmt_errormsg;
use function func_get_args;
use function func_num_args;
use function gettype;
use function is_object;
use function is_string;
Expand Down Expand Up @@ -312,7 +310,7 @@ public function fetchColumn($columnIndex = 0)
/**
* {@inheritdoc}
*/
public function rowCount()
public function rowCount() : int
{
return (@db2_num_rows($this->_stmt)) ? : 0;
}
Expand Down
11 changes: 6 additions & 5 deletions lib/Doctrine/DBAL/Driver/Mysqli/MysqliConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

use Doctrine\DBAL\Driver\Connection;
use Doctrine\DBAL\Driver\PingableConnection;
use Doctrine\DBAL\Driver\ResultStatement;
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
use Doctrine\DBAL\Driver\Statement as DriverStatement;
use Doctrine\DBAL\ParameterType;
use function defined;
use function floor;
use function func_get_args;
use function in_array;
use function ini_get;
use function mysqli_errno;
Expand Down Expand Up @@ -121,15 +122,15 @@ public function requiresQueryForServerVersion()
/**
* {@inheritdoc}
*/
public function prepare($prepareString)
public function prepare(string $sql) : DriverStatement
{
return new MysqliStatement($this->_conn, $prepareString);
return new MysqliStatement($this->_conn, $sql);
}

/**
* {@inheritdoc}
*/
public function query(string $sql)
public function query(string $sql) : ResultStatement
{
$stmt = $this->prepare($sql);
$stmt->execute();
Expand All @@ -148,7 +149,7 @@ public function quote($input, $type = ParameterType::STRING)
/**
* {@inheritdoc}
*/
public function exec($statement)
public function exec(string $statement) : int
{
if (false === $this->_conn->query($statement)) {
throw new MysqliException($this->_conn->error, $this->_conn->sqlstate, $this->_conn->errno);
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ public function closeCursor()
/**
* {@inheritdoc}
*/
public function rowCount()
public function rowCount() : int
{
if (false === $this->_columnNames) {
return $this->_stmt->affected_rows;
Expand Down
Loading

0 comments on commit f8079e5

Please sign in to comment.