Skip to content

Commit 7d56eff

Browse files
authored
add default parameter to throw_if / throw_unless (#35890)
1 parent 83ec510 commit 7d56eff

File tree

2 files changed

+67
-5
lines changed

2 files changed

+67
-5
lines changed

src/Illuminate/Support/helpers.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -277,10 +277,14 @@ function tap($value, $callback = null)
277277
*
278278
* @throws \Throwable
279279
*/
280-
function throw_if($condition, $exception, ...$parameters)
280+
function throw_if($condition, $exception = 'RuntimeException', ...$parameters)
281281
{
282282
if ($condition) {
283-
throw (is_string($exception) ? new $exception(...$parameters) : $exception);
283+
if (is_string($exception) && class_exists($exception)) {
284+
$exception = new $exception(...$parameters);
285+
}
286+
287+
throw is_string($exception) ? new RuntimeException($exception) : $exception;
284288
}
285289

286290
return $condition;
@@ -298,10 +302,14 @@ function throw_if($condition, $exception, ...$parameters)
298302
*
299303
* @throws \Throwable
300304
*/
301-
function throw_unless($condition, $exception, ...$parameters)
305+
function throw_unless($condition, $exception = 'RuntimeException', ...$parameters)
302306
{
303307
if (! $condition) {
304-
throw (is_string($exception) ? new $exception(...$parameters) : $exception);
308+
if (is_string($exception) && class_exists($exception)) {
309+
$exception = new $exception(...$parameters);
310+
}
311+
312+
throw is_string($exception) ? new RuntimeException($exception) : $exception;
305313
}
306314

307315
return $condition;

tests/Support/SupportHelpersTest.php

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Contracts\Support\Htmlable;
77
use Illuminate\Support\Env;
88
use Illuminate\Support\Optional;
9+
use LogicException;
910
use Mockery as m;
1011
use PHPUnit\Framework\TestCase;
1112
use RuntimeException;
@@ -362,10 +363,63 @@ public function testTap()
362363
}
363364

364365
public function testThrow()
366+
{
367+
$this->expectException(LogicException::class);
368+
369+
throw_if(true, new LogicException);
370+
}
371+
372+
public function testThrowDefaultException()
373+
{
374+
$this->expectException(RuntimeException::class);
375+
376+
throw_if(true);
377+
}
378+
379+
public function testThrowExceptionWithMessage()
365380
{
366381
$this->expectException(RuntimeException::class);
382+
$this->expectExceptionMessage('test');
383+
384+
throw_if(true, 'test');
385+
}
386+
387+
public function testThrowExceptionAsStringWithMessage()
388+
{
389+
$this->expectException(LogicException::class);
390+
$this->expectExceptionMessage('test');
391+
392+
throw_if(true, LogicException::class, 'test');
393+
}
394+
395+
public function testThrowUnless()
396+
{
397+
$this->expectException(LogicException::class);
398+
399+
throw_unless(false, new LogicException);
400+
}
401+
402+
public function testThrowUnlessDefaultException()
403+
{
404+
$this->expectException(RuntimeException::class);
405+
406+
throw_unless(false);
407+
}
408+
409+
public function testThrowUnlessExceptionWithMessage()
410+
{
411+
$this->expectException(RuntimeException::class);
412+
$this->expectExceptionMessage('test');
413+
414+
throw_unless(false, 'test');
415+
}
416+
417+
public function testThrowUnlessExceptionAsStringWithMessage()
418+
{
419+
$this->expectException(LogicException::class);
420+
$this->expectExceptionMessage('test');
367421

368-
throw_if(true, new RuntimeException);
422+
throw_unless(false, LogicException::class, 'test');
369423
}
370424

371425
public function testThrowReturnIfNotThrown()

0 commit comments

Comments
 (0)