Skip to content

Commit

Permalink
[doctrineGH-3844] Add deprecation message for CompositeExpression::ad…
Browse files Browse the repository at this point in the history
…d/addMultiple
  • Loading branch information
beberlei committed Mar 11, 2021
1 parent 07e4ec8 commit 5d20e3c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
41 changes: 34 additions & 7 deletions lib/Doctrine/DBAL/Query/Expression/CompositeExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Doctrine\DBAL\Query\Expression;

use Countable;
use Doctrine\Deprecations\Deprecation;

use function array_merge;
use function count;
Expand Down Expand Up @@ -43,11 +44,21 @@ class CompositeExpression implements Countable
* @param string $type Instance type of composite expression.
* @param self[]|string[] $parts Composition of expressions to be joined on composite expression.
*/
public function __construct($type, array $parts = [])
public function __construct($type, array $parts = [], bool $internalFlagCalledByFactory = false)
{
$this->type = $type;

$this->addMultiple($parts);
$this->addMultiple($parts, $internalFlagCalledByFactory);

if ($internalFlagCalledByFactory) {
return;
}

Deprecation::trigger(
'doctrine/dbal',
'https://github.com/doctrine/dbal/pull/3864',
'Do not use CompositeExpression constructor directly, use static and() and or() factory methods.'
);
}

/**
Expand All @@ -56,7 +67,7 @@ public function __construct($type, array $parts = [])
*/
public static function and($part, ...$parts): self
{
return new self(self::TYPE_AND, array_merge([$part], $parts));
return new self(self::TYPE_AND, array_merge([$part], $parts), true);
}

/**
Expand All @@ -65,7 +76,7 @@ public static function and($part, ...$parts): self
*/
public static function or($part, ...$parts): self
{
return new self(self::TYPE_OR, array_merge([$part], $parts));
return new self(self::TYPE_OR, array_merge([$part], $parts), true);
}

/**
Expand All @@ -77,10 +88,18 @@ public static function or($part, ...$parts): self
*
* @return CompositeExpression
*/
public function addMultiple(array $parts = [])
public function addMultiple(array $parts = [], bool $internalFlagCalledByConstructor = false)
{
if ($internalFlagCalledByConstructor === false) {
Deprecation::trigger(
'doctrine/dbal',
'https://github.com/doctrine/dbal/issues/3844',
'CompositeExpression::addMultiple() is deprecated, use CompositeExpression::with() instead.'
);
}

foreach ($parts as $part) {
$this->add($part);
$this->add($part, true);
}

return $this;
Expand All @@ -95,8 +114,16 @@ public function addMultiple(array $parts = [])
*
* @return CompositeExpression
*/
public function add($part)
public function add($part, bool $internalFlagCalledByAddMultiple = false)
{
if ($internalFlagCalledByAddMultiple === false) {
Deprecation::trigger(
'doctrine/dbal',
'https://github.com/doctrine/dbal/issues/3844',
'CompositeExpression::add() is deprecated, use CompositeExpression::with() instead.'
);
}

if (empty($part)) {
return $this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
namespace Doctrine\Tests\DBAL\Query\Expression;

use Doctrine\DBAL\Query\Expression\CompositeExpression;
use Doctrine\Deprecations\PHPUnit\VerifyDeprecations;
use Doctrine\Tests\DbalTestCase;

class CompositeExpressionTest extends DbalTestCase
{
use VerifyDeprecations;

public function testCount(): void
{
$expr = CompositeExpression::or('u.group_id = 1');
Expand All @@ -20,6 +23,9 @@ public function testCount(): void

public function testAdd(): void
{
$this->expectDeprecationWithIdentifier('https://github.com/doctrine/dbal/pull/3864');
$this->expectDeprecationWithIdentifier('https://github.com/doctrine/dbal/issues/3844');

$expr = CompositeExpression::or('u.group_id = 1');

self::assertCount(1, $expr);
Expand Down Expand Up @@ -68,6 +74,8 @@ public function testWith(): void
*/
public function testCompositeUsageAndGeneration(string $type, array $parts, string $expects): void
{
$this->expectDeprecationWithIdentifier('https://github.com/doctrine/dbal/pull/3864');

$expr = new CompositeExpression($type, $parts);

self::assertEquals($expects, (string) $expr);
Expand Down

0 comments on commit 5d20e3c

Please sign in to comment.