From c6b2d897489d535536de435bdef48f2783366ffb Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Sat, 24 Aug 2024 07:29:36 +0200 Subject: [PATCH] Precise EntityRepository::count (#11579) --- src/EntityRepository.php | 1 + src/Persisters/Entity/BasicEntityPersister.php | 5 ++++- src/Persisters/Entity/EntityPersister.php | 2 ++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/EntityRepository.php b/src/EntityRepository.php index a53c5284881..ad472315398 100644 --- a/src/EntityRepository.php +++ b/src/EntityRepository.php @@ -131,6 +131,7 @@ public function findOneBy(array $criteria, array|null $orderBy = null): object|n * @psalm-param array $criteria * * @return int The cardinality of the objects that match the given criteria. + * @psalm-return 0|positive-int * * @todo Add this method to `ObjectRepository` interface in the next major release */ diff --git a/src/Persisters/Entity/BasicEntityPersister.php b/src/Persisters/Entity/BasicEntityPersister.php index abaf8f4c87b..1bb54ed4410 100644 --- a/src/Persisters/Entity/BasicEntityPersister.php +++ b/src/Persisters/Entity/BasicEntityPersister.php @@ -860,7 +860,10 @@ public function count(array|Criteria $criteria = []): int ? $this->expandCriteriaParameters($criteria) : $this->expandParameters($criteria); - return (int) $this->conn->executeQuery($sql, $params, $types)->fetchOne(); + $count = (int) $this->conn->executeQuery($sql, $params, $types)->fetchOne(); + assert($count >= 0); + + return $count; } /** diff --git a/src/Persisters/Entity/EntityPersister.php b/src/Persisters/Entity/EntityPersister.php index 6b278a711d0..ad1c81147ce 100644 --- a/src/Persisters/Entity/EntityPersister.php +++ b/src/Persisters/Entity/EntityPersister.php @@ -125,6 +125,8 @@ public function delete(object $entity): bool; * Count entities (optionally filtered by a criteria) * * @param mixed[]|Criteria $criteria + * + * @psalm-return 0|positive-int */ public function count(array|Criteria $criteria = []): int;