Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First parameter of ExpressionBuilder::and/or() mandatory #3852

Merged
merged 1 commit into from
Jan 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions lib/Doctrine/DBAL/Query/Expression/ExpressionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}

/**
Expand All @@ -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());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to revert this to its original value; we can't delegate to and(), as andX() accepts an empty parameter list.

}

/**
Expand All @@ -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());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I split tests into and() and andX(), to ensure that both are covered.


foreach ($parts as $part) {
$composite->add($part);
Expand Down Expand Up @@ -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);
Expand Down