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 Nov 2, 2019
1 parent d65f130 commit 8adf7f9
Show file tree
Hide file tree
Showing 22 changed files with 81 additions and 91 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 @@ -202,7 +202,7 @@ public function fetchColumn($columnIndex = 0)
*
* @return int The number of rows.
*/
public function rowCount()
public function rowCount() : int
{
assert($this->statement instanceof Statement);

Expand Down
42 changes: 14 additions & 28 deletions lib/Doctrine/DBAL/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -846,18 +846,16 @@ public function fetchAll($sql, array $params = [], $types = [])
/**
* Prepares an SQL statement.
*
* @param string $statement The SQL statement to prepare.
*
* @return DriverStatement The prepared statement.
* @param string $sql The SQL statement to prepare.
*
* @throws DBALException
*/
public function prepare($statement)
public function prepare(string $sql) : DriverStatement
{
try {
$stmt = new Statement($statement, $this);
$stmt = new Statement($sql, $this);
} catch (Throwable $ex) {
throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $statement);
throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $sql);
}

$stmt->setFetchMode($this->defaultFetchMode);
Expand All @@ -880,7 +878,7 @@ public function prepare($statement)
*
* @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 @@ -924,11 +922,9 @@ public function executeQuery($query, array $params = [], $types = [], ?QueryCach
* @param int[]|string[] $types The types the previous parameters are in.
* @param QueryCacheProfile $qcp The query cache profile.
*
* @return ResultStatement
*
* @throws CacheException
*/
public function executeCacheQuery($query, $params, $types, QueryCacheProfile $qcp)
public function executeCacheQuery($query, $params, $types, QueryCacheProfile $qcp) : ResultStatement
{
$resultCache = $qcp->getResultCacheDriver() ?? $this->_config->getResultCacheImpl();

Expand Down Expand Up @@ -991,7 +987,7 @@ public function project($query, array $params, Closure $function)
/**
* {@inheritDoc}
*/
public function query(string $sql)
public function query(string $sql) : ResultStatement
{
$connection = $this->getWrappedConnection();

Expand Down Expand Up @@ -1021,11 +1017,9 @@ public function query(string $sql)
* @param mixed[] $params The query parameters.
* @param int[]|string[] $types The parameter types.
*
* @return int The number of affected rows.
*
* @throws DBALException
*/
public function executeUpdate($query, array $params = [], array $types = [])
public function executeUpdate(string $query, array $params = [], array $types = []) : int
{
$connection = $this->getWrappedConnection();

Expand Down Expand Up @@ -1058,15 +1052,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 DBALException
* {@inheritDoc}
*/
public function exec($statement)
public function exec(string $statement) : int
{
$connection = $this->getWrappedConnection();

Expand Down Expand Up @@ -1472,13 +1460,11 @@ public function convertToPHPValue($value, $type)
* @internal Duck-typing used on the $stmt parameter to support driver statements as well as
* raw PDOStatement instances.
*
* @param \Doctrine\DBAL\Driver\Statement $stmt The statement to bind the values to.
* @param mixed[] $params The map/list of named/positional parameters.
* @param int[]|string[] $types The parameter types (PDO binding types or DBAL mapping types).
*
* @return void
* @param DriverStatement $stmt The statement to bind the values to.
* @param mixed[] $params The map/list of named/positional parameters.
* @param int[]|string[] $types The parameter types (PDO binding types or DBAL mapping 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
12 changes: 7 additions & 5 deletions lib/Doctrine/DBAL/Connections/MasterSlaveConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\Connection as DriverConnection;
use Doctrine\DBAL\Driver\ResultStatement;
use Doctrine\DBAL\Driver\Statement;
use Doctrine\DBAL\Event\ConnectionEventArgs;
use Doctrine\DBAL\Events;
use InvalidArgumentException;
Expand Down Expand Up @@ -218,7 +220,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 @@ -301,7 +303,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 @@ -341,7 +343,7 @@ public function rollbackSavepoint($savepoint)
/**
* {@inheritDoc}
*/
public function query(string $sql)
public function query(string $sql) : ResultStatement
{
$this->connect('master');
assert($this->_conn instanceof DriverConnection);
Expand All @@ -361,10 +363,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 @@ -15,21 +15,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 @@ -44,11 +38,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
8 changes: 5 additions & 3 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 Down Expand Up @@ -75,7 +77,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 +90,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 +115,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
2 changes: 1 addition & 1 deletion lib/Doctrine/DBAL/Driver/IBMDB2/DB2Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ public function fetchColumn($columnIndex = 0)
/**
* {@inheritdoc}
*/
public function rowCount()
public function rowCount() : int
{
return @db2_num_rows($this->stmt) ? : 0;
}
Expand Down
10 changes: 6 additions & 4 deletions lib/Doctrine/DBAL/Driver/Mysqli/MysqliConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

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 mysqli;
use const MYSQLI_INIT_COMMAND;
Expand Down Expand Up @@ -125,15 +127,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 @@ -152,7 +154,7 @@ public function quote($input, $type = ParameterType::STRING)
/**
* {@inheritdoc}
*/
public function exec($statement)
public function exec(string $statement) : int
{
if ($this->conn->query($statement) === false) {
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 @@ -415,7 +415,7 @@ public function closeCursor()
/**
* {@inheritdoc}
*/
public function rowCount()
public function rowCount() : int
{
if ($this->_columnNames === false) {
return $this->_stmt->affected_rows;
Expand Down
10 changes: 6 additions & 4 deletions lib/Doctrine/DBAL/Driver/OCI8/OCI8Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Doctrine\DBAL\Driver\OCI8;

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 UnexpectedValueException;
use const OCI_COMMIT_ON_SUCCESS;
Expand Down Expand Up @@ -102,15 +104,15 @@ public function requiresQueryForServerVersion()
/**
* {@inheritdoc}
*/
public function prepare($prepareString)
public function prepare(string $sql) : DriverStatement
{
return new OCI8Statement($this->dbh, $prepareString, $this);
return new OCI8Statement($this->dbh, $sql, $this);
}

/**
* {@inheritdoc}
*/
public function query(string $sql)
public function query(string $sql) : ResultStatement
{
$stmt = $this->prepare($sql);
$stmt->execute();
Expand All @@ -134,7 +136,7 @@ public function quote($value, $type = ParameterType::STRING)
/**
* {@inheritdoc}
*/
public function exec($statement)
public function exec(string $statement) : int
{
$stmt = $this->prepare($statement);
$stmt->execute();
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/DBAL/Driver/OCI8/OCI8Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ public function fetchColumn($columnIndex = 0)
/**
* {@inheritdoc}
*/
public function rowCount()
public function rowCount() : int
{
return oci_num_rows($this->_sth) ?: 0;
}
Expand Down
8 changes: 4 additions & 4 deletions lib/Doctrine/DBAL/Driver/PDOConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function __construct($dsn, $user = null, $password = null, ?array $option
/**
* {@inheritdoc}
*/
public function exec($statement)
public function exec(string $statement) : int
{
try {
return $this->connection->exec($statement);
Expand All @@ -57,11 +57,11 @@ public function getServerVersion()
/**
* {@inheritdoc}
*/
public function prepare($prepareString)
public function prepare(string $sql) : Statement
{
try {
return $this->createStatement(
$this->connection->prepare($prepareString)
$this->connection->prepare($sql)
);
} catch (\PDOException $exception) {
throw new PDOException($exception);
Expand All @@ -71,7 +71,7 @@ public function prepare($prepareString)
/**
* {@inheritdoc}
*/
public function query(string $sql)
public function query(string $sql) : ResultStatement
{
try {
$stmt = $this->connection->query($sql);
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/DBAL/Driver/PDOStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public function execute($params = null)
/**
* {@inheritdoc}
*/
public function rowCount()
public function rowCount() : int
{
return $this->stmt->rowCount();
}
Expand Down
Loading

0 comments on commit 8adf7f9

Please sign in to comment.