-
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Result] Add proceed and map with helper functions (#65)
- Loading branch information
Showing
10 changed files
with
246 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Fun; | ||
|
||
/** | ||
* This method creates a callback that returns the value passed as argument. | ||
* It can e.g. be used as a success callback. | ||
* | ||
* @template T | ||
* @psalm-return callable(T): T | ||
* @psalm-pure | ||
*/ | ||
function passthrough(): callable | ||
{ | ||
return static fn ($result) => $result; | ||
} |
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,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Fun; | ||
|
||
use Exception; | ||
|
||
/** | ||
* This method creates a callback that throws the exception passed as argument. | ||
* It can e.g. be used as a failure callback. | ||
* | ||
* @psalm-return callable(Exception): no-return | ||
* @psalm-pure | ||
*/ | ||
function rethrow(): callable | ||
{ | ||
return static function (Exception $exception): void { | ||
throw $exception; | ||
}; | ||
} |
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
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,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Tests\Fun; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Psl\Fun; | ||
|
||
class PassthroughTest extends TestCase | ||
{ | ||
public function testPassthrough(): void | ||
{ | ||
$expected = 'x'; | ||
$passthrough = Fun\passthrough(); | ||
|
||
self::assertSame($expected, $passthrough($expected)); | ||
} | ||
} |
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,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Tests\Fun; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Psl\Fun; | ||
|
||
class RethrowTest extends TestCase | ||
{ | ||
public function testRethrow(): void | ||
{ | ||
$exception = new \Exception('foo'); | ||
$rethrow = Fun\rethrow(); | ||
|
||
$this->expectExceptionObject($exception); | ||
$rethrow($exception); | ||
} | ||
} |
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