Skip to content

Commit 20324a4

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

File tree

4 files changed

+53
-5
lines changed

4 files changed

+53
-5
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: 23 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,25 @@ public function add($part)
8488
return $this;
8589
}
8690

91+
/**
92+
* Returns a new CompositeExpression with the given parts added.
93+
*
94+
* @param self|string $part
95+
* @param self|string ...$parts
96+
*/
97+
public function with($part, ...$parts) : self
98+
{
99+
$that = clone $this;
100+
101+
$that->parts[] = $part;
102+
103+
foreach ($parts as $part) {
104+
$that->parts[] = $part;
105+
}
106+
107+
return $that;
108+
}
109+
87110
/**
88111
* Retrieves the amount of expressions on composite expression.
89112
*

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: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ 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
}
@@ -44,6 +44,26 @@ public function testAdd() : void
4444
self::assertCount(3, $expr);
4545
}
4646

47+
public function testWith() : void
48+
{
49+
$expr = new CompositeExpression(CompositeExpression::TYPE_OR, ['u.group_id = 1']);
50+
51+
self::assertCount(1, $expr);
52+
53+
// test immutability
54+
$expr->with(new CompositeExpression(CompositeExpression::TYPE_OR, ['u.user_id = 1']));
55+
56+
self::assertCount(1, $expr);
57+
58+
$expr = $expr->with(new CompositeExpression(CompositeExpression::TYPE_OR, ['u.user_id = 1']));
59+
60+
self::assertCount(2, $expr);
61+
62+
$expr = $expr->with('u.user_id = 1');
63+
64+
self::assertCount(3, $expr);
65+
}
66+
4767
/**
4868
* @param string[]|CompositeExpression[] $parts
4969
*

0 commit comments

Comments
 (0)