Skip to content

Commit 27c33cf

Browse files
committed
Merge branch '2.20.x' into 3.6.x
* 2.20.x: Make the data provider static Raise proper exception for invalid arguments in Base::add() (#12394)
2 parents 331f8b5 + 6068b61 commit 27c33cf

File tree

6 files changed

+32
-24
lines changed

6 files changed

+32
-24
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2580,30 +2580,12 @@ parameters:
25802580
count: 1
25812581
path: src/Query/Exec/SingleTableDeleteUpdateExecutor.php
25822582

2583-
-
2584-
message: '#^PHPDoc type array\<string\> of property Doctrine\\ORM\\Query\\Expr\\Andx\:\:\$allowedClasses is not covariant with PHPDoc type list\<class\-string\> of overridden property Doctrine\\ORM\\Query\\Expr\\Base\:\:\$allowedClasses\.$#'
2585-
identifier: property.phpDocType
2586-
count: 1
2587-
path: src/Query/Expr/Andx.php
2588-
25892583
-
25902584
message: '#^Method Doctrine\\ORM\\Query\\Expr\\Func\:\:getArguments\(\) should return list\<mixed\> but returns array\<mixed\>\.$#'
25912585
identifier: return.type
25922586
count: 1
25932587
path: src/Query/Expr/Func.php
25942588

2595-
-
2596-
message: '#^PHPDoc type array\<string\> of property Doctrine\\ORM\\Query\\Expr\\Orx\:\:\$allowedClasses is not covariant with PHPDoc type list\<class\-string\> of overridden property Doctrine\\ORM\\Query\\Expr\\Base\:\:\$allowedClasses\.$#'
2597-
identifier: property.phpDocType
2598-
count: 1
2599-
path: src/Query/Expr/Orx.php
2600-
2601-
-
2602-
message: '#^PHPDoc type array\<string\> of property Doctrine\\ORM\\Query\\Expr\\Select\:\:\$allowedClasses is not covariant with PHPDoc type list\<class\-string\> of overridden property Doctrine\\ORM\\Query\\Expr\\Base\:\:\$allowedClasses\.$#'
2603-
identifier: property.phpDocType
2604-
count: 1
2605-
path: src/Query/Expr/Select.php
2606-
26072589
-
26082590
message: '#^Method Doctrine\\ORM\\Query\\ParameterTypeInferer\:\:inferType\(\) never returns int so it can be removed from the return type\.$#'
26092591
identifier: return.unusedType

src/Query/Expr/Andx.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Doctrine\ORM\Query\Expr;
66

7+
use Stringable;
8+
79
/**
810
* Expression class for building DQL and parts.
911
*
@@ -13,7 +15,7 @@ class Andx extends Composite
1315
{
1416
protected string $separator = ' AND ';
1517

16-
/** @var string[] */
18+
/** @var list<class-string<Stringable>> */
1719
protected array $allowedClasses = [
1820
Comparison::class,
1921
Func::class,

src/Query/Expr/Base.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use function implode;
1414
use function in_array;
1515
use function is_array;
16+
use function is_object;
1617
use function is_string;
1718
use function sprintf;
1819

@@ -27,7 +28,7 @@ abstract class Base implements Stringable
2728
protected string $separator = ', ';
2829
protected string $postSeparator = ')';
2930

30-
/** @var list<class-string> */
31+
/** @var list<class-string<Stringable>> */
3132
protected array $allowedClasses = [];
3233

3334
/** @var list<string|Stringable> */
@@ -58,6 +59,8 @@ public function addMultiple(array|string|object $args = []): static
5859
}
5960

6061
/**
62+
* @param string|Stringable|null $arg
63+
*
6164
* @return $this
6265
*
6366
* @throws InvalidArgumentException
@@ -66,7 +69,8 @@ public function add(mixed $arg): static
6669
{
6770
if ($arg !== null && (! $arg instanceof self || $arg->count() > 0)) {
6871
// If we decide to keep Expr\Base instances, we can use this check
69-
if (! is_string($arg) && ! in_array($arg::class, $this->allowedClasses, true)) {
72+
// @phpstan-ignore function.alreadyNarrowedType (input validation)
73+
if (! is_string($arg) && ! (is_object($arg) && in_array($arg::class, $this->allowedClasses, true))) {
7074
throw new InvalidArgumentException(sprintf(
7175
"Expression of type '%s' not allowed in this context.",
7276
get_debug_type($arg),

src/Query/Expr/Orx.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Doctrine\ORM\Query\Expr;
66

7+
use Stringable;
8+
79
/**
810
* Expression class for building DQL OR clauses.
911
*
@@ -13,7 +15,7 @@ class Orx extends Composite
1315
{
1416
protected string $separator = ' OR ';
1517

16-
/** @var string[] */
18+
/** @var list<class-string<Stringable>> */
1719
protected array $allowedClasses = [
1820
Comparison::class,
1921
Func::class,

src/Query/Expr/Select.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Doctrine\ORM\Query\Expr;
66

7+
use Stringable;
8+
79
/**
810
* Expression class for building DQL select statements.
911
*
@@ -14,7 +16,7 @@ class Select extends Base
1416
protected string $preSeparator = '';
1517
protected string $postSeparator = '';
1618

17-
/** @var string[] */
19+
/** @var list<class-string<Stringable>> */
1820
protected array $allowedClasses = [Func::class];
1921

2022
/** @phpstan-var list<string|Func> */

tests/Tests/ORM/Query/ExprTest.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,11 +374,27 @@ public function testOrderByAsc(): void
374374

375375
public function testAddThrowsException(): void
376376
{
377-
$this->expectException(InvalidArgumentException::class);
378377
$orExpr = $this->expr->orX();
378+
$this->expectException(InvalidArgumentException::class);
379379
$orExpr->add($this->expr->quot(5, 2));
380380
}
381381

382+
#[DataProvider('provideInvalidTypesForAdd')]
383+
public function testAddThrowsExceptionOnInvalidType(mixed $arg): void
384+
{
385+
$orExpr = $this->expr->orX();
386+
$this->expectException(InvalidArgumentException::class);
387+
$orExpr->add($arg);
388+
}
389+
390+
/** @return Generator<string, array{mixed}> */
391+
public static function provideInvalidTypesForAdd(): Generator
392+
{
393+
yield 'integer 1' => [1];
394+
yield 'object' => [(object) ['foo' => 'bar']];
395+
yield 'array' => [['foo' => 'bar']];
396+
}
397+
382398
#[Group('DDC-1683')]
383399
public function testBooleanLiteral(): void
384400
{

0 commit comments

Comments
 (0)