Unverified Commit f9a0563f authored by Sergei Morozov's avatar Sergei Morozov Committed by GitHub

Merge pull request #3864 from BenMorel/2.11-nonempty-compositeexpression

CompositeExpression and()/or() factory methods
parents 9b2db8d2 1d503253
......@@ -10,8 +10,9 @@ The usage of the `andX()` and `orX()` methods of the `ExpressionBuilder` class h
## Deprecated `CompositeExpression` methods
The usage of the `add()` and `addMultiple()` methods of the `CompositeExpression` class has been deprecated. Use `with()` instead, which returns a new instance.
- The usage of the `add()` and `addMultiple()` methods of the `CompositeExpression` class has been deprecated. Use `with()` instead, which returns a new instance.
In the future, the `add*()` methods will be removed and the class will be effectively immutable.
- The usage of the `CompositeExpression` constructor has been deprecated. Use the `and()` / `or()` factory methods.
## Deprecated calling `QueryBuilder` methods with an array argument
......
......@@ -3,6 +3,7 @@
namespace Doctrine\DBAL\Query\Expression;
use Countable;
use function array_merge;
use function count;
use function implode;
......@@ -36,6 +37,8 @@ class CompositeExpression implements Countable
private $parts = [];
/**
* @internal Use the and() / or() factory methods.
*
* @param string $type Instance type of composite expression.
* @param self[]|string[] $parts Composition of expressions to be joined on composite expression.
*/
......@@ -46,6 +49,16 @@ class CompositeExpression implements Countable
$this->addMultiple($parts);
}
public static function and($part, ...$parts) : self
{
return new self(self::TYPE_AND, array_merge([$part], $parts));
}
public static function or($part, ...$parts) : self
{
return new self(self::TYPE_OR, array_merge([$part], $parts));
}
/**
* Adds multiple parts to composite expression.
*
......
......@@ -3,7 +3,6 @@
namespace Doctrine\DBAL\Query\Expression;
use Doctrine\DBAL\Connection;
use function array_merge;
use function func_get_arg;
use function func_get_args;
use function func_num_args;
......@@ -47,7 +46,7 @@ class ExpressionBuilder
*/
public function and($expression, ...$expressions) : CompositeExpression
{
return new CompositeExpression(CompositeExpression::TYPE_AND, array_merge([$expression], $expressions));
return CompositeExpression::and($expression, ...$expressions);
}
/**
......@@ -58,7 +57,7 @@ class ExpressionBuilder
*/
public function or($expression, ...$expressions) : CompositeExpression
{
return new CompositeExpression(CompositeExpression::TYPE_OR, array_merge([$expression], $expressions));
return CompositeExpression::or($expression, ...$expressions);
}
/**
......
......@@ -799,7 +799,7 @@ class QueryBuilder
public function where($predicates)
{
if (! (func_num_args() === 1 && $predicates instanceof CompositeExpression)) {
$predicates = new CompositeExpression(CompositeExpression::TYPE_AND, func_get_args());
$predicates = CompositeExpression::and(...func_get_args());
}
return $this->add('where', $predicates);
......@@ -832,7 +832,7 @@ class QueryBuilder
$where = $where->with(...$args);
} else {
array_unshift($args, $where);
$where = new CompositeExpression(CompositeExpression::TYPE_AND, $args);
$where = CompositeExpression::and(...$args);
}
return $this->add('where', $where, true);
......@@ -865,7 +865,7 @@ class QueryBuilder
$where = $where->with(...$args);
} else {
array_unshift($args, $where);
$where = new CompositeExpression(CompositeExpression::TYPE_OR, $args);
$where = CompositeExpression::or(...$args);
}
return $this->add('where', $where, true);
......@@ -990,7 +990,7 @@ class QueryBuilder
public function having($having)
{
if (! (func_num_args() === 1 && $having instanceof CompositeExpression)) {
$having = new CompositeExpression(CompositeExpression::TYPE_AND, func_get_args());
$having = CompositeExpression::and(...func_get_args());
}
return $this->add('having', $having);
......@@ -1013,7 +1013,7 @@ class QueryBuilder
$having = $having->with(...$args);
} else {
array_unshift($args, $having);
$having = new CompositeExpression(CompositeExpression::TYPE_AND, $args);
$having = CompositeExpression::and(...$args);
}
return $this->add('having', $having);
......@@ -1036,7 +1036,7 @@ class QueryBuilder
$having = $having->with(...$args);
} else {
array_unshift($args, $having);
$having = new CompositeExpression(CompositeExpression::TYPE_OR, $args);
$having = CompositeExpression::or(...$args);
}
return $this->add('having', $having);
......
......@@ -12,7 +12,7 @@ class CompositeExpressionTest extends DbalTestCase
{
public function testCount() : void
{
$expr = new CompositeExpression(CompositeExpression::TYPE_OR, ['u.group_id = 1']);
$expr = CompositeExpression::or('u.group_id = 1');
self::assertCount(1, $expr);
......@@ -23,7 +23,7 @@ class CompositeExpressionTest extends DbalTestCase
public function testAdd() : void
{
$expr = new CompositeExpression(CompositeExpression::TYPE_OR, ['u.group_id = 1']);
$expr = CompositeExpression::or('u.group_id = 1');
self::assertCount(1, $expr);
......@@ -31,7 +31,7 @@ class CompositeExpressionTest extends DbalTestCase
self::assertCount(1, $expr);
$expr->add(new CompositeExpression(CompositeExpression::TYPE_OR, ['u.user_id = 1']));
$expr->add(CompositeExpression::or('u.user_id = 1'));
self::assertCount(2, $expr);
......@@ -46,16 +46,16 @@ class CompositeExpressionTest extends DbalTestCase
public function testWith() : void
{
$expr = new CompositeExpression(CompositeExpression::TYPE_OR, ['u.group_id = 1']);
$expr = CompositeExpression::or('u.group_id = 1');
self::assertCount(1, $expr);
// test immutability
$expr->with(new CompositeExpression(CompositeExpression::TYPE_OR, ['u.user_id = 1']));
$expr->with(CompositeExpression::or('u.user_id = 1'));
self::assertCount(1, $expr);
$expr = $expr->with(new CompositeExpression(CompositeExpression::TYPE_OR, ['u.user_id = 1']));
$expr = $expr->with(CompositeExpression::or('u.user_id = 1'));
self::assertCount(2, $expr);
......@@ -106,9 +106,9 @@ class CompositeExpressionTest extends DbalTestCase
CompositeExpression::TYPE_AND,
[
'u.user = 1',
new CompositeExpression(
CompositeExpression::TYPE_OR,
['u.group_id = 1', 'u.group_id = 2']
CompositeExpression::or(
'u.group_id = 1',
'u.group_id = 2'
),
],
'(u.user = 1) AND ((u.group_id = 1) OR (u.group_id = 2))',
......@@ -117,9 +117,9 @@ class CompositeExpressionTest extends DbalTestCase
CompositeExpression::TYPE_OR,
[
'u.group_id = 1',
new CompositeExpression(
CompositeExpression::TYPE_AND,
['u.user = 1', 'u.group_id = 2']
CompositeExpression::and(
'u.user = 1',
'u.group_id = 2'
),
],
'(u.group_id = 1) OR ((u.user = 1) AND (u.group_id = 2))',
......
......@@ -79,9 +79,9 @@ class ExpressionBuilderTest extends DbalTestCase
[
[
'u.user = 1',
new CompositeExpression(
CompositeExpression::TYPE_OR,
['u.group_id = 1', 'u.group_id = 2']
CompositeExpression::or(
'u.group_id = 1',
'u.group_id = 2'
),
],
'(u.user = 1) AND ((u.group_id = 1) OR (u.group_id = 2))',
......@@ -89,9 +89,9 @@ class ExpressionBuilderTest extends DbalTestCase
[
[
'u.group_id = 1',
new CompositeExpression(
CompositeExpression::TYPE_AND,
['u.user = 1', 'u.group_id = 2']
CompositeExpression::and(
'u.user = 1',
'u.group_id = 2'
),
],
'(u.group_id = 1) AND ((u.user = 1) AND (u.group_id = 2))',
......@@ -152,9 +152,9 @@ class ExpressionBuilderTest extends DbalTestCase
[
[
'u.user = 1',
new CompositeExpression(
CompositeExpression::TYPE_OR,
['u.group_id = 1', 'u.group_id = 2']
CompositeExpression::or(
'u.group_id = 1',
'u.group_id = 2'
),
],
'(u.user = 1) OR ((u.group_id = 1) OR (u.group_id = 2))',
......@@ -162,9 +162,9 @@ class ExpressionBuilderTest extends DbalTestCase
[
[
'u.group_id = 1',
new CompositeExpression(
CompositeExpression::TYPE_AND,
['u.user = 1', 'u.group_id = 2']
CompositeExpression::and(
'u.user = 1',
'u.group_id = 2'
),
],
'(u.group_id = 1) OR ((u.user = 1) AND (u.group_id = 2))',
......
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