Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b4b2ffe

Browse files
authored
fix: Enhanced random method with additional safety check. (#991)
1 parent 2c84cb0 commit b4b2ffe

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

src/Persistence/RepositoryDecorator.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,13 @@ public function random(array $criteria = []): object
199199
$offset = \random_int(0, $count - 1);
200200
}
201201

202-
return $this->findBy($criteria, limit: 1, offset: $offset)[0];
202+
$result = $this->findBy($criteria, limit: 1, offset: $offset);
203+
204+
if (!\count($result)) {
205+
throw new NotEnoughObjects(\sprintf('At least %d "%s" object(s) must have been persisted (%d persisted).', 1, $this->getClassName(), 0));
206+
}
207+
208+
return $result[0];
203209
}
204210

205211
/**

0 commit comments

Comments
 (0)