Skip to content

Commit 0525210

Browse files
committed
Add CompositeExpression::with(), deprecate add*()
1 parent ad4f12b commit 0525210

File tree

5 files changed

+47
-19
lines changed

5 files changed

+47
-19
lines changed

UPGRADE.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
The usage of the `andX()` and `orX()` methods of the `ExpressionBuilder` class has been deprecated. Use `and()` and `or()` instead.
66

7+
## Deprecated `CompositeExpression` methods
8+
9+
The usage of the `add()` and `addMultiple()` methods of the `CompositeExpression` class has been deprecated. Use `with()` instead, which returns a new instance.
10+
In the future, the `add*()` methods will be removed and the class will be effectively immutable.
11+
712
# Upgrade to 2.10
813

914
## Deprecated `Doctrine\DBAL\Event\ConnectionEventArgs` methods

lib/Doctrine/DBAL/Query/Expression/CompositeExpression.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ public function __construct($type, array $parts = [])
4949
/**
5050
* Adds multiple parts to composite expression.
5151
*
52+
* @deprecated This class will be made immutable. Use with() instead.
53+
*
5254
* @param self[]|string[] $parts
5355
*
5456
* @return \Doctrine\DBAL\Query\Expression\CompositeExpression
@@ -65,6 +67,8 @@ public function addMultiple(array $parts = [])
6567
/**
6668
* Adds an expression to composite expression.
6769
*
70+
* @deprecated This class will be made immutable. Use with() instead.
71+
*
6872
* @param mixed $part
6973
*
7074
* @return \Doctrine\DBAL\Query\Expression\CompositeExpression
@@ -84,6 +88,28 @@ public function add($part)
8488
return $this;
8589
}
8690

91+
/**
92+
* Returns a CompositeExpression with the given part added.
93+
*
94+
* This methods returns a new instance. The current instance is unaffected by this method call.
95+
*
96+
* @param self|string ...$parts
97+
*/
98+
public function with(...$parts) : self
99+
{
100+
$that = clone $this;
101+
102+
foreach ($parts as $part) {
103+
if ($part instanceof self && count($part) === 0) {
104+
continue;
105+
}
106+
107+
$that->parts[] = $part;
108+
}
109+
110+
return $that;
111+
}
112+
87113
/**
88114
* Retrieves the amount of expressions on composite expression.
89115
*

lib/Doctrine/DBAL/Query/QueryBuilder.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,7 @@ public function andWhere($where)
823823
$where = $this->getQueryPart('where');
824824

825825
if ($where instanceof CompositeExpression && $where->getType() === CompositeExpression::TYPE_AND) {
826-
$where->addMultiple($args);
826+
$where = $where->with(...$args);
827827
} else {
828828
array_unshift($args, $where);
829829
$where = new CompositeExpression(CompositeExpression::TYPE_AND, $args);
@@ -856,7 +856,7 @@ public function orWhere($where)
856856
$where = $this->getQueryPart('where');
857857

858858
if ($where instanceof CompositeExpression && $where->getType() === CompositeExpression::TYPE_OR) {
859-
$where->addMultiple($args);
859+
$where = $where->with(...$args);
860860
} else {
861861
array_unshift($args, $where);
862862
$where = new CompositeExpression(CompositeExpression::TYPE_OR, $args);
@@ -998,7 +998,7 @@ public function andHaving($having)
998998
$having = $this->getQueryPart('having');
999999

10001000
if ($having instanceof CompositeExpression && $having->getType() === CompositeExpression::TYPE_AND) {
1001-
$having->addMultiple($args);
1001+
$having = $having->with(...$args);
10021002
} else {
10031003
array_unshift($args, $having);
10041004
$having = new CompositeExpression(CompositeExpression::TYPE_AND, $args);
@@ -1021,7 +1021,7 @@ public function orHaving($having)
10211021
$having = $this->getQueryPart('having');
10221022

10231023
if ($having instanceof CompositeExpression && $having->getType() === CompositeExpression::TYPE_OR) {
1024-
$having->addMultiple($args);
1024+
$having = $having->with(...$args);
10251025
} else {
10261026
array_unshift($args, $having);
10271027
$having = new CompositeExpression(CompositeExpression::TYPE_OR, $args);

tests/Doctrine/Tests/DBAL/Query/Expression/CompositeExpressionTest.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,35 @@ public function testCount() : void
1616

1717
self::assertCount(1, $expr);
1818

19-
$expr->add('u.group_id = 2');
19+
$expr = $expr->with('u.group_id = 2');
2020

2121
self::assertCount(2, $expr);
2222
}
2323

24-
public function testAdd() : void
24+
public function testAddWith() : void
2525
{
2626
$expr = new CompositeExpression(CompositeExpression::TYPE_OR, ['u.group_id = 1']);
2727

2828
self::assertCount(1, $expr);
2929

30-
$expr->add(new CompositeExpression(CompositeExpression::TYPE_AND, []));
30+
$expr = $expr->with(new CompositeExpression(CompositeExpression::TYPE_AND, []));
3131

3232
self::assertCount(1, $expr);
3333

34-
$expr->add(new CompositeExpression(CompositeExpression::TYPE_OR, ['u.user_id = 1']));
34+
$expr = $expr->with(new CompositeExpression(CompositeExpression::TYPE_OR, ['u.user_id = 1']));
3535

3636
self::assertCount(2, $expr);
3737

3838
$expr->add(null);
3939

4040
self::assertCount(2, $expr);
4141

42-
$expr->add('u.user_id = 1');
42+
// test immutability
43+
$expr->with('u.user_id = 1');
44+
45+
self::assertCount(2, $expr);
46+
47+
$expr = $expr->with('u.user_id = 1');
4348

4449
self::assertCount(3, $expr);
4550
}

tests/Doctrine/Tests/DBAL/Query/Expression/ExpressionBuilderTest.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,7 @@ public function testAnd(array $parts, string $expected) : void
4545
*/
4646
public function testAndX(array $parts, string $expected) : void
4747
{
48-
$composite = $this->expr->andX();
49-
50-
foreach ($parts as $part) {
51-
$composite->add($part);
52-
}
48+
$composite = $this->expr->andX()->with(...$parts);
5349

5450
self::assertEquals($expected, (string) $composite);
5551
}
@@ -118,11 +114,7 @@ public function testOr(array $parts, string $expected) : void
118114
*/
119115
public function testOrX(array $parts, string $expected) : void
120116
{
121-
$composite = $this->expr->orX();
122-
123-
foreach ($parts as $part) {
124-
$composite->add($part);
125-
}
117+
$composite = $this->expr->orX()->with(...$parts);
126118

127119
self::assertEquals($expected, (string) $composite);
128120
}

0 commit comments

Comments
 (0)