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;