Skip to content

Commit

Permalink
Merge pull request #3574 from jwage/query-types
Browse files Browse the repository at this point in the history
Add proper types to Doctrine\DBAL\Query namespace.
  • Loading branch information
morozov committed Jun 13, 2019
2 parents 96d6ce4 + 2f9d661 commit c045a08
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 160 deletions.
26 changes: 10 additions & 16 deletions lib/Doctrine/DBAL/Query/Expression/CompositeExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -51,11 +51,11 @@ public function __construct($type, array $parts = [])
/**
* 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);
Expand All @@ -67,11 +67,11 @@ public function addMultiple(array $parts = [])
/**
* 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;
Expand All @@ -88,20 +88,16 @@ public function add($part)

/**
* 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];
Expand All @@ -112,10 +108,8 @@ public function __toString()

/**
* Returns the type of this composite expression (AND/OR).
*
* @return string
*/
public function getType()
public function getType() : string
{
return $this->type;
}
Expand Down
62 changes: 16 additions & 46 deletions lib/Doctrine/DBAL/Query/Expression/ExpressionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,8 @@ public function __construct(Connection $connection)
*
* @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());
}
Expand All @@ -70,10 +68,8 @@ public function andX($x = null)
*
* @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());
}
Expand All @@ -84,10 +80,8 @@ public function orX($x = null)
* @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;
}
Expand All @@ -104,10 +98,8 @@ public function comparison($x, $operator, $y)
*
* @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);
}
Expand All @@ -123,10 +115,8 @@ public function eq($x, $y)
*
* @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);
}
Expand All @@ -142,10 +132,8 @@ public function neq($x, $y)
*
* @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);
}
Expand All @@ -161,10 +149,8 @@ public function lt($x, $y)
*
* @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);
}
Expand All @@ -180,10 +166,8 @@ public function lte($x, $y)
*
* @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);
}
Expand All @@ -199,10 +183,8 @@ public function gt($x, $y)
*
* @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);
}
Expand All @@ -211,10 +193,8 @@ public function gte($x, $y)
* 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';
}
Expand All @@ -223,10 +203,8 @@ public function isNull($x)
* 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';
}
Expand All @@ -236,10 +214,8 @@ public function isNotNull($x)
*
* @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)) : '');
Expand All @@ -250,10 +226,8 @@ public function like($x, $y/*, ?string $escapeChar = null */)
*
* @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)) : '');
Expand All @@ -264,10 +238,8 @@ public function notLike($x, $y/*, ?string $escapeChar = null */)
*
* @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) . ')');
}
Expand All @@ -277,18 +249,16 @@ public function in($x, $y)
*
* @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) . ')');
}

/**
* Creates an SQL literal expression from the string.
*/
public function literal(string $input)
public function literal(string $input) : string
{
return $this->connection->quote($input);
}
Expand Down
Loading

0 comments on commit c045a08

Please sign in to comment.