Page MenuHomeDevCentral

D3231.id8294.diff
No OneTemporary

D3231.id8294.diff

diff --git a/omnitools/src/Reflection/CallableElement.php b/omnitools/src/Reflection/CallableElement.php
--- a/omnitools/src/Reflection/CallableElement.php
+++ b/omnitools/src/Reflection/CallableElement.php
@@ -72,4 +72,35 @@
return $this->callable->getNumberOfParameters();
}
+ /**
+ * Determines if the callable has an explicit return type
+ * and that return type matches the specified type.
+ *
+ * Closure, arrow functions MUST declare explicitly the return type
+ * in the signature to be able to detect it. It will always be false if not.
+ */
+ public function hasReturnType (string $type) : bool {
+ if (!$this->callable->hasReturnType()) {
+ return false;
+ }
+
+ return $this->callable->getReturnType()->getName() === $type;
+ }
+
+ /**
+ * Gets the return type of the callable.
+ *
+ * @throws InvalidArgumentException if the callable doesn't have
+ * an explicit return type.
+ */
+ public function getReturnType () : string {
+ if (!$this->callable->hasReturnType()) {
+ throw new InvalidArgumentException(
+ "Callable doesn't have an explicit return type"
+ );
+ }
+
+ return $this->callable->getReturnType()->getName();
+ }
+
}
diff --git a/omnitools/src/Reflection/CodeVariable.php b/omnitools/src/Reflection/CodeVariable.php
--- a/omnitools/src/Reflection/CodeVariable.php
+++ b/omnitools/src/Reflection/CodeVariable.php
@@ -36,4 +36,18 @@
};
}
+ public function getType () : string {
+ $type = gettype($this->variable);
+
+ // For scalar types, gettype() doesn't return the same types
+ // as does reflection classes.
+ return match ($type) {
+ "boolean" => "bool",
+ "integer" => "int",
+ "double" => "float",
+ "object" => $this->variable::class,
+ default => $type,
+ };
+ }
+
}
diff --git a/omnitools/tests/Reflection/CallableElementTest.php b/omnitools/tests/Reflection/CallableElementTest.php
new file mode 100644
--- /dev/null
+++ b/omnitools/tests/Reflection/CallableElementTest.php
@@ -0,0 +1,56 @@
+<?php
+
+namespace Keruald\OmniTools\Tests\Reflection;
+
+use InvalidArgumentException;
+use Keruald\OmniTools\Reflection\CallableElement;
+
+use PHPUnit\Framework\TestCase;
+
+class CallableElementTest extends TestCase {
+
+ private CallableElement $closure;
+
+ protected function setUp () : void {
+ $fn = fn(int $a, int $b) : int => $a + $b;
+ $this->closure = new CallableElement($fn);
+ }
+
+ public function testCountArguments () : void {
+ $this->assertEquals(2, $this->closure->countArguments());
+ }
+
+ public function testCountArgumentsWhenThereIsNone () : void {
+ $fn = fn() => null;
+ $closure = new CallableElement($fn);
+
+ $this->assertEquals(0, $closure->countArguments());
+ }
+
+ public function testHasReturnType () : void {
+ $this->assertTrue($this->closure->hasReturnType("int"));
+ $this->assertFalse($this->closure->hasReturnType("quux"));
+ }
+
+ public function testHasReturnTypeWhenThereIsNone () : void {
+ // Closure without any explicit return type
+ $fn = fn(int $a, int $b) => $a + $b;
+ $closure = new CallableElement($fn);
+
+ $this->assertFalse($closure->hasReturnType("int"));
+ }
+
+ public function testGetReturnType () : void {
+ $this->assertEquals("int", $this->closure->getReturnType());
+ }
+
+ public function testGetReturnTypeWhenThereIsNone () : void {
+ // Closure without any explicit return type
+ $fn = fn(int $a, int $b) => $a + $b;
+ $closure = new CallableElement($fn);
+
+ $this->expectException(InvalidArgumentException::class);
+ $closure->getReturnType();
+ }
+
+}
diff --git a/omnitools/tests/Reflection/CodeVariableTest.php b/omnitools/tests/Reflection/CodeVariableTest.php
--- a/omnitools/tests/Reflection/CodeVariableTest.php
+++ b/omnitools/tests/Reflection/CodeVariableTest.php
@@ -23,6 +23,20 @@
$this->assertTrue($variable->hasType($type));
}
+ public function testGetTypeWithObject () {
+ $object = new Vector;
+ $variable = CodeVariable::from($object);
+
+ $this->assertEquals(Vector::class, $variable->getType());
+ }
+
+ #[DataProvider('provideScalarsAndReturnedTypes')]
+ public function testGetTypeWithScalar (mixed $scalar, string $expected) {
+ $variable = CodeVariable::from($scalar);
+
+ $this->assertEquals($expected, $variable->getType());
+ }
+
#[DataProvider('provideScalars')]
public function testFromWithScalar (mixed $scalar) {
$variable = CodeVariable::from($scalar);
@@ -60,4 +74,14 @@
yield [false, "boolean"];
yield [null, "NULL"];
}
+
+ public static function provideScalarsAndReturnedTypes () : iterable {
+ yield [0, "int"];
+ yield ["", "string"];
+ yield [19, "int"];
+ yield ["This is Sparta.", "string"];
+ yield [true, "bool"];
+ yield [false, "bool"];
+ yield [null, "NULL"];
+ }
}

File Metadata

Mime Type
text/plain
Expires
Mon, Nov 25, 19:30 (19 h, 14 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2262451
Default Alt Text
D3231.id8294.diff (5 KB)

Event Timeline