From 1d503253ff57987161f10d4f2c81b5a9889d48a4 Mon Sep 17 00:00:00 2001 From: Benjamin Morel Date: Thu, 30 Jan 2020 13:06:36 +0100 Subject: [PATCH] CompositeExpression and()/or() factory methods --- UPGRADE.md | 3 ++- .../Query/Expression/CompositeExpression.php | 13 ++++++++++ .../Query/Expression/ExpressionBuilder.php | 5 ++-- lib/Doctrine/DBAL/Query/QueryBuilder.php | 12 +++++----- .../Expression/CompositeExpressionTest.php | 24 +++++++++---------- .../Expression/ExpressionBuilderTest.php | 24 +++++++++---------- 6 files changed, 47 insertions(+), 34 deletions(-) diff --git a/UPGRADE.md b/UPGRADE.md index 63dd8b279dd..964c210e888 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -6,8 +6,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 diff --git a/lib/Doctrine/DBAL/Query/Expression/CompositeExpression.php b/lib/Doctrine/DBAL/Query/Expression/CompositeExpression.php index 204a356c2f2..0048d228ced 100644 --- a/lib/Doctrine/DBAL/Query/Expression/CompositeExpression.php +++ b/lib/Doctrine/DBAL/Query/Expression/CompositeExpression.php @@ -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 @@ public function __construct($type, array $parts = []) $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. * diff --git a/lib/Doctrine/DBAL/Query/Expression/ExpressionBuilder.php b/lib/Doctrine/DBAL/Query/Expression/ExpressionBuilder.php index b946c88f9ac..ef0e6eebb00 100644 --- a/lib/Doctrine/DBAL/Query/Expression/ExpressionBuilder.php +++ b/lib/Doctrine/DBAL/Query/Expression/ExpressionBuilder.php @@ -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 @@ public function __construct(Connection $connection) */ 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 @@ public function and($expression, ...$expressions) : CompositeExpression */ public function or($expression, ...$expressions) : CompositeExpression { - return new CompositeExpression(CompositeExpression::TYPE_OR, array_merge([$expression], $expressions)); + return CompositeExpression::or($expression, ...$expressions); } /** diff --git a/lib/Doctrine/DBAL/Query/QueryBuilder.php b/lib/Doctrine/DBAL/Query/QueryBuilder.php index c6c43ea1c72..d7b70a5d4ef 100644 --- a/lib/Doctrine/DBAL/Query/QueryBuilder.php +++ b/lib/Doctrine/DBAL/Query/QueryBuilder.php @@ -799,7 +799,7 @@ public function set($key, $value) 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 @@ public function andWhere($where) $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 @@ public function orWhere($where) $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 @@ public function values(array $values) 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 @@ public function andHaving($having) $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 @@ public function orHaving($having) $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); diff --git a/tests/Doctrine/Tests/DBAL/Query/Expression/CompositeExpressionTest.php b/tests/Doctrine/Tests/DBAL/Query/Expression/CompositeExpressionTest.php index fd02e25f6f7..1e9eee344d1 100644 --- a/tests/Doctrine/Tests/DBAL/Query/Expression/CompositeExpressionTest.php +++ b/tests/Doctrine/Tests/DBAL/Query/Expression/CompositeExpressionTest.php @@ -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 @@ public function testCount() : void 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 @@ public function testAdd() : void 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 @@ public function testAdd() : void 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 @@ public static function provideDataForConvertToString() : iterable 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 @@ public static function provideDataForConvertToString() : iterable 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))', diff --git a/tests/Doctrine/Tests/DBAL/Query/Expression/ExpressionBuilderTest.php b/tests/Doctrine/Tests/DBAL/Query/Expression/ExpressionBuilderTest.php index 190ff99e4e4..1c1915d7cb3 100644 --- a/tests/Doctrine/Tests/DBAL/Query/Expression/ExpressionBuilderTest.php +++ b/tests/Doctrine/Tests/DBAL/Query/Expression/ExpressionBuilderTest.php @@ -79,9 +79,9 @@ public static function provideDataForAnd() : iterable [ [ '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 @@ public static function provideDataForAnd() : iterable [ [ '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 @@ public static function provideDataForOr() : iterable [ [ '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 @@ public static function provideDataForOr() : iterable [ [ '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))',