Page MenuHomeDevCentral

D3740.diff
No OneTemporary

D3740.diff

diff --git a/omnitools/src/DataTypes/Result/Ok.php b/omnitools/src/DataTypes/Result/Ok.php
--- a/omnitools/src/DataTypes/Result/Ok.php
+++ b/omnitools/src/DataTypes/Result/Ok.php
@@ -4,6 +4,8 @@
use InvalidArgumentException;
+use Keruald\OmniTools\Reflection\Type;
+
class Ok extends Result {
private mixed $value = null;
@@ -28,7 +30,7 @@
}
public function setValue (mixed $value) : void {
- $type = self::getTypeOf($value);
+ $type = Type::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.
@@ -45,17 +47,6 @@
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;
- }
-
public function map (callable $callable) : self {
$value = $callable($this->value);
diff --git a/omnitools/src/Reflection/Type.php b/omnitools/src/Reflection/Type.php
new file mode 100644
--- /dev/null
+++ b/omnitools/src/Reflection/Type.php
@@ -0,0 +1,17 @@
+<?php
+
+namespace Keruald\OmniTools\Reflection;
+
+class Type {
+
+ public static function getTypeOf ($v) : string {
+ $type = gettype($v);
+
+ if ($type === "object") {
+ return get_class($v);
+ }
+
+ return $type;
+ }
+
+}
diff --git a/omnitools/tests/Reflection/TypeTest.php b/omnitools/tests/Reflection/TypeTest.php
new file mode 100644
--- /dev/null
+++ b/omnitools/tests/Reflection/TypeTest.php
@@ -0,0 +1,27 @@
+<?php
+
+namespace Keruald\OmniTools\Tests\Reflection;
+
+use Keruald\OmniTools\Reflection\Type;
+use Keruald\OmniTools\Strings\Multibyte\OmniString;
+
+use PHPUnit\Framework\Attributes\DataProvider;
+use PHPUnit\Framework\TestCase;
+
+class TypeTest extends TestCase {
+
+ public static function provideTypes () : iterable {
+ // From gettype()
+ yield ["foo", "string"];
+ yield [42, "integer"];
+
+ // From get_class()
+ yield [new OmniString(), OmniString::class];
+ }
+
+ #[DataProvider("provideTypes")]
+ function testGetTypeOf(mixed $value, string $expectedType) : void {
+ $this->assertEquals($expectedType, Type::getTypeOf($value));
+ }
+
+}

File Metadata

Mime Type
text/plain
Expires
Wed, Oct 15, 17:11 (17 h, 43 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3076507
Default Alt Text
D3740.diff (2 KB)

Event Timeline