Unverified Commit 8b03048c authored by Sergei Morozov's avatar Sergei Morozov

Merge pull request #3574 from jwage/query-types

Add proper types to Doctrine\DBAL\Query namespace.
parents 8d7874f5 598bcfd7
......@@ -41,7 +41,7 @@ class CompositeExpression implements Countable
* @param string $type Instance type of composite expression.
* @param self[]|string[] $parts Composition of expressions to be joined on composite expression.
*/
public function __construct($type, array $parts = [])
public function __construct(string $type, array $parts = [])
{
$this->type = $type;
......@@ -51,11 +51,11 @@ class CompositeExpression implements Countable
/**
* Adds multiple parts to composite expression.
*
* @param self[]|string[] $parts
* @param array<int, self|string> $parts
*
* @return \Doctrine\DBAL\Query\Expression\CompositeExpression
* @return $this
*/
public function addMultiple(array $parts = [])
public function addMultiple(array $parts = []) : self
{
foreach ($parts as $part) {
$this->add($part);
......@@ -67,11 +67,11 @@ class CompositeExpression implements Countable
/**
* Adds an expression to composite expression.
*
* @param mixed $part
* @param self|string $part
*
* @return \Doctrine\DBAL\Query\Expression\CompositeExpression
* @return $this
*/
public function add($part)
public function add($part) : self
{
if (empty($part)) {
return $this;
......@@ -88,20 +88,16 @@ class CompositeExpression implements Countable
/**
* Retrieves the amount of expressions on composite expression.
*
* @return int
*/
public function count()
public function count() : int
{
return count($this->parts);
}
/**
* Retrieves the string representation of this composite expression.
*
* @return string
*/
public function __toString()
public function __toString() : string
{
if ($this->count() === 1) {
return (string) $this->parts[0];
......@@ -112,10 +108,8 @@ class CompositeExpression implements Countable
/**
* Returns the type of this composite expression (AND/OR).
*
* @return string
*/
public function getType()
public function getType() : string
{
return $this->type;
}
......
......@@ -51,10 +51,8 @@ class ExpressionBuilder
*
* @param mixed $x Optional clause. Defaults = null, but requires
* at least one defined when converting to string.
*
* @return CompositeExpression
*/
public function andX($x = null)
public function andX($x = null) : CompositeExpression
{
return new CompositeExpression(CompositeExpression::TYPE_AND, func_get_args());
}
......@@ -70,10 +68,8 @@ class ExpressionBuilder
*
* @param mixed $x Optional clause. Defaults = null, but requires
* at least one defined when converting to string.
*
* @return CompositeExpression
*/
public function orX($x = null)
public function orX($x = null) : CompositeExpression
{
return new CompositeExpression(CompositeExpression::TYPE_OR, func_get_args());
}
......@@ -84,10 +80,8 @@ class ExpressionBuilder
* @param mixed $x The left expression.
* @param string $operator One of the ExpressionBuilder::* constants.
* @param mixed $y The right expression.
*
* @return string
*/
public function comparison($x, $operator, $y)
public function comparison($x, string $operator, $y) : string
{
return $x . ' ' . $operator . ' ' . $y;
}
......@@ -104,10 +98,8 @@ class ExpressionBuilder
*
* @param mixed $x The left expression.
* @param mixed $y The right expression.
*
* @return string
*/
public function eq($x, $y)
public function eq($x, $y) : string
{
return $this->comparison($x, self::EQ, $y);
}
......@@ -123,10 +115,8 @@ class ExpressionBuilder
*
* @param mixed $x The left expression.
* @param mixed $y The right expression.
*
* @return string
*/
public function neq($x, $y)
public function neq($x, $y) : string
{
return $this->comparison($x, self::NEQ, $y);
}
......@@ -142,10 +132,8 @@ class ExpressionBuilder
*
* @param mixed $x The left expression.
* @param mixed $y The right expression.
*
* @return string
*/
public function lt($x, $y)
public function lt($x, $y) : string
{
return $this->comparison($x, self::LT, $y);
}
......@@ -161,10 +149,8 @@ class ExpressionBuilder
*
* @param mixed $x The left expression.
* @param mixed $y The right expression.
*
* @return string
*/
public function lte($x, $y)
public function lte($x, $y) : string
{
return $this->comparison($x, self::LTE, $y);
}
......@@ -180,10 +166,8 @@ class ExpressionBuilder
*
* @param mixed $x The left expression.
* @param mixed $y The right expression.
*
* @return string
*/
public function gt($x, $y)
public function gt($x, $y) : string
{
return $this->comparison($x, self::GT, $y);
}
......@@ -199,10 +183,8 @@ class ExpressionBuilder
*
* @param mixed $x The left expression.
* @param mixed $y The right expression.
*
* @return string
*/
public function gte($x, $y)
public function gte($x, $y) : string
{
return $this->comparison($x, self::GTE, $y);
}
......@@ -211,10 +193,8 @@ class ExpressionBuilder
* Creates an IS NULL expression with the given arguments.
*
* @param string $x The field in string format to be restricted by IS NULL.
*
* @return string
*/
public function isNull($x)
public function isNull(string $x) : string
{
return $x . ' IS NULL';
}
......@@ -223,10 +203,8 @@ class ExpressionBuilder
* Creates an IS NOT NULL expression with the given arguments.
*
* @param string $x The field in string format to be restricted by IS NOT NULL.
*
* @return string
*/
public function isNotNull($x)
public function isNotNull(string $x) : string
{
return $x . ' IS NOT NULL';
}
......@@ -236,10 +214,8 @@ class ExpressionBuilder
*
* @param string $x Field in string format to be inspected by LIKE() comparison.
* @param mixed $y Argument to be used in LIKE() comparison.
*
* @return string
*/
public function like($x, $y/*, ?string $escapeChar = null */)
public function like(string $x, $y/*, ?string $escapeChar = null */) : string
{
return $this->comparison($x, 'LIKE', $y) .
(func_num_args() >= 3 ? sprintf(' ESCAPE %s', func_get_arg(2)) : '');
......@@ -250,10 +226,8 @@ class ExpressionBuilder
*
* @param string $x Field in string format to be inspected by NOT LIKE() comparison.
* @param mixed $y Argument to be used in NOT LIKE() comparison.
*
* @return string
*/
public function notLike($x, $y/*, ?string $escapeChar = null */)
public function notLike(string $x, $y/*, ?string $escapeChar = null */) : string
{
return $this->comparison($x, 'NOT LIKE', $y) .
(func_num_args() >= 3 ? sprintf(' ESCAPE %s', func_get_arg(2)) : '');
......@@ -264,10 +238,8 @@ class ExpressionBuilder
*
* @param string $x The field in string format to be inspected by IN() comparison.
* @param string|string[] $y The placeholder or the array of values to be used by IN() comparison.
*
* @return string
*/
public function in($x, $y)
public function in(string $x, $y) : string
{
return $this->comparison($x, 'IN', '(' . implode(', ', (array) $y) . ')');
}
......@@ -277,10 +249,8 @@ class ExpressionBuilder
*
* @param string $x The field in string format to be inspected by NOT IN() comparison.
* @param string|string[] $y The placeholder or the array of values to be used by NOT IN() comparison.
*
* @return string
*/
public function notIn($x, $y)
public function notIn(string $x, $y) : string
{
return $this->comparison($x, 'NOT IN', '(' . implode(', ', (array) $y) . ')');
}
......@@ -288,7 +258,7 @@ class ExpressionBuilder
/**
* Creates an SQL literal expression from the string.
*/
public function literal(string $input)
public function literal(string $input) : string
{
return $this->connection->quote($input);
}
......
......@@ -75,7 +75,7 @@ class QueryBuilder
/**
* The array of SQL parts collected.
*
* @var mixed[]
* @var array<string, mixed>
*/
private $sqlParts = self::SQL_PARTS_DEFAULTS;
......@@ -89,14 +89,14 @@ class QueryBuilder
/**
* The query parameters.
*
* @var mixed[]
* @var array<int, mixed>|array<string, mixed>
*/
private $params = [];
/**
* The parameter type map of this query.
*
* @var int[]|string[]
* @var array<int, mixed>|array<string, mixed>
*/
private $paramTypes = [];
......@@ -119,14 +119,14 @@ class QueryBuilder
*
* @var int
*/
private $firstResult = null;
private $firstResult = 0;
/**
* The maximum number of results to retrieve.
*
* @var int
* @var int|null
*/
private $maxResults = null;
private $maxResults;
/**
* The counter of bound parameters used with {@see bindValue).
......@@ -158,30 +158,24 @@ class QueryBuilder
*
* For more complex expression construction, consider storing the expression
* builder object in a local variable.
*
* @return ExpressionBuilder
*/
public function expr()
public function expr() : ExpressionBuilder
{
return $this->connection->getExpressionBuilder();
}
/**
* Gets the type of the currently built query.
*
* @return int
*/
public function getType()
public function getType() : int
{
return $this->type;
}
/**
* Gets the associated DBAL Connection for this query builder.
*
* @return Connection
*/
public function getConnection()
public function getConnection() : Connection
{
return $this->connection;
}
......@@ -191,7 +185,7 @@ class QueryBuilder
*
* @return int Either QueryBuilder::STATE_DIRTY or QueryBuilder::STATE_CLEAN.
*/
public function getState()
public function getState() : int
{
return $this->state;
}
......@@ -225,7 +219,7 @@ class QueryBuilder
*
* @return string The SQL query string.
*/
public function getSQL()
public function getSQL() : string
{
if ($this->sql !== null && $this->state === self::STATE_CLEAN) {
return $this->sql;
......@@ -272,7 +266,7 @@ class QueryBuilder
*
* @return $this This QueryBuilder instance.
*/
public function setParameter($key, $value, $type = null)
public function setParameter($key, $value, $type = null) : self
{
if ($type !== null) {
$this->paramTypes[$key] = $type;
......@@ -297,12 +291,12 @@ class QueryBuilder
* ));
* </code>
*
* @param mixed[] $params The query parameters to set.
* @param int[]|string[] $types The query parameters types to set.
* @param array<int, mixed>|array<string, mixed> $params The query parameters to set.
* @param array<int, mixed>|array<string, mixed> $types The query parameters types to set.
*
* @return $this This QueryBuilder instance.
*/
public function setParameters(array $params, array $types = [])
public function setParameters(array $params, array $types = []) : self
{
$this->paramTypes = $types;
$this->params = $params;
......@@ -313,9 +307,9 @@ class QueryBuilder
/**
* Gets all defined query parameters for the query being constructed indexed by parameter index or name.
*
* @return mixed[] The currently defined query parameters indexed by parameter index or name.
* @return array<string|int, mixed> The currently defined query parameters indexed by parameter index or name.
*/
public function getParameters()
public function getParameters() : array
{
return $this->params;
}
......@@ -323,7 +317,7 @@ class QueryBuilder
/**
* Gets a (previously set) query parameter of the query being constructed.
*
* @param mixed $key The key (index or name) of the bound parameter.
* @param string|int $key The key (index or name) of the bound parameter.
*
* @return mixed The value of the bound parameter.
*/
......@@ -335,9 +329,9 @@ class QueryBuilder
/**
* Gets all defined query parameter types for the query being constructed indexed by parameter index or name.
*
* @return int[]|string[] The currently defined query parameter types indexed by parameter index or name.
* @return array<string|int, mixed> The currently defined query parameter types indexed by parameter index or name.
*/
public function getParameterTypes()
public function getParameterTypes() : array
{
return $this->paramTypes;
}
......@@ -345,7 +339,7 @@ class QueryBuilder
/**
* Gets a (previously set) query parameter type of the query being constructed.
*
* @param mixed $key The key (index or name) of the bound parameter type.
* @param string|int $key The key (index or name) of the bound parameter type.
*
* @return mixed The value of the bound parameter type.
*/
......@@ -361,7 +355,7 @@ class QueryBuilder
*
* @return $this This QueryBuilder instance.
*/
public function setFirstResult($firstResult)
public function setFirstResult(int $firstResult) : self
{
$this->state = self::STATE_DIRTY;
$this->firstResult = $firstResult;
......@@ -375,7 +369,7 @@ class QueryBuilder
*
* @return int The position of the first result.
*/
public function getFirstResult()
public function getFirstResult() : int
{
return $this->firstResult;
}
......@@ -387,7 +381,7 @@ class QueryBuilder
*
* @return $this This QueryBuilder instance.
*/
public function setMaxResults($maxResults)
public function setMaxResults(int $maxResults) : self
{
$this->state = self::STATE_DIRTY;
$this->maxResults = $maxResults;
......@@ -401,7 +395,7 @@ class QueryBuilder
*
* @return int The maximum number of results.
*/
public function getMaxResults()
public function getMaxResults() : int
{
return $this->maxResults;
}
......@@ -412,13 +406,11 @@ class QueryBuilder
* The available parts are: 'select', 'from', 'set', 'where',
* 'groupBy', 'having' and 'orderBy'.
*
* @param string $sqlPartName
* @param mixed $sqlPart
* @param bool $append
*
* @return $this This QueryBuilder instance.
*/
public function add($sqlPartName, $sqlPart, $append = false)
public function add(string $sqlPartName, $sqlPart, bool $append = false) : self
{
$isArray = is_array($sqlPart);
$isMultiple = is_array($this->sqlParts[$sqlPartName]);
......@@ -466,7 +458,7 @@ class QueryBuilder
*
* @return $this This QueryBuilder instance.
*/
public function select($select = null)
public function select($select = null) : self
{
$this->type = self::SELECT;
......@@ -513,7 +505,7 @@ class QueryBuilder
*
* @return $this This QueryBuilder instance.
*/
public function addSelect($select = null)
public function addSelect($select = null) : self
{
$this->type = self::SELECT;
......@@ -542,7 +534,7 @@ class QueryBuilder
*
* @return $this This QueryBuilder instance.
*/
public function delete($delete = null, $alias = null)
public function delete(?string $delete = null, ?string $alias = null) : self
{
$this->type = self::DELETE;
......@@ -572,7 +564,7 @@ class QueryBuilder
*
* @return $this This QueryBuilder instance.
*/
public function update($update = null, $alias = null)
public function update(?string $update = null, ?string $alias = null) : self
{
$this->type = self::UPDATE;
......@@ -605,7 +597,7 @@ class QueryBuilder
*
* @return $this This QueryBuilder instance.
*/
public function insert($insert = null)
public function insert(?string $insert = null) : self
{
$this->type = self::INSERT;
......@@ -631,7 +623,7 @@ class QueryBuilder
*
* @return $this This QueryBuilder instance.
*/
public function from($from, $alias = null)
public function from(string $from, ?string $alias = null)
{
return $this->add('from', [
'table' => $from,
......@@ -656,7 +648,7 @@ class QueryBuilder
*
* @return $this This QueryBuilder instance.
*/
public function join($fromAlias, $join, $alias, $condition = null)
public function join(string $fromAlias, string $join, string $alias, ?string $condition = null)
{
return $this->innerJoin($fromAlias, $join, $alias, $condition);
}
......@@ -678,7 +670,7 @@ class QueryBuilder
*
* @return $this This QueryBuilder instance.
*/
public function innerJoin($fromAlias, $join, $alias, $condition = null)
public function innerJoin(string $fromAlias, string $join, string $alias, ?string $condition = null)
{
return $this->add('join', [
$fromAlias => [
......@@ -707,7 +699,7 @@ class QueryBuilder
*
* @return $this This QueryBuilder instance.
*/
public function leftJoin($fromAlias, $join, $alias, $condition = null)
public function leftJoin(string $fromAlias, string $join, string $alias, ?string $condition = null)
{
return $this->add('join', [
$fromAlias => [
......@@ -736,7 +728,7 @@ class QueryBuilder
*
* @return $this This QueryBuilder instance.
*/
public function rightJoin($fromAlias, $join, $alias, $condition = null)
public function rightJoin(string $fromAlias, string $join, string $alias, ?string $condition = null)
{
return $this->add('join', [
$fromAlias => [
......@@ -763,7 +755,7 @@ class QueryBuilder
*
* @return $this This QueryBuilder instance.
*/
public function set($key, $value)
public function set(string $key, string $value)
{
return $this->add('set', $key . ' = ' . $value, true);
}
......@@ -884,7 +876,7 @@ class QueryBuilder
*
* @return $this This QueryBuilder instance.
*/
public function groupBy($groupBy)
public function groupBy($groupBy) : self
{
if (empty($groupBy)) {
return $this;
......@@ -910,7 +902,7 @@ class QueryBuilder
*
* @return $this This QueryBuilder instance.
*/
public function addGroupBy($groupBy)
public function addGroupBy($groupBy) : self
{
if (empty($groupBy)) {
return $this;
......@@ -940,7 +932,7 @@ class QueryBuilder
*
* @return $this This QueryBuilder instance.
*/
public function setValue($column, $value)
public function setValue(string $column, string $value) : self
{
$this->sqlParts['values'][$column] = $value;
......@@ -962,7 +954,7 @@ class QueryBuilder
* );
* </code>
*
* @param mixed[] $values The values to specify for the insert query indexed by column names.
* @param array<int, mixed> $values The values to specify for the insert query indexed by column names.
*
* @return $this This QueryBuilder instance.
*/
......@@ -1043,7 +1035,7 @@ class QueryBuilder
*
* @return $this This QueryBuilder instance.
*/
public function orderBy($sort, $order = null)
public function orderBy(string $sort, ?string $order = null)
{
return $this->add('orderBy', $sort . ' ' . (! $order ? 'ASC' : $order), false);
}
......@@ -1056,7 +1048,7 @@ class QueryBuilder
*
* @return $this This QueryBuilder instance.
*/
public function addOrderBy($sort, $order = null)
public function addOrderBy(string $sort, ?string $order = null)
{
return $this->add('orderBy', $sort . ' ' . (! $order ? 'ASC' : $order), true);
}
......@@ -1064,11 +1056,9 @@ class QueryBuilder
/**
* Gets a query part by its name.
*
* @param string $queryPartName
*
* @return mixed
*/
public function getQueryPart($queryPartName)
public function getQueryPart(string $queryPartName)
{
return $this->sqlParts[$queryPartName];
}
......@@ -1076,9 +1066,9 @@ class QueryBuilder
/**
* Gets all query parts.
*
* @return mixed[]
* @return array<string, mixed>
*/
public function getQueryParts()
public function getQueryParts() : array
{
return $this->sqlParts;
}
......@@ -1086,11 +1076,11 @@ class QueryBuilder
/**
* Resets SQL parts.
*
* @param string[]|null $queryPartNames
* @param array<int, string>|null $queryPartNames
*
* @return $this This QueryBuilder instance.
*/
public function resetQueryParts($queryPartNames = null)
public function resetQueryParts(?array $queryPartNames = null) : self
{
if ($queryPartNames === null) {
$queryPartNames = array_keys($this->sqlParts);
......@@ -1106,11 +1096,9 @@ class QueryBuilder
/**
* Resets a single SQL part.
*
* @param string $queryPartName
*
* @return $this This QueryBuilder instance.
*/
public function resetQueryPart($queryPartName)
public function resetQueryPart(string $queryPartName) : self
{
$this->sqlParts[$queryPartName] = self::SQL_PARTS_DEFAULTS[$queryPartName];
......@@ -1120,11 +1108,9 @@ class QueryBuilder
}
/**
* @return string
*
* @throws QueryException
*/
private function getSQLForSelect()
private function getSQLForSelect() : string
{
$query = 'SELECT ' . ($this->sqlParts['distinct'] ? 'DISTINCT ' : '') .
implode(', ', $this->sqlParts['select']);
......@@ -1147,9 +1133,9 @@ class QueryBuilder
}
/**
* @return string[]
* @return array<string, string>
*/
private function getFromClauses()
private function getFromClauses() : array
{
$fromClauses = [];
$knownAliases = [];
......@@ -1158,9 +1144,13 @@ class QueryBuilder
foreach ($this->sqlParts['from'] as $from) {
if ($from['alias'] === null || $from['alias'] === $from['table']) {
$tableSql = $from['table'];
/** @var string $tableReference */
$tableReference = $from['table'];
} else {
$tableSql = $from['table'] . ' ' . $from['alias'];
/** @var string $tableReference */
$tableReference = $from['alias'];
}
......@@ -1175,11 +1165,11 @@ class QueryBuilder
}
/**
* @param string[] $knownAliases
* @param array<string, true> $knownAliases
*
* @throws QueryException
*/
private function verifyAllAliasesAreKnown(array $knownAliases)
private function verifyAllAliasesAreKnown(array $knownAliases) : void
{
foreach ($this->sqlParts['join'] as $fromAlias => $joins) {
if (! isset($knownAliases[$fromAlias])) {
......@@ -1188,20 +1178,15 @@ class QueryBuilder
}
}
/**
* @return bool
*/
private function isLimitQuery()
private function isLimitQuery() : bool
{
return $this->maxResults !== null || $this->firstResult !== null;
return $this->maxResults !== null || $this->firstResult !== 0;
}
/**
* Converts this instance into an INSERT string in SQL.
*
* @return string
*/
private function getSQLForInsert()
private function getSQLForInsert() : string
{
return 'INSERT INTO ' . $this->sqlParts['from']['table'] .
' (' . implode(', ', array_keys($this->sqlParts['values'])) . ')' .
......@@ -1210,10 +1195,8 @@ class QueryBuilder
/**
* Converts this instance into an UPDATE string in SQL.
*
* @return string
*/
private function getSQLForUpdate()
private function getSQLForUpdate() : string
{
$from = $this->sqlParts['from'];
......@@ -1230,10 +1213,8 @@ class QueryBuilder
/**
* Converts this instance into a DELETE string in SQL.
*
* @return string
*/
private function getSQLForDelete()
private function getSQLForDelete() : string
{
$from = $this->sqlParts['from'];
......@@ -1252,7 +1233,7 @@ class QueryBuilder
*
* @return string The string representation of this QueryBuilder.
*/
public function __toString()
public function __toString() : string
{
return $this->getSQL();
}
......@@ -1285,7 +1266,7 @@ class QueryBuilder
*
* @return string the placeholder name used.
*/
public function createNamedParameter($value, $type = ParameterType::STRING, $placeHolder = null)
public function createNamedParameter($value, $type = ParameterType::STRING, ?string $placeHolder = null) : string
{
if ($placeHolder === null) {
$this->boundCounter++;
......@@ -1314,11 +1295,8 @@ class QueryBuilder
* </code>
*
* @param mixed $value
* @param int $type
*
* @return string
*/
public function createPositionalParameter($value, $type = ParameterType::STRING)
public function createPositionalParameter($value, int $type = ParameterType::STRING) : string
{
$this->boundCounter++;
$this->setParameter($this->boundCounter, $value, $type);
......@@ -1327,14 +1305,11 @@ class QueryBuilder
}
/**
* @param string $fromAlias
* @param string[] $knownAliases
*
* @return string
* @param array<string, true> $knownAliases
*
* @throws QueryException
*/
private function getSQLForJoins($fromAlias, array &$knownAliases)
private function getSQLForJoins(string $fromAlias, array &$knownAliases) : string
{
$sql = '';
......@@ -1359,8 +1334,6 @@ class QueryBuilder
/**
* Deep clone of all expression objects in the SQL parts.
*
* @return void
*/
public function __clone()
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment