From d6a3057f73c3ca5a181a08d427677667a7b8047c Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Sun, 7 Mar 2021 00:17:01 +0100 Subject: [PATCH] [GH-3864] Trigger deprecation calling CompositeExpression ctor directly. --- .../Query/Expression/CompositeExpression.php | 16 +++++++++++++--- .../Query/Expression/CompositeExpressionTest.php | 7 +++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/Doctrine/DBAL/Query/Expression/CompositeExpression.php b/lib/Doctrine/DBAL/Query/Expression/CompositeExpression.php index 70721b15576..150387d433b 100644 --- a/lib/Doctrine/DBAL/Query/Expression/CompositeExpression.php +++ b/lib/Doctrine/DBAL/Query/Expression/CompositeExpression.php @@ -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.' + ); } /** @@ -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); } /** @@ -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); } /** diff --git a/tests/Doctrine/Tests/DBAL/Query/Expression/CompositeExpressionTest.php b/tests/Doctrine/Tests/DBAL/Query/Expression/CompositeExpressionTest.php index c1daefa4d35..eccd555351c 100644 --- a/tests/Doctrine/Tests/DBAL/Query/Expression/CompositeExpressionTest.php +++ b/tests/Doctrine/Tests/DBAL/Query/Expression/CompositeExpressionTest.php @@ -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'); @@ -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); @@ -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);