Skip to content

Commit 791667a

Browse files
authored
Merge pull request #11317 from doctrine/2.18.x
Merge 2.18.x up into 2.19.x
2 parents abd9186 + feb27f0 commit 791667a

File tree

16 files changed

+157
-64
lines changed

16 files changed

+157
-64
lines changed

phpcs.xml.dist

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,4 +279,9 @@
279279
<!-- https://github.com/doctrine/orm/issues/8537 -->
280280
<exclude-pattern>src/QueryBuilder.php</exclude-pattern>
281281
</rule>
282+
283+
<rule ref="SlevomatCodingStandard.PHP.UselessParentheses">
284+
<!-- We need those parentheses to make enum access seem like valid syntax on PHP 7 -->
285+
<exclude-pattern>src/Mapping/Driver/XmlDriver.php</exclude-pattern>
286+
</rule>
282287
</ruleset>

phpstan-baseline.neon

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -325,21 +325,6 @@ parameters:
325325
count: 1
326326
path: src/Query/AST/Functions/DateSubFunction.php
327327

328-
-
329-
message: "#^Parameter \\#1 \\$simpleArithmeticExpr of method Doctrine\\\\ORM\\\\Query\\\\SqlWalker\\:\\:walkSimpleArithmeticExpression\\(\\) expects Doctrine\\\\ORM\\\\Query\\\\AST\\\\SimpleArithmeticExpression, Doctrine\\\\ORM\\\\Query\\\\AST\\\\Node given\\.$#"
330-
count: 1
331-
path: src/Query/AST/Functions/LengthFunction.php
332-
333-
-
334-
message: "#^Parameter \\#1 \\$simpleArithmeticExpr of method Doctrine\\\\ORM\\\\Query\\\\SqlWalker\\:\\:walkSimpleArithmeticExpression\\(\\) expects Doctrine\\\\ORM\\\\Query\\\\AST\\\\SimpleArithmeticExpression, Doctrine\\\\ORM\\\\Query\\\\AST\\\\Node given\\.$#"
335-
count: 1
336-
path: src/Query/AST/Functions/LowerFunction.php
337-
338-
-
339-
message: "#^Parameter \\#1 \\$simpleArithmeticExpr of method Doctrine\\\\ORM\\\\Query\\\\SqlWalker\\:\\:walkSimpleArithmeticExpression\\(\\) expects Doctrine\\\\ORM\\\\Query\\\\AST\\\\SimpleArithmeticExpression, Doctrine\\\\ORM\\\\Query\\\\AST\\\\Node given\\.$#"
340-
count: 1
341-
path: src/Query/AST/Functions/UpperFunction.php
342-
343328
-
344329
message: "#^Method Doctrine\\\\ORM\\\\Query\\\\AST\\\\IndexBy\\:\\:dispatch\\(\\) should return string but returns void\\.$#"
345330
count: 1

phpstan-persistence2.neon

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,31 @@ parameters:
3131
message: '/^Instanceof between Doctrine\\DBAL\\Platforms\\AbstractPlatform and Doctrine\\DBAL\\Platforms\\MySQLPlatform will always evaluate to false\.$/'
3232
path: src/Utility/LockSqlHelper.php
3333

