Skip to content

Commit

Permalink
Merge pull request doctrine#3552 from morozov/statement-types
Browse files Browse the repository at this point in the history
Enforced parameter and return value types in Statement classes
  • Loading branch information
morozov committed Aug 26, 2019
2 parents b4ba0d6 + ef9140c commit 33ea073
Show file tree
Hide file tree
Showing 15 changed files with 206 additions and 241 deletions.
10 changes: 5 additions & 5 deletions lib/Doctrine/DBAL/Cache/ArrayStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function closeCursor() : void
/**
* {@inheritdoc}
*/
public function columnCount()
public function columnCount() : int
{
return $this->columnCount;
}
Expand All @@ -75,7 +75,7 @@ public function rowCount() : int
/**
* {@inheritdoc}
*/
public function setFetchMode($fetchMode, ...$args) : void
public function setFetchMode(int $fetchMode, ...$args) : void
{
if (count($args) > 0) {
throw new InvalidArgumentException('Caching layer does not support 2nd/3rd argument to setFetchMode().');
Expand All @@ -97,7 +97,7 @@ public function getIterator()
/**
* {@inheritdoc}
*/
public function fetch($fetchMode = null, ...$args)
public function fetch(?int $fetchMode = null, ...$args)
{
if (! isset($this->data[$this->num])) {
return false;
Expand Down Expand Up @@ -130,7 +130,7 @@ public function fetch($fetchMode = null, ...$args)
/**
* {@inheritdoc}
*/
public function fetchAll($fetchMode = null, ...$args)
public function fetchAll(?int $fetchMode = null, ...$args) : array
{
$rows = [];
while ($row = $this->fetch($fetchMode, ...$args)) {
Expand All @@ -143,7 +143,7 @@ public function fetchAll($fetchMode = null, ...$args)
/**
* {@inheritdoc}
*/
public function fetchColumn($columnIndex = 0)
public function fetchColumn(int $columnIndex = 0)
{
$row = $this->fetch(FetchMode::NUMERIC);

Expand Down
42 changes: 8 additions & 34 deletions lib/Doctrine/DBAL/Cache/QueryCacheProfile.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,39 +26,27 @@ class QueryCacheProfile
/** @var string|null */
private $cacheKey;

/**
* @param int $lifetime
* @param string|null $cacheKey
*/
public function __construct($lifetime = 0, $cacheKey = null, ?Cache $resultCache = null)
public function __construct(int $lifetime = 0, ?string $cacheKey = null, ?Cache $resultCache = null)
{
$this->lifetime = $lifetime;
$this->cacheKey = $cacheKey;
$this->resultCacheDriver = $resultCache;
}

/**
* @return Cache|null
*/
public function getResultCacheDriver()
public function getResultCacheDriver() : ?Cache
{
return $this->resultCacheDriver;
}

/**
* @return int
*/
public function getLifetime()
public function getLifetime() : int
{
return $this->lifetime;
}

/**
* @return string
*
* @throws CacheException
*/
public function getCacheKey()
public function getCacheKey() : string
{
if ($this->cacheKey === null) {
throw NoCacheKey::new();
Expand All @@ -70,14 +58,13 @@ public function getCacheKey()
/**
* Generates the real cache key from query, params, types and connection parameters.
*
* @param string $query
* @param mixed[] $params
* @param int[]|string[] $types
* @param mixed[] $connectionParams
*
* @return string[]
*/
public function generateCacheKeys($query, $params, $types, array $connectionParams = [])
public function generateCacheKeys(string $query, array $params, array $types, array $connectionParams = []) : array
{
$realCacheKey = 'query=' . $query .
'&params=' . serialize($params) .
Expand All @@ -94,30 +81,17 @@ public function generateCacheKeys($query, $params, $types, array $connectionPara
return [$cacheKey, $realCacheKey];
}

/**
* @return \Doctrine\DBAL\Cache\QueryCacheProfile
*/
public function setResultCacheDriver(Cache $cache)
public function setResultCacheDriver(Cache $cache) : self
{
return new QueryCacheProfile($this->lifetime, $this->cacheKey, $cache);
}

/**
* @param string|null $cacheKey
*
* @return \Doctrine\DBAL\Cache\QueryCacheProfile
*/
public function setCacheKey($cacheKey)
public function setCacheKey(?string $cacheKey) : self
{
return new QueryCacheProfile($this->lifetime, $cacheKey, $this->resultCacheDriver);
}

/**
* @param int $lifetime
*
* @return \Doctrine\DBAL\Cache\QueryCacheProfile
*/
public function setLifetime($lifetime)
public function setLifetime(int $lifetime) : self
{
return new QueryCacheProfile($lifetime, $this->cacheKey, $this->resultCacheDriver);
}
Expand Down
17 changes: 6 additions & 11 deletions lib/Doctrine/DBAL/Cache/ResultCacheStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,7 @@ class ResultCacheStatement implements IteratorAggregate, ResultStatement
/** @var int */
private $defaultFetchMode = FetchMode::MIXED;

/**
* @param string $cacheKey
* @param string $realKey
* @param int $lifetime
*/
public function __construct(ResultStatement $stmt, Cache $resultCache, $cacheKey, $realKey, $lifetime)
public function __construct(ResultStatement $stmt, Cache $resultCache, string $cacheKey, string $realKey, int $lifetime)
{
$this->statement = $stmt;
$this->resultCache = $resultCache;
Expand Down Expand Up @@ -98,15 +93,15 @@ public function closeCursor() : void
/**
* {@inheritdoc}
*/
public function columnCount()
public function columnCount() : int
{
return $this->statement->columnCount();
}

/**
* {@inheritdoc}
*/
public function setFetchMode($fetchMode, ...$args) : void
public function setFetchMode(int $fetchMode, ...$args) : void
{
$this->defaultFetchMode = $fetchMode;
}
Expand All @@ -124,7 +119,7 @@ public function getIterator()
/**
* {@inheritdoc}
*/
public function fetch($fetchMode = null, ...$args)
public function fetch(?int $fetchMode = null, ...$args)
{
if ($this->data === null) {
$this->data = [];
Expand Down Expand Up @@ -164,7 +159,7 @@ public function fetch($fetchMode = null, ...$args)
/**
* {@inheritdoc}
*/
public function fetchAll($fetchMode = null, ...$args)
public function fetchAll(?int $fetchMode = null, ...$args) : array
{
$this->data = $this->statement->fetchAll($fetchMode, ...$args);
$this->emptied = true;
Expand All @@ -175,7 +170,7 @@ public function fetchAll($fetchMode = null, ...$args)
/**
* {@inheritdoc}
*/
public function fetchColumn($columnIndex = 0)
public function fetchColumn(int $columnIndex = 0)
{
$row = $this->fetch(FetchMode::NUMERIC);

Expand Down
38 changes: 20 additions & 18 deletions lib/Doctrine/DBAL/Driver/IBMDB2/DB2Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use const DB2_PARAM_IN;
use function array_change_key_case;
use function array_key_exists;
use function assert;
use function count;
use function db2_bind_param;
use function db2_execute;
Expand All @@ -36,6 +37,7 @@
use function fclose;
use function fwrite;
use function gettype;
use function is_int;
use function is_object;
use function is_resource;
use function is_string;
Expand Down Expand Up @@ -89,37 +91,39 @@ public function __construct($stmt)
/**
* {@inheritdoc}
*/
public function bindValue($param, $value, $type = ParameterType::STRING) : void
public function bindValue($param, $value, int $type = ParameterType::STRING) : void
{
$this->bindParam($param, $value, $type);
}

/**
* {@inheritdoc}
*/
public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null) : void
public function bindParam($param, &$variable, int $type = ParameterType::STRING, ?int $length = null) : void
{
assert(is_int($param));

switch ($type) {
case ParameterType::INTEGER:
$this->bind($column, $variable, DB2_PARAM_IN, DB2_LONG);
$this->bind($param, $variable, DB2_PARAM_IN, DB2_LONG);
break;

case ParameterType::LARGE_OBJECT:
if (isset($this->lobs[$column])) {
[, $handle] = $this->lobs[$column];
if (isset($this->lobs[$param])) {
[, $handle] = $this->lobs[$param];
fclose($handle);
}

$handle = $this->createTemporaryFile();
$path = stream_get_meta_data($handle)['uri'];

$this->bind($column, $path, DB2_PARAM_FILE, DB2_BINARY);
$this->bind($param, $path, DB2_PARAM_FILE, DB2_BINARY);

$this->lobs[$column] = [&$variable, $handle];
$this->lobs[$param] = [&$variable, $handle];
break;

default:
$this->bind($column, $variable, DB2_PARAM_IN, DB2_CHAR);
$this->bind($param, $variable, DB2_PARAM_IN, DB2_CHAR);
break;
}
}
Expand All @@ -130,7 +134,7 @@ public function bindParam($column, &$variable, $type = ParameterType::STRING, $l
*
* @throws DB2Exception
*/
private function bind($position, &$variable, int $parameterType, int $dataType) : void
private function bind(int $position, &$variable, int $parameterType, int $dataType) : void
{
$this->bindParam[$position] =& $variable;

Expand Down Expand Up @@ -158,15 +162,15 @@ public function closeCursor() : void
/**
* {@inheritdoc}
*/
public function columnCount()
public function columnCount() : int
{
return db2_num_fields($this->stmt) ?: 0;
}

/**
* {@inheritdoc}
*/
public function execute($params = null) : void
public function execute(?array $params = null) : void
{
if ($params === null) {
ksort($this->bindParam);
Expand Down Expand Up @@ -206,7 +210,7 @@ public function execute($params = null) : void
/**
* {@inheritdoc}
*/
public function setFetchMode($fetchMode, ...$args) : void
public function setFetchMode(int $fetchMode, ...$args) : void
{
$this->defaultFetchMode = $fetchMode;

Expand All @@ -232,7 +236,7 @@ public function getIterator()
/**
* {@inheritdoc}
*/
public function fetch($fetchMode = null, ...$args)
public function fetch(?int $fetchMode = null, ...$args)
{
// do not try fetching from the statement if it's not expected to contain result
// in order to prevent exceptional situation
Expand Down Expand Up @@ -282,7 +286,7 @@ public function fetch($fetchMode = null, ...$args)
/**
* {@inheritdoc}
*/
public function fetchAll($fetchMode = null, ...$args)
public function fetchAll(?int $fetchMode = null, ...$args) : array
{
$rows = [];

Expand All @@ -309,7 +313,7 @@ public function fetchAll($fetchMode = null, ...$args)
/**
* {@inheritdoc}
*/
public function fetchColumn($columnIndex = 0)
public function fetchColumn(int $columnIndex = 0)
{
$row = $this->fetch(FetchMode::NUMERIC);

Expand Down Expand Up @@ -339,11 +343,9 @@ public function rowCount() : int
* @param string|object $destinationClass Name of the class or class instance to cast to.
* @param mixed[] $ctorArgs Arguments to use for constructing the destination class instance.
*
* @return object
*
* @throws DB2Exception
*/
private function castObject(stdClass $sourceObject, $destinationClass, array $ctorArgs = [])
private function castObject(stdClass $sourceObject, $destinationClass, array $ctorArgs = []) : object
{
if (! is_string($destinationClass)) {
if (! is_object($destinationClass)) {
Expand Down
Loading

0 comments on commit 33ea073

Please sign in to comment.