From 2ce513c174fc15f2aca2da3bca3067f66d2b82f1 Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Thu, 12 Sep 2024 20:43:17 +0400 Subject: [PATCH] modifs.mineures sans commentaires --- .idea/php-test-framework.xml | 14 ++++ tests/wip/php/funcTest.php | 115 ++++++++++++++++++++++++++ tests/wip/php/impl/{CC.php => C0.php} | 2 +- tests/wip/php/impl/C1.php | 18 ++++ wip/php/func.php | 30 ++++--- 5 files changed, 166 insertions(+), 13 deletions(-) create mode 100644 .idea/php-test-framework.xml rename tests/wip/php/impl/{CC.php => C0.php} (94%) create mode 100644 tests/wip/php/impl/C1.php diff --git a/.idea/php-test-framework.xml b/.idea/php-test-framework.xml new file mode 100644 index 0000000..c092999 --- /dev/null +++ b/.idea/php-test-framework.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/tests/wip/php/funcTest.php b/tests/wip/php/funcTest.php index e5da71e..9e164f0 100644 --- a/tests/wip/php/funcTest.php +++ b/tests/wip/php/funcTest.php @@ -2,6 +2,13 @@ namespace { function tsimple(): int { return 0; } + function tm1($a): array { return [$a]; } + function to1($b=9): array { return [$b]; } + function tv(...$c): array { return [...$c]; } + function tm1o1($a, $b=9): array { return [$a, $b]; } + function tm1v($a, ...$c): array { return [$a, ...$c]; } + function tm1o1v($a, $b=9, ...$c): array { return [$a, $b, ...$c]; } + function to1v($b=9, ...$c): array { return [$b, ...$c]; } } namespace nur\sery\wip\php\impl { @@ -10,6 +17,8 @@ namespace nur\sery\wip\php\impl { namespace nur\sery\wip\php { use nulib\tests\TestCase; + use nur\sery\wip\php\impl\C0; + use nur\sery\wip\php\impl\C1; use nur\sery\wip\php\impl\SC; class funcTest extends TestCase { @@ -913,5 +922,111 @@ namespace nur\sery\wip\php { } } } + + function testInvokeFunction() { + # m1 + self::assertSame([null], func::call("tm1")); + self::assertSame([null], func::call("tm1", null)); + self::assertSame([null], func::call("tm1", null, null)); + self::assertSame([null], func::call("tm1", null, null, null)); + self::assertSame([null], func::call("tm1", null, null, null, null)); + self::assertSame([1], func::call("tm1", 1)); + self::assertSame([1], func::call("tm1", 1, 2)); + self::assertSame([1], func::call("tm1", 1, 2, 3)); + self::assertSame([1], func::call("tm1", 1, 2, 3, 4)); + + # o1 + self::assertSame([9], func::call("to1")); + self::assertSame([null], func::call("to1", null)); + self::assertSame([null], func::call("to1", null, null)); + self::assertSame([null], func::call("to1", null, null, null)); + self::assertSame([null], func::call("to1", null, null, null, null)); + self::assertSame([1], func::call("to1", 1)); + self::assertSame([1], func::call("to1", 1, 2)); + self::assertSame([1], func::call("to1", 1, 2, 3)); + self::assertSame([1], func::call("to1", 1, 2, 3, 4)); + + # v + self::assertSame([], func::call("tv")); + self::assertSame([null], func::call("tv", null)); + self::assertSame([null, null], func::call("tv", null, null)); + self::assertSame([null, null, null], func::call("tv", null, null, null)); + self::assertSame([null, null, null, null], func::call("tv", null, null, null, null)); + self::assertSame([1], func::call("tv", 1)); + self::assertSame([1, 2], func::call("tv", 1, 2)); + self::assertSame([1, 2, 3], func::call("tv", 1, 2, 3)); + self::assertSame([1, 2, 3, 4], func::call("tv", 1, 2, 3, 4)); + + # m1o1 + self::assertSame([null, 9], func::call("tm1o1")); + self::assertSame([null, 9], func::call("tm1o1", null)); + self::assertSame([null, null], func::call("tm1o1", null, null)); + self::assertSame([null, null], func::call("tm1o1", null, null, null)); + self::assertSame([null, null], func::call("tm1o1", null, null, null, null)); + self::assertSame([1, 9], func::call("tm1o1", 1)); + self::assertSame([1, 2], func::call("tm1o1", 1, 2)); + self::assertSame([1, 2], func::call("tm1o1", 1, 2, 3)); + self::assertSame([1, 2], func::call("tm1o1", 1, 2, 3, 4)); + + # m1v + self::assertSame([null], func::call("tm1v")); + self::assertSame([null], func::call("tm1v", null)); + self::assertSame([null, null], func::call("tm1v", null, null)); + self::assertSame([null, null, null], func::call("tm1v", null, null, null)); + self::assertSame([null, null, null, null], func::call("tm1v", null, null, null, null)); + self::assertSame([1], func::call("tm1v", 1)); + self::assertSame([1, 2], func::call("tm1v", 1, 2)); + self::assertSame([1, 2, 3], func::call("tm1v", 1, 2, 3)); + self::assertSame([1, 2, 3, 4], func::call("tm1v", 1, 2, 3, 4)); + + # m1o1v + self::assertSame([null, 9], func::call("tm1o1v")); + self::assertSame([null, 9], func::call("tm1o1v", null)); + self::assertSame([null, null], func::call("tm1o1v", null, null)); + self::assertSame([null, null, null], func::call("tm1o1v", null, null, null)); + self::assertSame([null, null, null, null], func::call("tm1o1v", null, null, null, null)); + self::assertSame([1, 9], func::call("tm1o1v", 1)); + self::assertSame([1, 2], func::call("tm1o1v", 1, 2)); + self::assertSame([1, 2, 3], func::call("tm1o1v", 1, 2, 3)); + self::assertSame([1, 2, 3, 4], func::call("tm1o1v", 1, 2, 3, 4)); + + # o1v + self::assertSame([9], func::call("to1v")); + self::assertSame([null], func::call("to1v", null)); + self::assertSame([null, null], func::call("to1v", null, null)); + self::assertSame([null, null, null], func::call("to1v", null, null, null)); + self::assertSame([null, null, null, null], func::call("to1v", null, null, null, null)); + self::assertSame([1], func::call("to1v", 1)); + self::assertSame([1, 2], func::call("to1v", 1, 2)); + self::assertSame([1, 2, 3], func::call("to1v", 1, 2, 3)); + self::assertSame([1, 2, 3, 4], func::call("to1v", 1, 2, 3, 4)); + } + + function testInvokeClass() { + $func = func::with(SC::class); + self::assertInstanceOf(SC::class, $func->invoke()); + self::assertInstanceOf(SC::class, $func->invoke([])); + self::assertInstanceOf(SC::class, $func->invoke([1])); + self::assertInstanceOf(SC::class, $func->invoke([1, 2])); + self::assertInstanceOf(SC::class, $func->invoke([1, 2, 3])); + + $func = func::with(C0::class); + self::assertInstanceOf(C0::class, $func->invoke()); + self::assertInstanceOf(C0::class, $func->invoke([])); + self::assertInstanceOf(C0::class, $func->invoke([1])); + self::assertInstanceOf(C0::class, $func->invoke([1, 2])); + self::assertInstanceOf(C0::class, $func->invoke([1, 2, 3])); + + $func = func::with(C1::class); + /** @var C1 $i1 */ + $i1 = $func->invoke(); + self::assertInstanceOf(C1::class, $i1); self::assertSame(null, $i1->first); + $i1 = $func->invoke([]); + self::assertInstanceOf(C1::class, $i1); self::assertSame(null, $i1->first); + $i1 = $func->invoke([1]); + self::assertInstanceOf(C1::class, $i1); self::assertSame(1, $i1->first); + $i1 = $func->invoke([1, 2]); + self::assertInstanceOf(C1::class, $i1); self::assertSame(1, $i1->first); + } } } diff --git a/tests/wip/php/impl/CC.php b/tests/wip/php/impl/C0.php similarity index 94% rename from tests/wip/php/impl/CC.php rename to tests/wip/php/impl/C0.php index defc10c..f9c7619 100644 --- a/tests/wip/php/impl/CC.php +++ b/tests/wip/php/impl/C0.php @@ -1,7 +1,7 @@ first = $first; + } + + public $first; + + static function tstatic(): int { + return 10; + } + + function tmethod(): int { + return 11; + } +} diff --git a/wip/php/func.php b/wip/php/func.php index 201662d..965d2f8 100644 --- a/wip/php/func.php +++ b/wip/php/func.php @@ -67,15 +67,17 @@ class func { } if ($func instanceof ReflectionFunction) return true; if (is_string($func)) { - $func = [false, $func]; + $c = false; + $f = $func; } elseif (is_array($func)) { if (!array_key_exists(0, $func)) return false; + $c = $func[0]; if (!array_key_exists(1, $func)) return false; + $f = $func[1]; } else { return false; } - if ($func[0] !== false) return false; - $f = $func[1]; + if ($c !== false) return false; if (!is_string($f)) return false; if (self::_is_invalid($f)) return false; if (self::_parse_static($f)) return false; @@ -91,6 +93,7 @@ class func { return false; } } + $func = [false, $f]; return true; } @@ -123,25 +126,28 @@ class func { } if ($func instanceof ReflectionClass) return true; if (is_string($func)) { - $func = [$func, false]; + $c = $func; + $f = false; } elseif (is_array($func)) { if (!array_key_exists(0, $func)) return false; + $c = $func[0]; if (!array_key_exists(1, $func)) return false; + $f = $func[1]; } else { return false; } - $c = $func[0]; if (!is_string($c)) return false; if (self::_is_invalid($c)) return false; - if (self::_parse_static($f)) return false; - if (self::_parse_method($f)) return false; - if ($func[1] !== false) return false; + if (self::_parse_static($c)) return false; + if (self::_parse_method($c)) return false; + if ($f !== false) return false; if ($strict) { if (!class_exists($c)) { $reason = "$msg: class not found"; return false; } } + $func = [$c, false]; return true; } @@ -504,7 +510,7 @@ class func { ############################################################################# - const TYPE_CLOSURE = 0, TYPE_SIMPLE = 1, TYPE_STATIC = 2, TYPE_METHOD = 3, TYPE_CLASS = 4; + const TYPE_CLOSURE = 0, TYPE_SIMPLE = 1, TYPE_CLASS = 2, TYPE_STATIC = 3, TYPE_METHOD = 4; function __construct($func) { $object = null; @@ -516,12 +522,12 @@ class func { } elseif ($func instanceof ReflectionFunction) { $type = self::TYPE_SIMPLE; $reflection = $func; - } elseif ($func instanceof ReflectionMethod) { - $type = self::TYPE_STATIC; - $reflection = $func; } elseif ($func instanceof ReflectionClass) { $type = self::TYPE_CLASS; $reflection = $func; + } elseif ($func instanceof ReflectionMethod) { + $type = self::TYPE_STATIC; + $reflection = $func; } elseif (is_array($func)) { if (count($func) > 2) { $prefixArgs = array_slice($func, 2);