Commit e8497cf0 authored by Benjamin Morel's avatar Benjamin Morel

Make CompositeExpression immutable

parent 43819af1
# Upgrade to 3.0 # Upgrade to 3.0
## BC BREAK: Removed `CompositeExpression` methods
The `add()` and `addMultiple()` methods of the `CompositeExpression` class have been removed. Use `with()` instead, which returns a new instance.
The `CompositeExpression` class is now immutable.
## BC BREAK: Changes in the QueryBuilder API. ## BC BREAK: Changes in the QueryBuilder API.
1. The `select()`, `addSelect()`, `groupBy()` and `addGroupBy()` methods no longer accept an array of arguments. Pass each expression as an individual argument or expand an array of expressions using the `...` operator. 1. The `select()`, `addSelect()`, `groupBy()` and `addGroupBy()` methods no longer accept an array of arguments. Pass each expression as an individual argument or expand an array of expressions using the `...` operator.
......
...@@ -5,11 +5,15 @@ declare(strict_types=1); ...@@ -5,11 +5,15 @@ declare(strict_types=1);
namespace Doctrine\DBAL\Query\Expression; namespace Doctrine\DBAL\Query\Expression;
use Countable; use Countable;
use function array_filter;
use function array_values;
use function count; use function count;
use function implode; use function implode;
/** /**
* Composite expression is responsible to build a group of similar expression. * Composite expression is responsible to build a group of similar expression.
*
* This class is immutable.
*/ */
class CompositeExpression implements Countable class CompositeExpression implements Countable
{ {
...@@ -43,51 +47,10 @@ class CompositeExpression implements Countable ...@@ -43,51 +47,10 @@ class CompositeExpression implements Countable
*/ */
public function __construct(string $type, array $parts = []) public function __construct(string $type, array $parts = [])
{ {
$this->type = $type; $this->type = $type;
$this->parts = array_values(array_filter($parts, static function ($part) {
$this->addMultiple($parts); return ! ($part instanceof self && count($part) === 0);
} }));
/**
* Adds multiple parts to composite expression.
*
* @deprecated This class will be made immutable. Use with() instead.
*
* @param array<int, self|string> $parts
*
* @return $this
*/
public function addMultiple(array $parts = []) : self
{
foreach ($parts as $part) {
$this->add($part);
}
return $this;
}
/**
* Adds an expression to composite expression.
*
* @deprecated This class will be made immutable. Use with() instead.
*
* @param self|string $part
*
* @return $this
*/
public function add($part) : self
{
if (empty($part)) {
return $this;
}
if ($part instanceof self && count($part) === 0) {
return $this;
}
$this->parts[] = $part;
return $this;
} }
/** /**
......
...@@ -1039,7 +1039,7 @@ class QueryBuilder ...@@ -1039,7 +1039,7 @@ class QueryBuilder
private function appendToPredicate($currentPredicate, string $type, ...$predicates) private function appendToPredicate($currentPredicate, string $type, ...$predicates)
{ {
if ($currentPredicate instanceof CompositeExpression && $currentPredicate->getType() === $type) { if ($currentPredicate instanceof CompositeExpression && $currentPredicate->getType() === $type) {
return $currentPredicate->addMultiple($predicates); return $currentPredicate->with(...$predicates);
} }
if ($currentPredicate !== null) { if ($currentPredicate !== null) {
......
...@@ -23,25 +23,6 @@ class CompositeExpressionTest extends DbalTestCase ...@@ -23,25 +23,6 @@ class CompositeExpressionTest extends DbalTestCase
self::assertCount(2, $expr); self::assertCount(2, $expr);
} }
public function testAdd() : void
{
$expr = new CompositeExpression(CompositeExpression::TYPE_OR, ['u.group_id = 1']);
self::assertCount(1, $expr);
$expr->add(new CompositeExpression(CompositeExpression::TYPE_AND, []));
self::assertCount(1, $expr);
$expr->add(new CompositeExpression(CompositeExpression::TYPE_OR, ['u.user_id = 1']));
self::assertCount(2, $expr);
$expr->add('u.user_id = 1');
self::assertCount(3, $expr);
}
public function testWith() : void public function testWith() : void
{ {
$expr = new CompositeExpression(CompositeExpression::TYPE_OR, ['u.group_id = 1']); $expr = new CompositeExpression(CompositeExpression::TYPE_OR, ['u.group_id = 1']);
......
...@@ -50,7 +50,7 @@ class ExpressionBuilderTest extends DbalTestCase ...@@ -50,7 +50,7 @@ class ExpressionBuilderTest extends DbalTestCase
$composite = $this->expr->andX(); $composite = $this->expr->andX();
foreach ($parts as $part) { foreach ($parts as $part) {
$composite->add($part); $composite = $composite->with($part);
} }
self::assertEquals($expected, (string) $composite); self::assertEquals($expected, (string) $composite);
...@@ -123,7 +123,7 @@ class ExpressionBuilderTest extends DbalTestCase ...@@ -123,7 +123,7 @@ class ExpressionBuilderTest extends DbalTestCase
$composite = $this->expr->orX(); $composite = $this->expr->orX();
foreach ($parts as $part) { foreach ($parts as $part) {
$composite->add($part); $composite = $composite->with($part);
} }
self::assertEquals($expected, (string) $composite); self::assertEquals($expected, (string) $composite);
......
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