34+
# Forward compatibility with Collections 3
35+
-
36+
message: '#^Parameter \$order of anonymous function has invalid type Doctrine\\Common\\Collections\\Order\.$#'
37+
path: src/Internal/CriteriaOrderings.php
38+
39+
-
40+
message: '#^Anonymous function has invalid return type Doctrine\\Common\\Collections\\Order\.$#'
41+
path: src/Internal/CriteriaOrderings.php
42+
43+
-
44+
message: '#^Access to property \$value on an unknown class Doctrine\\Common\\Collections\\Order\.$#'
45+
path: src/Internal/CriteriaOrderings.php
46+
47+
-
48+
message: '#^Call to static method from\(\) on an unknown class Doctrine\\Common\\Collections\\Order\.$#'
49+
path: src/Internal/CriteriaOrderings.php
50+
51+
-
52+
message: '#^Call to an undefined method Doctrine\\Common\\Collections\\Criteria\:\:orderings\(\)\.$#'
53+
path: src/Internal/CriteriaOrderings.php
54+
55+
-
56+
message: '#^Method .+\:\:mapToOrderEnumIfAvailable\(\) has invalid return type Doctrine\\Common\\Collections\\Order\.$#'
57+
path: src/Internal/CriteriaOrderings.php
58+
3459
# False positive
3560
-
3661
message: '/^Call to an undefined method Doctrine\\Common\\Cache\\Cache::deleteAll\(\)\.$/'

psalm-baseline.xml

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1572,11 +1572,6 @@
15721572
<code><![CDATA[$assoc['joinColumns']]]></code>
15731573
</PossiblyUndefinedArrayOffset>
15741574
</file>
1575-
<file src="src/Query/AST/Functions/LengthFunction.php">
1576-
<ArgumentTypeCoercion>
1577-
<code><![CDATA[$this->stringPrimary]]></code>
1578-
</ArgumentTypeCoercion>
1579-
</file>
15801575
<file src="src/Query/AST/Functions/LocateFunction.php">
15811576
<PossiblyInvalidArgument>
15821577
<code><![CDATA[$this->simpleArithmeticExpression]]></code>
@@ -1585,11 +1580,6 @@
15851580
<code><![CDATA[$parser->SimpleArithmeticExpression()]]></code>
15861581
</PossiblyInvalidPropertyAssignmentValue>
15871582
</file>
1588-
<file src="src/Query/AST/Functions/LowerFunction.php">
1589-
<ArgumentTypeCoercion>
1590-
<code><![CDATA[$this->stringPrimary]]></code>
1591-
</ArgumentTypeCoercion>
1592-
</file>
15931583
<file src="src/Query/AST/Functions/ModFunction.php">
15941584
<PossiblyInvalidPropertyAssignmentValue>
15951585
<code><![CDATA[$parser->SimpleArithmeticExpression()]]></code>
@@ -1617,11 +1607,6 @@
16171607
<code><![CDATA[$parser->SimpleArithmeticExpression()]]></code>
16181608
</PossiblyInvalidPropertyAssignmentValue>
16191609
</file>
1620-
<file src="src/Query/AST/Functions/UpperFunction.php">
1621-
<ArgumentTypeCoercion>
1622-
<code><![CDATA[$this->stringPrimary]]></code>
1623-
</ArgumentTypeCoercion>
1624-
</file>
16251610
<file src="src/Query/AST/GeneralCaseExpression.php">
16261611
<ParamNameMismatch>
16271612
<code>$sqlWalker</code>
@@ -2126,9 +2111,6 @@
21262111
<NoValue>
21272112
<code>$expression</code>
21282113
</NoValue>
2129-
<PossiblyInvalidArgument>
2130-
<code><![CDATA[$aggExpression->pathExpression]]></code>
2131-
</PossiblyInvalidArgument>
21322114
<PossiblyNullArgument>
21332115
<code><![CDATA[$AST->whereClause]]></code>
21342116
<code><![CDATA[$AST->whereClause]]></code>

src/Cache/Persister/Entity/AbstractEntityPersister.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Doctrine\ORM\Cache\TimestampCacheKey;
1717
use Doctrine\ORM\Cache\TimestampRegion;
1818
use Doctrine\ORM\EntityManagerInterface;
19+
use Doctrine\ORM\Internal\CriteriaOrderings;
1920
use Doctrine\ORM\Mapping\ClassMetadata;
2021
use Doctrine\ORM\Mapping\ClassMetadataFactory;
2122
use Doctrine\ORM\PersistentCollection;
@@ -30,6 +31,8 @@
3031

