Skip to content

[12.x] Validate that outOf is greater than 0 in Lottery helper #55969

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Illuminate/Support/Lottery.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,18 @@ class Lottery
* Create a new Lottery instance.
*
* @param int|float $chances
* @param int|null $outOf
* @param int<1, max>|null $outOf
*/
public function __construct($chances, $outOf = null)
{
if ($outOf === null && is_float($chances) && $chances > 1) {
throw new RuntimeException('Float must not be greater than 1.');
}

if ($outOf !== null && $outOf < 1) {
throw new RuntimeException('Lottery "out of" value must be greater than or equal to 1.');
}

$this->chances = $chances;

$this->outOf = $outOf;
Expand Down
7 changes: 7 additions & 0 deletions tests/Support/LotteryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,13 @@ public function testItThrowsForFloatsOverOne()
new Lottery(1.1);
}

public function testItThrowsForOutOfLessThanOne()
{
$this->expectException(RuntimeException::class);

new Lottery(1, 0);
}

public function testItCanWinWithFloat()
{
$wins = false;
Expand Down
Loading