forked from larastan/larastan
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add throw_if and throw_unless extensions
- Loading branch information
Showing
4 changed files
with
85 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Features\Types; | ||
|
||
use Illuminate\Validation\ValidationException; | ||
|
||
class ThrowIf | ||
{ | ||
public function testThrowIfTryCatch(?int $foo): ?int | ||
{ | ||
try { | ||
throw_if(! $foo, ValidationException::withMessages(['$foo is null'])); | ||
} catch (\Exception $e) { | ||
|
||
} | ||
|
||
// Should still be nullable because we don't know if the exception was caught. | ||
return $foo; | ||
} | ||
|
||
/** | ||
* @param int|string $foo | ||
* @return int | ||
*/ | ||
public function testThrowIfWithTypeCheck($foo = 5): int | ||
{ | ||
throw_if(is_string($foo), new \Exception('$foo is a string')); | ||
|
||
return $foo; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Features\Types; | ||
|
||
use Illuminate\Validation\ValidationException; | ||
|
||
class ThrowUnless | ||
{ | ||
public function testThrowUnless(?int $foo): int | ||
{ | ||
throw_unless(! is_null($foo), ValidationException::withMessages(['$foo is null'])); | ||
|
||
return $foo; | ||
} | ||
|
||
/** | ||
* @param int|string $foo | ||
* @return int | ||
*/ | ||
public function testThrowUnlessWithTypeCheck($foo = 5): int | ||
{ | ||
throw_unless(! is_string($foo), new \Exception('$foo is a string')); | ||
|
||
return $foo; | ||
} | ||
} |