From 4566a9b6ff15d2cd4e789b6852b0d872cae6def0 Mon Sep 17 00:00:00 2001 From: Benjamin Morel Date: Tue, 28 Jan 2020 15:51:23 +0100 Subject: [PATCH] First parameter of ExpressionBuilder::and/or() mandatory --- .../Query/Expression/ExpressionBuilder.php | 19 +++++++------ .../Expression/ExpressionBuilderTest.php | 28 +++++++++++++++++-- 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/lib/Doctrine/DBAL/Query/Expression/ExpressionBuilder.php b/lib/Doctrine/DBAL/Query/Expression/ExpressionBuilder.php index b8649b5e1d8..b946c88f9ac 100644 --- a/lib/Doctrine/DBAL/Query/Expression/ExpressionBuilder.php +++ b/lib/Doctrine/DBAL/Query/Expression/ExpressionBuilder.php @@ -3,6 +3,7 @@ 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; @@ -41,21 +42,23 @@ public function __construct(Connection $connection) /** * Creates a conjunction of the given expressions. * - * @param string|CompositeExpression ...$expressions Requires at least one defined when converting to string. + * @param string|CompositeExpression $expression + * @param string|CompositeExpression ...$expressions */ - public function and(...$expressions) : CompositeExpression + public function and($expression, ...$expressions) : CompositeExpression { - return new CompositeExpression(CompositeExpression::TYPE_AND, $expressions); + return new CompositeExpression(CompositeExpression::TYPE_AND, array_merge([$expression], $expressions)); } /** * Creates a disjunction of the given expressions. * - * @param string|CompositeExpression ...$expressions Requires at least one defined when converting to string. + * @param string|CompositeExpression $expression + * @param string|CompositeExpression ...$expressions */ - public function or(...$expressions) : CompositeExpression + public function or($expression, ...$expressions) : CompositeExpression { - return new CompositeExpression(CompositeExpression::TYPE_OR, $expressions); + return new CompositeExpression(CompositeExpression::TYPE_OR, array_merge([$expression], $expressions)); } /** @@ -68,7 +71,7 @@ public function or(...$expressions) : CompositeExpression */ public function andX($x = null) { - return $this->and(...func_get_args()); + return new CompositeExpression(CompositeExpression::TYPE_AND, func_get_args()); } /** @@ -81,7 +84,7 @@ public function andX($x = null) */ public function orX($x = null) { - return $this->or(...func_get_args()); + return new CompositeExpression(CompositeExpression::TYPE_OR, func_get_args()); } /** diff --git a/tests/Doctrine/Tests/DBAL/Query/Expression/ExpressionBuilderTest.php b/tests/Doctrine/Tests/DBAL/Query/Expression/ExpressionBuilderTest.php index 5f5d3d6aad1..190ff99e4e4 100644 --- a/tests/Doctrine/Tests/DBAL/Query/Expression/ExpressionBuilderTest.php +++ b/tests/Doctrine/Tests/DBAL/Query/Expression/ExpressionBuilderTest.php @@ -33,7 +33,19 @@ protected function setUp() : void */ public function testAnd(array $parts, string $expected) : void { - $composite = $this->expr->and(); + $composite = $this->expr->and(...$parts); + + self::assertEquals($expected, (string) $composite); + } + + /** + * @param string[]|CompositeExpression[] $parts + * + * @dataProvider provideDataForAnd + */ + public function testAndX(array $parts, string $expected) : void + { + $composite = $this->expr->andX(); foreach ($parts as $part) { $composite->add($part); @@ -94,7 +106,19 @@ public static function provideDataForAnd() : iterable */ public function testOr(array $parts, string $expected) : void { - $composite = $this->expr->or(); + $composite = $this->expr->or(...$parts); + + self::assertEquals($expected, (string) $composite); + } + + /** + * @param string[]|CompositeExpression[] $parts + * + * @dataProvider provideDataForOr + */ + public function testOrX(array $parts, string $expected) : void + { + $composite = $this->expr->orX(); foreach ($parts as $part) { $composite->add($part);