From 26581d1ecd86cdbd57e083d567906eebd778c40f Mon Sep 17 00:00:00 2001 From: Toon Verwerft Date: Tue, 29 Sep 2020 15:54:27 +0200 Subject: [PATCH] [Result] Rename classes --- src/Psl/Asio/wrap.php | 27 ------------------- src/Psl/Internal/Loader.php | 8 +++--- .../Failure.php} | 6 ++--- .../ResultInterface.php} | 8 +++--- .../WrappedResult.php => Result/Success.php} | 6 ++--- src/Psl/Result/wrap.php | 27 +++++++++++++++++++ .../FailureTest.php} | 14 +++++----- .../SuccessTest.php} | 14 +++++----- tests/Psl/{Asio => Result}/WrapTest.php | 8 +++--- 9 files changed, 59 insertions(+), 59 deletions(-) delete mode 100644 src/Psl/Asio/wrap.php rename src/Psl/{Asio/WrappedException.php => Result/Failure.php} (90%) rename src/Psl/{Asio/IResultOrExceptionWrapper.php => Result/ResultInterface.php} (86%) rename src/Psl/{Asio/WrappedResult.php => Result/Success.php} (91%) create mode 100644 src/Psl/Result/wrap.php rename tests/Psl/{Asio/WrappedExceptionTest.php => Result/FailureTest.php} (65%) rename tests/Psl/{Asio/WrappedResultTest.php => Result/SuccessTest.php} (70%) rename tests/Psl/{Asio => Result}/WrapTest.php (84%) diff --git a/src/Psl/Asio/wrap.php b/src/Psl/Asio/wrap.php deleted file mode 100644 index ee5b814..0000000 --- a/src/Psl/Asio/wrap.php +++ /dev/null @@ -1,27 +0,0 @@ - - */ -function wrap(callable $fun): IResultOrExceptionWrapper -{ - try { - $result = $fun(); - return new WrappedResult($result); - } catch (Exception $e) { - return new WrappedException($e); - } -} diff --git a/src/Psl/Internal/Loader.php b/src/Psl/Internal/Loader.php index c74ac38..26df4b9 100644 --- a/src/Psl/Internal/Loader.php +++ b/src/Psl/Internal/Loader.php @@ -97,7 +97,6 @@ final class Loader 'Psl\Arr\map_with_key', 'Psl\Fun\after', 'Psl\Fun\pipe', - 'Psl\Asio\wrap', 'Psl\Internal\boolean', 'Psl\Internal\type', 'Psl\Internal\validate_offset', @@ -179,6 +178,7 @@ final class Loader 'Psl\Math\sum_floats', 'Psl\Math\tan', 'Psl\Math\to_base', + 'Psl\Result\wrap', 'Psl\SecureRandom\bytes', 'Psl\SecureRandom\float', 'Psl\SecureRandom\int', @@ -327,7 +327,6 @@ final class Loader public const INTERFACES = [ 'Psl\Exception\ExceptionInterface', - 'Psl\Asio\IResultOrExceptionWrapper', 'Psl\Collection\CollectionInterface', 'Psl\Collection\IndexAccessInterface', 'Psl\Collection\MutableCollectionInterface', @@ -340,6 +339,7 @@ final class Loader 'Psl\Collection\MutableMapInterface', 'Psl\Observer\SubjectInterface', 'Psl\Observer\ObserverInterface', + 'Psl\Result\ResultInterface', ]; public const TRAITS = [ @@ -353,11 +353,11 @@ final class Loader 'Psl\Collection\MutableVector', 'Psl\Collection\Map', 'Psl\Collection\MutableMap', - 'Psl\Asio\WrappedException', - 'Psl\Asio\WrappedResult', 'Psl\Exception\InvalidArgumentException', 'Psl\Exception\RuntimeException', 'Psl\Exception\InvariantViolationException', + 'Psl\Result\Failure', + 'Psl\Result\Success', 'Psl\Type\Internal\ArrayKeyType', 'Psl\Type\Internal\ArrayType', 'Psl\Type\Internal\BoolType', diff --git a/src/Psl/Asio/WrappedException.php b/src/Psl/Result/Failure.php similarity index 90% rename from src/Psl/Asio/WrappedException.php rename to src/Psl/Result/Failure.php index 864d138..9969c40 100644 --- a/src/Psl/Asio/WrappedException.php +++ b/src/Psl/Result/Failure.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Psl\Asio; +namespace Psl\Result; use Exception; use Psl; @@ -13,9 +13,9 @@ use Psl; * @template T * @template Te of Exception * - * @implements IResultOrExceptionWrapper + * @implements ResultInterface */ -final class WrappedException implements IResultOrExceptionWrapper +final class Failure implements ResultInterface { /** * @psalm-var Te diff --git a/src/Psl/Asio/IResultOrExceptionWrapper.php b/src/Psl/Result/ResultInterface.php similarity index 86% rename from src/Psl/Asio/IResultOrExceptionWrapper.php rename to src/Psl/Result/ResultInterface.php index 5a27725..f763e81 100644 --- a/src/Psl/Asio/IResultOrExceptionWrapper.php +++ b/src/Psl/Result/ResultInterface.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Psl\Asio; +namespace Psl\Result; use Exception; use Psl; @@ -11,12 +11,12 @@ use Psl; * Represents a result of operation that either has a successful result or the exception object if * that operation failed. * - * This is an interface. You get generally `IResultOrExceptionWrapper` by calling `wrap()`, passing in - * the `callable(): T`, and a `WrappedResult` or `WrappedException` is returned. + * This is an interface. You get generally `ResultInterface` by calling `tryResultFrom()`, passing in + * the `callable(): T`, and a `Success` or `Failure` is returned. * * @template T */ -interface IResultOrExceptionWrapper +interface ResultInterface { /** * Return the result of the operation, or throw underlying exception. diff --git a/src/Psl/Asio/WrappedResult.php b/src/Psl/Result/Success.php similarity index 91% rename from src/Psl/Asio/WrappedResult.php rename to src/Psl/Result/Success.php index 7be1db2..9626b4e 100644 --- a/src/Psl/Asio/WrappedResult.php +++ b/src/Psl/Result/Success.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Psl\Asio; +namespace Psl\Result; use Exception; use Psl; @@ -12,9 +12,9 @@ use Psl; * * @template T * - * @implements IResultOrExceptionWrapper + * @implements ResultInterface */ -final class WrappedResult implements IResultOrExceptionWrapper +final class Success implements ResultInterface { /** * @psalm-var T diff --git a/src/Psl/Result/wrap.php b/src/Psl/Result/wrap.php new file mode 100644 index 0000000..a7c0b6f --- /dev/null +++ b/src/Psl/Result/wrap.php @@ -0,0 +1,27 @@ + + */ +function wrap(callable $fun): ResultInterface +{ + try { + $result = $fun(); + return new Success($result); + } catch (Exception $e) { + return new Failure($e); + } +} diff --git a/tests/Psl/Asio/WrappedExceptionTest.php b/tests/Psl/Result/FailureTest.php similarity index 65% rename from tests/Psl/Asio/WrappedExceptionTest.php rename to tests/Psl/Result/FailureTest.php index f40162b..4a67578 100644 --- a/tests/Psl/Asio/WrappedExceptionTest.php +++ b/tests/Psl/Result/FailureTest.php @@ -2,29 +2,29 @@ declare(strict_types=1); -namespace Psl\Tests\Asio; +namespace Psl\Tests\Result; use PHPUnit\Framework\TestCase; -use Psl\Asio\WrappedException; +use Psl\Result\Failure; -class WrappedExceptionTest extends TestCase +class FailureTest extends TestCase { public function testIsSucceeded(): void { - $wrapper = new WrappedException(new \Exception('foo')); + $wrapper = new Failure(new \Exception('foo')); self::assertFalse($wrapper->isSucceeded()); } public function testIsFailed(): void { - $wrapper = new WrappedException(new \Exception('foo')); + $wrapper = new Failure(new \Exception('foo')); self::assertTrue($wrapper->isFailed()); } public function testGetResult(): void { $exception = new \Exception('bar'); - $wrapper = new WrappedException($exception); + $wrapper = new Failure($exception); $this->expectExceptionObject($exception); $wrapper->getResult(); @@ -33,7 +33,7 @@ class WrappedExceptionTest extends TestCase public function testGetException(): void { $exception = new \Exception('bar'); - $wrapper = new WrappedException($exception); + $wrapper = new Failure($exception); $e = $wrapper->getException(); self::assertSame($exception, $e); } diff --git a/tests/Psl/Asio/WrappedResultTest.php b/tests/Psl/Result/SuccessTest.php similarity index 70% rename from tests/Psl/Asio/WrappedResultTest.php rename to tests/Psl/Result/SuccessTest.php index 3873f19..b96ab3f 100644 --- a/tests/Psl/Asio/WrappedResultTest.php +++ b/tests/Psl/Result/SuccessTest.php @@ -2,35 +2,35 @@ declare(strict_types=1); -namespace Psl\Tests\Asio; +namespace Psl\Tests\Result; use PHPUnit\Framework\TestCase; -use Psl\Asio\WrappedResult; +use Psl\Result\Success; use Psl\Exception\InvariantViolationException; -class WrappedResultTest extends TestCase +class SuccessTest extends TestCase { public function testIsSucceeded(): void { - $wrapper = new WrappedResult('hello'); + $wrapper = new Success('hello'); self::assertTrue($wrapper->isSucceeded()); } public function testIsFailed(): void { - $wrapper = new WrappedResult('hello'); + $wrapper = new Success('hello'); self::assertFalse($wrapper->isFailed()); } public function testGetResult(): void { - $wrapper = new WrappedResult('hello'); + $wrapper = new Success('hello'); self::assertSame('hello', $wrapper->getResult()); } public function testGetException(): void { - $wrapper = new WrappedResult('hello'); + $wrapper = new Success('hello'); $this->expectException(InvariantViolationException::class); $this->expectExceptionMessage('No exception thrown'); diff --git a/tests/Psl/Asio/WrapTest.php b/tests/Psl/Result/WrapTest.php similarity index 84% rename from tests/Psl/Asio/WrapTest.php rename to tests/Psl/Result/WrapTest.php index 8d97e4a..f955887 100644 --- a/tests/Psl/Asio/WrapTest.php +++ b/tests/Psl/Result/WrapTest.php @@ -2,10 +2,10 @@ declare(strict_types=1); -namespace Psl\Tests\Asio; +namespace Psl\Tests\Result; use PHPUnit\Framework\TestCase; -use Psl\Asio; +use Psl\Result; use Psl\Exception\InvariantViolationException; class WrapTest extends TestCase @@ -13,7 +13,7 @@ class WrapTest extends TestCase public function testWrapException(): void { $exception = new \Exception('foo'); - $wrapper = Asio\wrap(static function () use ($exception): void { + $wrapper = Result\wrap(static function () use ($exception): void { throw $exception; }); self::assertFalse($wrapper->isSucceeded()); @@ -27,7 +27,7 @@ class WrapTest extends TestCase public function testWrapResult(): void { - $wrapper = Asio\wrap(static function (): string { + $wrapper = Result\wrap(static function (): string { return 'foo'; }); self::assertTrue($wrapper->isSucceeded());