Skip to content

Commit

Permalink
[doctrineGH-3864] Trigger deprecation calling CompositeExpression cto…
Browse files Browse the repository at this point in the history
…r directly.
  • Loading branch information
beberlei committed Mar 11, 2021
1 parent a85cef0 commit 9107c89
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
16 changes: 13 additions & 3 deletions lib/Doctrine/DBAL/Query/Expression/CompositeExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,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);

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 @@ -57,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 @@ -66,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 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,8 @@ public function testCount(): void

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

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

self::assertCount(1, $expr);
Expand Down Expand Up @@ -68,6 +73,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 9107c89

Please sign in to comment.