From c54a271244fba7e1d19fd7e9bc25dffc69e3b83d Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Tue, 20 Aug 2024 19:59:28 +0200 Subject: [PATCH] Precise EntityRepository::count --- 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 377e03ce274..5f889bc11f6 100644 --- a/src/Persisters/Entity/BasicEntityPersister.php +++ b/src/Persisters/Entity/BasicEntityPersister.php @@ -835,7 +835,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;