From 31651e6b68c242c8be4d84ec2a528f6ac53a04fb Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Fri, 1 Mar 2024 09:12:54 +0100 Subject: [PATCH] Bump Doctrine Collections to 2.2 --- composer.json | 2 +- .../Entity/AbstractEntityPersister.php | 10 ++-- src/Internal/CriteriaOrderings.php | 50 ------------------- src/PersistentCollection.php | 14 +++--- .../Collection/ManyToManyPersister.php | 7 +-- .../Entity/BasicEntityPersister.php | 8 +-- src/QueryBuilder.php | 6 +-- 7 files changed, 22 insertions(+), 75 deletions(-) delete mode 100644 src/Internal/CriteriaOrderings.php diff --git a/composer.json b/composer.json index c65c03b959d..59950ba165c 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,7 @@ "php": "^8.1", "composer-runtime-api": "^2", "ext-ctype": "*", - "doctrine/collections": "^2.1", + "doctrine/collections": "^2.2", "doctrine/dbal": "^3.8.2 || ^4", "doctrine/deprecations": "^0.5.3 || ^1", "doctrine/event-manager": "^1.2 || ^2", diff --git a/src/Cache/Persister/Entity/AbstractEntityPersister.php b/src/Cache/Persister/Entity/AbstractEntityPersister.php index fddc3d26303..9f371d8b205 100644 --- a/src/Cache/Persister/Entity/AbstractEntityPersister.php +++ b/src/Cache/Persister/Entity/AbstractEntityPersister.php @@ -5,6 +5,7 @@ namespace Doctrine\ORM\Cache\Persister\Entity; use Doctrine\Common\Collections\Criteria; +use Doctrine\Common\Collections\Order; use Doctrine\DBAL\LockMode; use Doctrine\ORM\Cache; use Doctrine\ORM\Cache\CollectionCacheKey; @@ -17,7 +18,6 @@ use Doctrine\ORM\Cache\TimestampCacheKey; use Doctrine\ORM\Cache\TimestampRegion; use Doctrine\ORM\EntityManagerInterface; -use Doctrine\ORM\Internal\CriteriaOrderings; use Doctrine\ORM\Mapping\AssociationMapping; use Doctrine\ORM\Mapping\ClassMetadata; use Doctrine\ORM\Mapping\ClassMetadataFactory; @@ -34,8 +34,6 @@ abstract class AbstractEntityPersister implements CachedEntityPersister { - use CriteriaOrderings; - protected UnitOfWork $uow; protected ClassMetadataFactory $metadataFactory; @@ -204,8 +202,8 @@ private function storeJoinedAssociations(object $entity): void /** * Generates a string of currently query * - * @param string[]|Criteria $criteria - * @param string[]|null $orderBy + * @param string[]|Criteria $criteria + * @param array|null $orderBy */ protected function getHash( string $query, @@ -429,7 +427,7 @@ public function count(array|Criteria $criteria = []): int */ public function loadCriteria(Criteria $criteria): array { - $orderBy = self::getCriteriaOrderings($criteria); + $orderBy = $criteria->orderings(); $limit = $criteria->getMaxResults(); $offset = $criteria->getFirstResult(); $query = $this->persister->getSelectSQL($criteria); diff --git a/src/Internal/CriteriaOrderings.php b/src/Internal/CriteriaOrderings.php deleted file mode 100644 index 88ab2a35a43..00000000000 --- a/src/Internal/CriteriaOrderings.php +++ /dev/null @@ -1,50 +0,0 @@ - - * - * @psalm-suppress DeprecatedMethod We need to call the deprecated API if the new one does not exist yet. - */ - private static function getCriteriaOrderings(Criteria $criteria): array - { - if (! method_exists(Criteria::class, 'orderings')) { - return $criteria->getOrderings(); - } - - return array_map( - static fn (Order $order): string => $order->value, - $criteria->orderings(), - ); - } - - /** - * @param array $orderings - * - * @return array|array - */ - private static function mapToOrderEnumIfAvailable(array $orderings): array - { - if (! enum_exists(Order::class)) { - return $orderings; - } - - return array_map( - static fn (string $order): Order => Order::from(strtoupper($order)), - $orderings, - ); - } -} diff --git a/src/PersistentCollection.php b/src/PersistentCollection.php index 3dcaee0ecf9..24cdef0a5de 100644 --- a/src/PersistentCollection.php +++ b/src/PersistentCollection.php @@ -8,8 +8,8 @@ use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\Criteria; +use Doctrine\Common\Collections\Order; use Doctrine\Common\Collections\Selectable; -use Doctrine\ORM\Internal\CriteriaOrderings; use Doctrine\ORM\Mapping\AssociationMapping; use Doctrine\ORM\Mapping\ClassMetadata; use Doctrine\ORM\Mapping\ToManyAssociationMapping; @@ -24,6 +24,7 @@ use function assert; use function is_object; use function spl_object_id; +use function strtoupper; /** * A PersistentCollection represents a collection of elements that have persistent state. @@ -41,8 +42,6 @@ */ final class PersistentCollection extends AbstractLazyCollection implements Selectable { - use CriteriaOrderings; - /** * A snapshot of the collection at the moment it was fetched from the database. * This is used to create a diff of the collection at commit time. @@ -588,9 +587,12 @@ public function matching(Criteria $criteria): Collection $criteria = clone $criteria; $criteria->where($expression); - $criteria->orderBy(self::mapToOrderEnumIfAvailable( - self::getCriteriaOrderings($criteria) ?: $association->orderBy(), - )); + $criteria->orderBy( + $criteria->orderings() ?: array_map( + static fn (string $order): Order => Order::from(strtoupper($order)), + $association->orderBy(), + ), + ); $persister = $this->getUnitOfWork()->getEntityPersister($association->targetEntity); diff --git a/src/Persisters/Collection/ManyToManyPersister.php b/src/Persisters/Collection/ManyToManyPersister.php index 4843271e107..7cf993d5997 100644 --- a/src/Persisters/Collection/ManyToManyPersister.php +++ b/src/Persisters/Collection/ManyToManyPersister.php @@ -9,7 +9,6 @@ use Doctrine\Common\Collections\Expr\Comparison; use Doctrine\DBAL\Exception as DBALException; use Doctrine\DBAL\LockMode; -use Doctrine\ORM\Internal\CriteriaOrderings; use Doctrine\ORM\Mapping\AssociationMapping; use Doctrine\ORM\Mapping\ClassMetadata; use Doctrine\ORM\Mapping\InverseSideMapping; @@ -33,8 +32,6 @@ */ class ManyToManyPersister extends AbstractCollectionPersister { - use CriteriaOrderings; - public function delete(PersistentCollection $collection): void { $mapping = $this->getMapping($collection); @@ -735,7 +732,7 @@ private function expandCriteriaParameters(Criteria $criteria): array private function getOrderingSql(Criteria $criteria, ClassMetadata $targetClass): string { - $orderings = self::getCriteriaOrderings($criteria); + $orderings = $criteria->orderings(); if ($orderings) { $orderBy = []; foreach ($orderings as $name => $direction) { @@ -744,7 +741,7 @@ private function getOrderingSql(Criteria $criteria, ClassMetadata $targetClass): $targetClass, $this->platform, ); - $orderBy[] = $field . ' ' . $direction; + $orderBy[] = $field . ' ' . $direction->value; } return ' ORDER BY ' . implode(', ', $orderBy); diff --git a/src/Persisters/Entity/BasicEntityPersister.php b/src/Persisters/Entity/BasicEntityPersister.php index fc16c669e9f..377e03ce274 100644 --- a/src/Persisters/Entity/BasicEntityPersister.php +++ b/src/Persisters/Entity/BasicEntityPersister.php @@ -7,6 +7,7 @@ use BackedEnum; use Doctrine\Common\Collections\Criteria; use Doctrine\Common\Collections\Expr\Comparison; +use Doctrine\Common\Collections\Order; use Doctrine\DBAL\ArrayParameterType; use Doctrine\DBAL\Connection; use Doctrine\DBAL\LockMode; @@ -16,7 +17,6 @@ use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Types; use Doctrine\ORM\EntityManagerInterface; -use Doctrine\ORM\Internal\CriteriaOrderings; use Doctrine\ORM\Mapping\AssociationMapping; use Doctrine\ORM\Mapping\ClassMetadata; use Doctrine\ORM\Mapping\JoinColumnMapping; @@ -98,7 +98,6 @@ */ class BasicEntityPersister implements EntityPersister { - use CriteriaOrderings; use LockSqlHelper; /** @var array */ @@ -844,7 +843,10 @@ public function count(array|Criteria $criteria = []): int */ public function loadCriteria(Criteria $criteria): array { - $orderBy = self::getCriteriaOrderings($criteria); + $orderBy = array_map( + static fn (Order $order): string => $order->value, + $criteria->orderings(), + ); $limit = $criteria->getMaxResults(); $offset = $criteria->getFirstResult(); $query = $this->getSelectSQL($criteria, null, null, $limit, $offset, $orderBy); diff --git a/src/QueryBuilder.php b/src/QueryBuilder.php index 3c41590e7b0..a6a39a964b8 100644 --- a/src/QueryBuilder.php +++ b/src/QueryBuilder.php @@ -8,7 +8,6 @@ use Doctrine\Common\Collections\Criteria; use Doctrine\DBAL\ArrayParameterType; use Doctrine\DBAL\ParameterType; -use Doctrine\ORM\Internal\CriteriaOrderings; use Doctrine\ORM\Internal\NoUnknownNamedArguments; use Doctrine\ORM\Internal\QueryType; use Doctrine\ORM\Query\Expr; @@ -42,7 +41,6 @@ */ class QueryBuilder implements Stringable { - use CriteriaOrderings; use NoUnknownNamedArguments; /** @@ -1189,7 +1187,7 @@ public function addCriteria(Criteria $criteria): static } } - foreach (self::getCriteriaOrderings($criteria) as $sort => $order) { + foreach ($criteria->orderings() as $sort => $order) { $hasValidAlias = false; foreach ($allAliases as $alias) { if (str_starts_with($sort . '.', $alias . '.')) { @@ -1202,7 +1200,7 @@ public function addCriteria(Criteria $criteria): static $sort = $allAliases[0] . '.' . $sort; } - $this->addOrderBy($sort, $order); + $this->addOrderBy($sort, $order->value); } // Overwrite limits only if they was set in criteria