3132
abstract class AbstractEntityPersister implements CachedEntityPersister
3233
{
34+
use CriteriaOrderings;
35+
3336
/** @var UnitOfWork */
3437
protected $uow;
3538

@@ -475,7 +478,7 @@ public function count($criteria = [])
475478
*/
476479
public function loadCriteria(Criteria $criteria)
477480
{
478-
$orderBy = $criteria->getOrderings();
481+
$orderBy = self::getCriteriaOrderings($criteria);
479482
$limit = $criteria->getMaxResults();
480483
$offset = $criteria->getFirstResult();
481484
$query = $this->persister->getSelectSQL($criteria);

src/Internal/CriteriaOrderings.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\ORM\Internal;
6+
7+
use Doctrine\Common\Collections\Criteria;
8+
use Doctrine\Common\Collections\Order;
9+
10+
use function array_map;
11+
use function class_exists;
12+
use function method_exists;
13+
use function strtoupper;
14+
15+
trait CriteriaOrderings
16+
{
17+
/**
18+
* @return array<string, string>
19+
*
20+
* @psalm-suppress DeprecatedMethod We need to call the deprecated API if the new one does not exist yet.
21+
*/
22+
private static function getCriteriaOrderings(Criteria $criteria): array
23+
{
24+
if (! method_exists(Criteria::class, 'orderings')) {
25+
return $criteria->getOrderings();
26+
}
27+
28+
return array_map(
29+
static function (Order $order): string {
30+
return $order->value;
31+
},
32+
$criteria->orderings()
33+
);
34+
}
35+
36+
/**
37+
* @param array<string, string> $orderings
38+
*
39+
* @return array<string, string>|array<string, Order>
40+
*/
41+
private static function mapToOrderEnumIfAvailable(array $orderings): array
42+
{
43+
if (! class_exists(Order::class)) {
44+
return $orderings;
45+
}
46+
47+
return array_map(
48+
static function (string $order): Order {
49+
return Order::from(strtoupper($order));
50+
},
51+
$orderings
52+
);
53+
}
54+
}

src/Mapping/Driver/XmlDriver.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Doctrine\ORM\Mapping\Driver;
66

77
use Doctrine\Common\Collections\Criteria;
8+
use Doctrine\Common\Collections\Order;
89
use Doctrine\ORM\Mapping\Builder\EntityListenerBuilder;
910
use Doctrine\ORM\Mapping\ClassMetadata;
1011
use Doctrine\ORM\Mapping\MappingException;
@@ -16,6 +17,7 @@
1617
use SimpleXMLElement;
1718

1819
use function assert;
20+
use function class_exists;
1921
use function constant;
2022
use function count;
2123
use function defined;
@@ -481,9 +483,10 @@ public function loadMetadataForClass($className, PersistenceClassMetadata $metad
481483
if (isset($oneToManyElement->{'order-by'})) {
482484
$orderBy = [];
483485
foreach ($oneToManyElement->{'order-by'}->{'order-by-field'} ?? [] as $orderByField) {
486+
/** @psalm-suppress DeprecatedConstant */
484487
$orderBy[(string) $orderByField['name']] = isset($orderByField['direction'])
485488
? (string) $orderByField['direction']
486-
: Criteria::ASC;
489+
: (class_exists(Order::class) ? (Order::Ascending)->value : Criteria::ASC);
487490
}
488491

489492
$mapping['orderBy'] = $orderBy;
@@ -609,9 +612,10 @@ public function loadMetadataForClass($className, PersistenceClassMetadata $metad
609612
if (isset($manyToManyElement->{'order-by'})) {
610613
$orderBy = [];
611614
foreach ($manyToManyElement->{'order-by'}->{'order-by-field'} ?? [] as $orderByField) {
615+
/** @psalm-suppress DeprecatedConstant */
612616
$orderBy[(string) $orderByField['name']] = isset($orderByField['direction'])
613617
? (string) $orderByField['direction']
614-
: Criteria::ASC;
618+
: (class_exists(Order::class) ? (Order::Ascending)->value : Criteria::ASC);
615619
}
616620

617621
$mapping['orderBy'] = $orderBy;

src/PersistentCollection.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Doctrine\Common\Collections\Collection;
1010
use Doctrine\Common\Collections\Criteria;
1111
use Doctrine\Common\Collections\Selectable;
12+
use Doctrine\ORM\Internal\CriteriaOrderings;
1213
use Doctrine\ORM\Mapping\ClassMetadata;
1314
use ReturnTypeWillChange;
1415
use RuntimeException;
@@ -41,6 +42,8 @@
4142
*/
4243
final class PersistentCollection extends AbstractLazyCollection implements Selectable
4344
{
45+
use CriteriaOrderings;
46+
4447
/**
4548
* A snapshot of the collection at the moment it was fetched from the database.
4649
* This is used to create a diff of the collection at commit time.
@@ -671,7 +674,9 @@ public function matching(Criteria $criteria): Collection
671674

672675
$criteria = clone $criteria;
673676
$criteria->where($expression);
674-
$criteria->orderBy($criteria->getOrderings() ?: $association['orderBy'] ?? []);
677+
$criteria->orderBy(self::mapToOrderEnumIfAvailable(
678+
self::getCriteriaOrderings($criteria) ?: $association['orderBy'] ?? []
679+
));
675680

676681
$persister = $this->getUnitOfWork()->getEntityPersister($association['targetEntity']);
677682

src/Persisters/Collection/ManyToManyPersister.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Doctrine\Common\Collections\Criteria;
99
use Doctrine\Common\Collections\Expr\Comparison;
1010
use Doctrine\DBAL\Exception as DBALException;
11+
use Doctrine\ORM\Internal\CriteriaOrderings;
1112
use Doctrine\ORM\Mapping\ClassMetadata;
1213
use Doctrine\ORM\PersistentCollection;
1314
use Doctrine\ORM\Persisters\SqlValueVisitor;
@@ -30,6 +31,8 @@
3031
*/
3132
class ManyToManyPersister extends AbstractCollectionPersister
3233
{
34+
use CriteriaOrderings;
35+
3336
/**
3437
* {@inheritDoc}
3538
*/
@@ -745,7 +748,7 @@ private function expandCriteriaParameters(Criteria $criteria): array
745748

746749
private function getOrderingSql(Criteria $criteria, ClassMetadata $targetClass): string
747750
{
748-
$orderings = $criteria->getOrderings();
751+
$orderings = self::getCriteriaOrderings($criteria);
749752
if ($orderings) {
750753
$orderBy = [];
751754
foreach ($orderings as $name => $direction) {

src/Persisters/Entity/BasicEntityPersister.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Doctrine\DBAL\Types\Types;
1616
use Doctrine\Deprecations\Deprecation;
1717
use Doctrine\ORM\EntityManagerInterface;
18+
use Doctrine\ORM\Internal\CriteriaOrderings;
1819
use Doctrine\ORM\Mapping\ClassMetadata;
1920
use Doctrine\ORM\Mapping\MappingException;
2021
use Doctrine\ORM\Mapping\QuoteStrategy;
@@ -93,6 +94,7 @@
9394
*/
9495
class BasicEntityPersister implements EntityPersister
9596
{
97+
use CriteriaOrderings;
9698
use LockSqlHelper;
9799

98100
/** @var array<string,string> */
@@ -884,7 +886,7 @@ public function count($criteria = [])
884886
*/
885887
public function loadCriteria(Criteria $criteria)
886888
{
887-
$orderBy = $criteria->getOrderings();
889+
$orderBy = self::getCriteriaOrderings($criteria);
888890
$limit = $criteria->getMaxResults();
889891
$offset = $criteria->getFirstResult();
890892
$query = $this->getSelectSQL($criteria, null, null, $limit, $offset, $orderBy);

0 commit comments

Comments
 (0)