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

Prepare CompositeExpression for immutability #3850

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
5 changes: 5 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

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

## Deprecated `CompositeExpression` methods

The usage of the `add()` and `addMultiple()` methods of the `CompositeExpression` class has been deprecated. Use `with()` instead, which returns a new instance.
In the future, the `add*()` methods will be removed and the class will be effectively immutable.

# Upgrade to 2.10

## Deprecated `Doctrine\DBAL\Event\ConnectionEventArgs` methods
Expand Down
23 changes: 23 additions & 0 deletions lib/Doctrine/DBAL/Query/Expression/CompositeExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public function __construct($type, array $parts = [])
/**
* Adds multiple parts to composite expression.
*
* @deprecated This class will be made immutable. Use with() instead.
*
* @param self[]|string[] $parts
*
* @return \Doctrine\DBAL\Query\Expression\CompositeExpression
Expand All @@ -65,6 +67,8 @@ public function addMultiple(array $parts = [])
/**
* Adds an expression to composite expression.
*
* @deprecated This class will be made immutable. Use with() instead.
*
* @param mixed $part
*
* @return \Doctrine\DBAL\Query\Expression\CompositeExpression
Expand All @@ -84,6 +88,25 @@ public function add($part)
return $this;
}

/**
* Returns a new CompositeExpression with the given parts added.
*
* @param self|string $part
* @param self|string ...$parts
*/
public function with($part, ...$parts) : self
{
$that = clone $this;

$that->parts[] = $part;

foreach ($parts as $part) {
$that->parts[] = $part;
}

return $that;
}

/**
* Retrieves the amount of expressions on composite expression.
*
Expand Down
8 changes: 4 additions & 4 deletions lib/Doctrine/DBAL/Query/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,7 @@ public function andWhere($where)
$where = $this->getQueryPart('where');

if ($where instanceof CompositeExpression && $where->getType() === CompositeExpression::TYPE_AND) {
$where->addMultiple($args);
$where = $where->with(...$args);
} else {
array_unshift($args, $where);
$where = new CompositeExpression(CompositeExpression::TYPE_AND, $args);
Expand Down Expand Up @@ -856,7 +856,7 @@ public function orWhere($where)
$where = $this->getQueryPart('where');

if ($where instanceof CompositeExpression && $where->getType() === CompositeExpression::TYPE_OR) {
$where->addMultiple($args);
$where = $where->with(...$args);
} else {
array_unshift($args, $where);
$where = new CompositeExpression(CompositeExpression::TYPE_OR, $args);
Expand Down Expand Up @@ -998,7 +998,7 @@ public function andHaving($having)
$having = $this->getQueryPart('having');

if ($having instanceof CompositeExpression && $having->getType() === CompositeExpression::TYPE_AND) {
$having->addMultiple($args);
$having = $having->with(...$args);
} else {
array_unshift($args, $having);
$having = new CompositeExpression(CompositeExpression::TYPE_AND, $args);
Expand All @@ -1021,7 +1021,7 @@ public function orHaving($having)
$having = $this->getQueryPart('having');

if ($having instanceof CompositeExpression && $having->getType() === CompositeExpression::TYPE_OR) {
$having->addMultiple($args);
$having = $having->with(...$args);
} else {
array_unshift($args, $having);
$having = new CompositeExpression(CompositeExpression::TYPE_OR, $args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function testCount() : void

self::assertCount(1, $expr);

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

self::assertCount(2, $expr);
}
Expand Down Expand Up @@ -44,6 +44,26 @@ public function testAdd() : void
self::assertCount(3, $expr);
}

public function testWith() : void
{
$expr = new CompositeExpression(CompositeExpression::TYPE_OR, ['u.group_id = 1']);

self::assertCount(1, $expr);

// test immutability
$expr->with(new CompositeExpression(CompositeExpression::TYPE_OR, ['u.user_id = 1']));

self::assertCount(1, $expr);

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

self::assertCount(2, $expr);

$expr = $expr->with('u.user_id = 1');

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

/**
* @param string[]|CompositeExpression[] $parts
*
Expand Down