Page MenuHomeDevCentral

D1656.id9238.diff
No OneTemporary

D1656.id9238.diff

diff --git a/omnitools/src/DataTypes/Result/Err.php b/omnitools/src/DataTypes/Result/Err.php
new file mode 100644
--- /dev/null
+++ b/omnitools/src/DataTypes/Result/Err.php
@@ -0,0 +1,35 @@
+<?php
+
+namespace Keruald\OmniTools\DataTypes\Result;
+
+use Exception;
+use InvalidArgumentException;
+
+class Err extends Result {
+ private Exception $error;
+
+ public function isOK () : false {
+ return false;
+ }
+
+ public function isError () : true {
+ return true;
+ }
+
+ public function getValue () : mixed {
+ throw new InvalidArgumentException(<<<'EOD'
+This result is an error, so it doesn't have a value.
+You can check first with isOK() if this is a value.
+Or if you want the error, use getError().
+EOD
+);
+ }
+
+ public function getError () : Exception {
+ return $this->error;
+ }
+
+ public function setError (Exception $error) : void {
+ $this->error = $error;
+ }
+}
diff --git a/omnitools/src/DataTypes/Result/Ok.php b/omnitools/src/DataTypes/Result/Ok.php
new file mode 100644
--- /dev/null
+++ b/omnitools/src/DataTypes/Result/Ok.php
@@ -0,0 +1,52 @@
+<?php
+
+namespace Keruald\OmniTools\DataTypes\Result;
+
+use InvalidArgumentException;
+
+class Ok extends Result {
+ private mixed $value = null;
+
+ private string $type;
+
+ public function isOK () : true {
+ return true;
+ }
+
+ public function isError () : false {
+ return false;
+ }
+
+ public function getValue () : mixed {
+ return $this->value;
+ }
+
+ public function setValue (mixed $value) : void {
+ $type = self::getTypeOf($value);
+ if (!$this->isAcceptableValueType($type)) {
+ throw new InvalidArgumentException(<<<'EOD'
+When you mutate the value of an Ok object, you can't mutate the object type.
+Please consider return a new Ok instead.
+EOD
+ );
+ }
+
+ $this->value = $value;
+ $this->type = $type;
+ }
+
+ private function isAcceptableValueType (string $type) : bool {
+ return $this->value === null || $type === $this->type;
+ }
+
+
+ private static function getTypeOf ($v) : string {
+ $type = gettype($v);
+
+ if ($type === "object") {
+ return get_class($v);
+ }
+
+ return $type;
+ }
+}
diff --git a/omnitools/src/DataTypes/Result/Result.php b/omnitools/src/DataTypes/Result/Result.php
new file mode 100644
--- /dev/null
+++ b/omnitools/src/DataTypes/Result/Result.php
@@ -0,0 +1,10 @@
+<?php
+
+namespace Keruald\OmniTools\DataTypes\Result;
+
+abstract class Result {
+ public abstract function isOK () : bool;
+ public abstract function isError () : bool;
+
+ public abstract function getValue () : mixed;
+}
diff --git a/omnitools/tests/DataTypes/Result/ErrTest.php b/omnitools/tests/DataTypes/Result/ErrTest.php
new file mode 100644
--- /dev/null
+++ b/omnitools/tests/DataTypes/Result/ErrTest.php
@@ -0,0 +1,26 @@
+<?php
+declare(strict_types=1);
+
+namespace Keruald\OmniTools\Tests\DataTypes\Result;
+
+use Keruald\OmniTools\DataTypes\Result\Err;
+use PHPUnit\Framework\TestCase;
+
+class ErrTest extends TestCase {
+ public function setUp () : void {
+ $this->v = new Err;
+ }
+
+ public function testIsOk () : void {
+ $this->AssertFalse($this->v->isOk());
+ }
+
+ public function testIsError () : void {
+ $this->assertTrue($this->v->isError());
+ }
+
+ public function testGetValue () : void {
+ $this->expectException("InvalidArgumentException");
+ $this->v->getValue();
+ }
+}
diff --git a/omnitools/tests/DataTypes/Result/OkTest.php b/omnitools/tests/DataTypes/Result/OkTest.php
new file mode 100644
--- /dev/null
+++ b/omnitools/tests/DataTypes/Result/OkTest.php
@@ -0,0 +1,37 @@
+<?php
+declare(strict_types=1);
+
+namespace Keruald\OmniTools\Tests\DataTypes\Result;
+
+use Keruald\OmniTools\DataTypes\Result\Ok;
+use PHPUnit\Framework\TestCase;
+
+class OkTest extends TestCase {
+
+ public function setUp () : void {
+ $this->v = new Ok;
+ $this->v->setValue(42);
+ }
+
+ public function testIsOk () : void {
+ $this->AssertTrue($this->v->isOk());
+ }
+
+ public function testIsError () : void {
+ $this->assertFalse($this->v->isError());
+ }
+
+ public function testGetValue () : void {
+ $this->assertEquals(42, $this->v->getValue());
+ }
+
+ public function testSetValue () : void {
+ $this->v->setValue(666);
+ $this->assertEquals(666, $this->v->getValue());
+ }
+
+ public function testSetValueWhenTypeIsMutated () : void {
+ $this->expectException("InvalidArgumentException");
+ $this->v->setValue("Another type");
+ }
+}
diff --git a/phpunit.xml b/phpunit.xml
--- a/phpunit.xml
+++ b/phpunit.xml
@@ -20,6 +20,10 @@
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd"
+ displayDetailsOnTestsThatTriggerDeprecations="true"
+ displayDetailsOnTestsThatTriggerErrors="true"
+ displayDetailsOnTestsThatTriggerWarnings="true"
+ displayDetailsOnTestsThatTriggerNotices="true"
bootstrap="vendor/autoload.php"
cacheDirectory=".phpunit.cache"
stopOnFailure="false">

File Metadata

Mime Type
text/plain
Expires
Mon, Nov 18, 14:34 (21 h, 58 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2250768
Default Alt Text
D1656.id9238.diff (5 KB)

Event Timeline