From 15d4de2d3c2d0a6302be6168568fed00e18264e4 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sat, 7 Nov 2026 21:11:52 +0100 Subject: [PATCH 01/29] Add lang.reflection.Closure to reflect callable closures --- .../php/lang/reflection/Closure.class.php | 95 +++++++++++++++++++ src/main/php/lang/reflection/Member.class.php | 2 +- .../php/lang/reflection/Parameter.class.php | 2 +- .../php/lang/reflection/Parameters.class.php | 2 +- src/main/php/lang/reflection/Target.class.php | 7 ++ .../lang/reflection/TargetException.class.php | 4 +- .../reflection/unittest/ClosureTest.class.php | 90 ++++++++++++++++++ 7 files changed, 197 insertions(+), 5 deletions(-) create mode 100755 src/main/php/lang/reflection/Closure.class.php create mode 100755 src/main/php/lang/reflection/Target.class.php create mode 100755 src/test/php/lang/reflection/unittest/ClosureTest.class.php diff --git a/src/main/php/lang/reflection/Closure.class.php b/src/main/php/lang/reflection/Closure.class.php new file mode 100755 index 0000000..e3011ef --- /dev/null +++ b/src/main/php/lang/reflection/Closure.class.php @@ -0,0 +1,95 @@ +reflect= $arg; + } else { + $this->reflect= new ReflectionFunction($arg); + } + } + + /** Returns a compound name consisting of `[NAME]()` */ + public function compoundName(): string { return $this->reflect->name.'()'; } + + /** + * Returns this functions's doc comment, or NULL if there is none. + * + * @return ?string + */ + public function comment() { + return false === ($c= $this->reflect->getDocComment()) + ? null + : trim(preg_replace('/\n\s+\* ?/', "\n", substr($c, 3, -2))) + ; + } + + /** + * Looks up a parameter + * + * @param int|string $arg Either a position or a name + * @return ?lang.reflection.Parameter + */ + public function parameter($arg) { + if (is_int($arg)) { + $p= $this->reflect->getParameters()[$arg] ?? null; + } else { + $p= null; + foreach ($this->reflect->getParameters() as $parameter) { + if ($arg !== $parameter->name) continue; + $p= $parameter; + break; + } + } + return null === $p ? null : new Parameter($p, $this->reflect); + } + + /** + * Returns all parameters + * + * @return lang.reflection.Parameters + */ + public function parameters(): Parameters { + return new Parameters($this->reflect); + } + + /** @return lang.reflection.Constraint */ + public function returns() { + $present= true; + $absent= function($set) use(&$present) { $present= false; }; + + $t= Type::resolve($this->reflect->getReturnType(), [], $absent); + return new Constraint($t ?? Type::$VAR, $present); + } + + /** + * Invokes this closure + * + * @param var[] $args + * @return var + * @throws lang.reflection.CannotInvoke if prerequisites to the invocation fail + * @throws lang.reflection.InvocationFailed if invocation raises an exception + */ + public function invoke($args= []) { + try { + $pass= PHP_VERSION_ID < 80000 && $args ? Routine::pass($this->reflect, $args) : $args; + return $this->reflect->invokeArgs($pass); + } catch (ReflectionException|ArgumentCountError|TypeError $e) { + throw new CannotInvoke($this, $e); + } catch (Throwable $e) { + + // This really should be an ArgumentCountError... + if (0 === strpos($e->getMessage(), 'Unknown named parameter $')) { + throw new CannotInvoke($this, $e); + } + + throw new InvocationFailed($this, $e); + } + } +} \ No newline at end of file diff --git a/src/main/php/lang/reflection/Member.class.php b/src/main/php/lang/reflection/Member.class.php index 568b394..2dcb808 100755 --- a/src/main/php/lang/reflection/Member.class.php +++ b/src/main/php/lang/reflection/Member.class.php @@ -3,7 +3,7 @@ use lang\{XPClass, Reflection, Value}; /** Base class for constants, properties and methods */ -abstract class Member implements Annotated, Value { +abstract class Member implements Annotated, Target, Value { protected $reflect, $annotations; /** diff --git a/src/main/php/lang/reflection/Parameter.class.php b/src/main/php/lang/reflection/Parameter.class.php index cb11475..43d560f 100755 --- a/src/main/php/lang/reflection/Parameter.class.php +++ b/src/main/php/lang/reflection/Parameter.class.php @@ -15,7 +15,7 @@ class Parameter implements Annotated { * Creates a new parameter * * @param ReflectionParameter $reflect - * @param ReflectionMethod $method + * @param ReflectionFuntionAbstract $method */ public function __construct($reflect, $method= null) { $this->reflect= $reflect; diff --git a/src/main/php/lang/reflection/Parameters.class.php b/src/main/php/lang/reflection/Parameters.class.php index 6854099..66d6f8b 100755 --- a/src/main/php/lang/reflection/Parameters.class.php +++ b/src/main/php/lang/reflection/Parameters.class.php @@ -11,7 +11,7 @@ class Parameters implements \IteratorAggregate { private $method; - /** @param ReflectionMethod $method */ + /** @param ReflectionFunctionAbstract $method */ public function __construct($method) { $this->method= $method; } diff --git a/src/main/php/lang/reflection/Target.class.php b/src/main/php/lang/reflection/Target.class.php new file mode 100755 index 0000000..7303045 --- /dev/null +++ b/src/main/php/lang/reflection/Target.class.php @@ -0,0 +1,7 @@ +compoundName(), $cause); $this->target= $target; } /** Returns the target member */ - public function target(): Member { return $this->target; } + public function target(): Target { return $this->target; } } \ No newline at end of file diff --git a/src/test/php/lang/reflection/unittest/ClosureTest.class.php b/src/test/php/lang/reflection/unittest/ClosureTest.class.php new file mode 100755 index 0000000..ff9bc83 --- /dev/null +++ b/src/test/php/lang/reflection/unittest/ClosureTest.class.php @@ -0,0 +1,90 @@ + true]; + yield [function() { return true; }]; + yield [new ReflectionFunction('strtoupper')]; + } + + #[Test, Values(from: 'callables')] + public function can_create($callable) { + new Closure($callable); + } + + #[Test] + public function no_comment() { + Assert::null((new Closure(fn() => true))->comment()); + } + + #[Test] + public function with_comment() { + Assert::equals('Test', (new Closure(/** Test */ fn() => true))->comment()); + } + + #[Test] + public function empty_parameters() { + $parameters= (new Closure(fn() => true))->parameters(); + Assert::equals(0, $parameters->size()); + } + + #[Test] + public function parameters() { + $parameters= (new Closure(fn($name) => true))->parameters(); + Assert::equals(1, $parameters->size()); + } + + #[Test, Values([0, 'name'])] + public function parameter($arg) { + $parameter= (new Closure(fn($name) => true))->parameter($arg); + Assert::equals('name', $parameter->name()); + Assert::false($parameter->optional()); + } + + #[Test] + public function optional_parameter() { + $parameters= (new Closure(fn($name= null) => true))->parameters(); + Assert::equals(0, $parameters->size($required= true)); + Assert::equals(1, $parameters->size($required= false)); + } + + #[Test] + public function without_return_type() { + $constraint= (new Closure(fn($name) => true))->returns(); + Assert::false($constraint->present()); + Assert::equals(Type::$VAR, $constraint->type()); + } + + #[Test] + public function return_type() { + $constraint= (new Closure(fn($name): bool => true))->returns(); + Assert::true($constraint->present()); + Assert::equals(Primitive::$BOOL, $constraint->type()); + } + + #[Test] + public function invoke() { + $closure= (new Closure(fn($name) => strtoupper($name))); + Assert::equals('TEST', $closure->invoke(['test'])); + } + + #[Test] + public function invoke_named() { + $closure= (new Closure(fn($name) => strtoupper($name))); + Assert::equals('TEST', $closure->invoke(['name' => 'test'])); + } + + #[Test, Expect(CannotInvoke::class)] + public function invoke_missing_argument() { + $closure= (new Closure(fn($name) => strtoupper($name))); + $closure->invoke([]); + } +} \ No newline at end of file From a139ede5c3ee507dbe63afb119dd4c724937c3a6 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Mon, 9 Nov 2026 20:57:38 +0100 Subject: [PATCH 02/29] Rename Closure -> Invocable --- ...{Closure.class.php => Invocable.class.php} | 2 +- ...Test.class.php => InvocableTest.class.php} | 34 +++++++++---------- 2 files changed, 18 insertions(+), 18 deletions(-) rename src/main/php/lang/reflection/{Closure.class.php => Invocable.class.php} (98%) rename src/test/php/lang/reflection/unittest/{ClosureTest.class.php => InvocableTest.class.php} (60%) diff --git a/src/main/php/lang/reflection/Closure.class.php b/src/main/php/lang/reflection/Invocable.class.php similarity index 98% rename from src/main/php/lang/reflection/Closure.class.php rename to src/main/php/lang/reflection/Invocable.class.php index e3011ef..eee2b9b 100755 --- a/src/main/php/lang/reflection/Closure.class.php +++ b/src/main/php/lang/reflection/Invocable.class.php @@ -3,7 +3,7 @@ use ReflectionException, ReflectionFunction, ArgumentCountError, TypeError, Throwable; use lang\{Reflection, Type}; -class Closure implements Target { +class Invocable implements Target { private $reflect; /** @param string|Closure|ReflectionFunction $arg */ diff --git a/src/test/php/lang/reflection/unittest/ClosureTest.class.php b/src/test/php/lang/reflection/unittest/InvocableTest.class.php similarity index 60% rename from src/test/php/lang/reflection/unittest/ClosureTest.class.php rename to src/test/php/lang/reflection/unittest/InvocableTest.class.php index ff9bc83..fa9be64 100755 --- a/src/test/php/lang/reflection/unittest/ClosureTest.class.php +++ b/src/test/php/lang/reflection/unittest/InvocableTest.class.php @@ -1,11 +1,11 @@ true))->comment()); + Assert::null((new Invocable(fn() => true))->comment()); } #[Test] public function with_comment() { - Assert::equals('Test', (new Closure(/** Test */ fn() => true))->comment()); + Assert::equals('Test', (new Invocable(/** Test */ fn() => true))->comment()); } #[Test] public function empty_parameters() { - $parameters= (new Closure(fn() => true))->parameters(); + $parameters= (new Invocable(fn() => true))->parameters(); Assert::equals(0, $parameters->size()); } #[Test] public function parameters() { - $parameters= (new Closure(fn($name) => true))->parameters(); + $parameters= (new Invocable(fn($name) => true))->parameters(); Assert::equals(1, $parameters->size()); } #[Test, Values([0, 'name'])] public function parameter($arg) { - $parameter= (new Closure(fn($name) => true))->parameter($arg); + $parameter= (new Invocable(fn($name) => true))->parameter($arg); Assert::equals('name', $parameter->name()); Assert::false($parameter->optional()); } #[Test] public function optional_parameter() { - $parameters= (new Closure(fn($name= null) => true))->parameters(); + $parameters= (new Invocable(fn($name= null) => true))->parameters(); Assert::equals(0, $parameters->size($required= true)); Assert::equals(1, $parameters->size($required= false)); } #[Test] public function without_return_type() { - $constraint= (new Closure(fn($name) => true))->returns(); + $constraint= (new Invocable(fn($name) => true))->returns(); Assert::false($constraint->present()); Assert::equals(Type::$VAR, $constraint->type()); } #[Test] public function return_type() { - $constraint= (new Closure(fn($name): bool => true))->returns(); + $constraint= (new Invocable(fn($name): bool => true))->returns(); Assert::true($constraint->present()); Assert::equals(Primitive::$BOOL, $constraint->type()); } #[Test] public function invoke() { - $closure= (new Closure(fn($name) => strtoupper($name))); - Assert::equals('TEST', $closure->invoke(['test'])); + $Invocable= (new Invocable(fn($name) => strtoupper($name))); + Assert::equals('TEST', $Invocable->invoke(['test'])); } #[Test] public function invoke_named() { - $closure= (new Closure(fn($name) => strtoupper($name))); - Assert::equals('TEST', $closure->invoke(['name' => 'test'])); + $Invocable= (new Invocable(fn($name) => strtoupper($name))); + Assert::equals('TEST', $Invocable->invoke(['name' => 'test'])); } #[Test, Expect(CannotInvoke::class)] public function invoke_missing_argument() { - $closure= (new Closure(fn($name) => strtoupper($name))); - $closure->invoke([]); + $Invocable= (new Invocable(fn($name) => strtoupper($name))); + $Invocable->invoke([]); } } \ No newline at end of file From b8c2f5b8b4437d3b238ac253548b77580ece6549 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Mon, 9 Nov 2026 21:02:24 +0100 Subject: [PATCH 03/29] QA: Use imports for global classes --- src/main/php/lang/Reflection.class.php | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/main/php/lang/Reflection.class.php b/src/main/php/lang/Reflection.class.php index 7c70851..392c980 100755 --- a/src/main/php/lang/Reflection.class.php +++ b/src/main/php/lang/Reflection.class.php @@ -1,5 +1,6 @@ providesClass($name)) { - return new Type(new \ReflectionClass($cl->loadClass0($name))); + return new Type(new ReflectionClass($cl->loadClass0($name))); } else if ($cl->providesPackage($name)) { return new Package($name); } try { - return new Type(new \ReflectionClass(strtr($arg, '.', '\\'))); - } catch (\ReflectionException $e) { + return new Type(new ReflectionClass(strtr($arg, '.', '\\'))); + } catch (ReflectionException $e) { throw new ClassNotFoundException($name, [$cl]); } } From 9483a5b4d39abf175466eab3ea4a4b460ae3a6be Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Mon, 9 Nov 2026 21:12:01 +0100 Subject: [PATCH 04/29] Add Invocable::name() --- src/main/php/lang/reflection/Invocable.class.php | 9 +++++++++ .../reflection/unittest/InvocableTest.class.php | 13 +++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/main/php/lang/reflection/Invocable.class.php b/src/main/php/lang/reflection/Invocable.class.php index eee2b9b..7ab0e28 100755 --- a/src/main/php/lang/reflection/Invocable.class.php +++ b/src/main/php/lang/reflection/Invocable.class.php @@ -15,6 +15,15 @@ public function __construct($arg) { } } + /** Returns the name, null for anonymous functions */ + public function name(): ?string { + if (PHP_VERSION_ID < 80200 ? strpos($this->reflect->name, '{closure}') : $this->reflect->isAnonymous()) { + return null; + } else { + return $this->reflect->name; + } + } + /** Returns a compound name consisting of `[NAME]()` */ public function compoundName(): string { return $this->reflect->name.'()'; } diff --git a/src/test/php/lang/reflection/unittest/InvocableTest.class.php b/src/test/php/lang/reflection/unittest/InvocableTest.class.php index fa9be64..6024e0a 100755 --- a/src/test/php/lang/reflection/unittest/InvocableTest.class.php +++ b/src/test/php/lang/reflection/unittest/InvocableTest.class.php @@ -9,10 +9,10 @@ class InvocableTest { /** @return iterable */ private function callables() { - yield ['strtoupper']; - yield [fn() => true]; - yield [function() { return true; }]; - yield [new ReflectionFunction('strtoupper')]; + yield ['strtoupper', 'strtoupper']; + yield [fn() => true, null]; + yield [function() { return true; }, null]; + yield [new ReflectionFunction('strtoupper'), 'strtoupper']; } #[Test, Values(from: 'callables')] @@ -20,6 +20,11 @@ public function can_create($callable) { new Invocable($callable); } + #[Test, Values(from: 'callables')] + public function name($callable, $expected) { + Assert::equals($expected, (new Invocable($callable))->name()); + } + #[Test] public function no_comment() { Assert::null((new Invocable(fn() => true))->comment()); From f8dd92d548bdc5b43d3fda83b30ec9a0351d4e30 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Mon, 9 Nov 2026 21:23:25 +0100 Subject: [PATCH 05/29] Allow creating Invocable instances from callables --- src/main/php/lang/reflection/Invocable.class.php | 6 ++++-- .../php/lang/reflection/unittest/InvocableTest.class.php | 5 ++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/main/php/lang/reflection/Invocable.class.php b/src/main/php/lang/reflection/Invocable.class.php index 7ab0e28..6496270 100755 --- a/src/main/php/lang/reflection/Invocable.class.php +++ b/src/main/php/lang/reflection/Invocable.class.php @@ -1,15 +1,17 @@ reflect= $arg; + } else if (is_array($arg)) { + $this->reflect= new ReflectionFunction(Closure::fromCallable($arg)); } else { $this->reflect= new ReflectionFunction($arg); } diff --git a/src/test/php/lang/reflection/unittest/InvocableTest.class.php b/src/test/php/lang/reflection/unittest/InvocableTest.class.php index 6024e0a..d3a4e26 100755 --- a/src/test/php/lang/reflection/unittest/InvocableTest.class.php +++ b/src/test/php/lang/reflection/unittest/InvocableTest.class.php @@ -1,6 +1,6 @@ true, null]; yield [function() { return true; }, null]; + yield [[$this, 'name'], 'name']; + yield [[Closure::class, 'fromCallable'], 'fromCallable']; + yield [Closure::fromCallable('strtoupper'), 'strtoupper']; yield [new ReflectionFunction('strtoupper'), 'strtoupper']; } From 6e7bb7d6742030af2d7b376b3772d25b80a908c6 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Mon, 9 Nov 2026 21:46:19 +0100 Subject: [PATCH 06/29] Add annotation support for invocables --- .../php/lang/meta/MetaInformation.class.php | 2 +- .../php/lang/reflection/Invocable.class.php | 15 +++++++++++++ .../unittest/InvocableTest.class.php | 22 +++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/main/php/lang/meta/MetaInformation.class.php b/src/main/php/lang/meta/MetaInformation.class.php index 57f792c..7d18a02 100755 --- a/src/main/php/lang/meta/MetaInformation.class.php +++ b/src/main/php/lang/meta/MetaInformation.class.php @@ -7,7 +7,7 @@ * it to another source otherwise. */ class MetaInformation { - private $annotations; + public $annotations; public function __construct($annotations) { $this->annotations= $annotations; diff --git a/src/main/php/lang/reflection/Invocable.class.php b/src/main/php/lang/reflection/Invocable.class.php index 6496270..67d5116 100755 --- a/src/main/php/lang/reflection/Invocable.class.php +++ b/src/main/php/lang/reflection/Invocable.class.php @@ -5,6 +5,7 @@ class Invocable implements Target { private $reflect; + private $annotations= null; /** @param callable|ReflectionFunction $arg */ public function __construct($arg) { @@ -41,6 +42,20 @@ public function comment() { ; } + /** @return lang.reflection.Annotations */ + public function annotations() { + $this->annotations ?? $this->annotations= Reflection::meta()->annotations->ofType($this->reflect); + return new Annotations($this->annotations); + } + + /** @return ?lang.reflection.Annotation */ + public function annotation(string $type) { + $this->annotations ?? $this->annotations= Reflection::meta()->annotations->ofType($this->reflect); + + $t= strtr($type, '.', '\\'); + return isset($this->annotations[$t]) ? new Annotation($t, $this->annotations[$t]) : null; + } + /** * Looks up a parameter * diff --git a/src/test/php/lang/reflection/unittest/InvocableTest.class.php b/src/test/php/lang/reflection/unittest/InvocableTest.class.php index d3a4e26..6952446 100755 --- a/src/test/php/lang/reflection/unittest/InvocableTest.class.php +++ b/src/test/php/lang/reflection/unittest/InvocableTest.class.php @@ -38,6 +38,28 @@ public function with_comment() { Assert::equals('Test', (new Invocable(/** Test */ fn() => true))->comment()); } + #[Test] + public function no_annotations() { + Assert::equals([], iterator_to_array((new Invocable(fn() => true))->annotations())); + } + + #[Test] + public function with_annotation() { + Assert::equals( + 'test', + (new Invocable(#[Test] fn() => true))->annotation(Test::class)->name() + ); + } + + #[Test] + public function with_annotations() { + $annotations= []; + foreach ((new Invocable(#[Test, Values] fn() => true))->annotations() as $annotation) { + $annotations[]= $annotation->name(); + } + Assert::equals(['test', 'values'], $annotations); + } + #[Test] public function empty_parameters() { $parameters= (new Invocable(fn() => true))->parameters(); From 7aceae21e1acffac26386a5f20cb95adfb5ffb03 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Mon, 9 Nov 2026 21:53:20 +0100 Subject: [PATCH 07/29] Annotations on invocables raise an exception in PHP 7 --- src/main/php/lang/meta/FromAttributes.class.php | 5 +++++ src/main/php/lang/meta/FromSyntaxTree.class.php | 5 +++++ src/main/php/lang/reflection/Invocable.class.php | 4 ++-- .../php/lang/reflection/unittest/InvocableTest.class.php | 7 ++++--- 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/main/php/lang/meta/FromAttributes.class.php b/src/main/php/lang/meta/FromAttributes.class.php index 4c77402..c973d7b 100755 --- a/src/main/php/lang/meta/FromAttributes.class.php +++ b/src/main/php/lang/meta/FromAttributes.class.php @@ -30,6 +30,11 @@ private function annotations($reflect, $context) { return $r; } + /** @return iterable */ + public function ofInvocable($reflect) { + return $this->annotations($reflect, $reflect); + } + /** @return iterable */ public function ofType($reflect) { return $this->annotations($reflect, $reflect); diff --git a/src/main/php/lang/meta/FromSyntaxTree.class.php b/src/main/php/lang/meta/FromSyntaxTree.class.php index 6a1b0a5..7892d40 100755 --- a/src/main/php/lang/meta/FromSyntaxTree.class.php +++ b/src/main/php/lang/meta/FromSyntaxTree.class.php @@ -198,6 +198,11 @@ public function imports($reflect) { return $imports; } + /** @return iterable */ + public function ofInvocable($reflect) { + throw new IllegalStateException('Invocables cannot have annotations in PHP 7'); + } + /** @return iterable */ public function ofType($reflect) { $tree= $this->tree($reflect, $reflect->getFileName()); diff --git a/src/main/php/lang/reflection/Invocable.class.php b/src/main/php/lang/reflection/Invocable.class.php index 67d5116..e38d46e 100755 --- a/src/main/php/lang/reflection/Invocable.class.php +++ b/src/main/php/lang/reflection/Invocable.class.php @@ -44,13 +44,13 @@ public function comment() { /** @return lang.reflection.Annotations */ public function annotations() { - $this->annotations ?? $this->annotations= Reflection::meta()->annotations->ofType($this->reflect); + $this->annotations ?? $this->annotations= Reflection::meta()->annotations->ofInvocable($this->reflect); return new Annotations($this->annotations); } /** @return ?lang.reflection.Annotation */ public function annotation(string $type) { - $this->annotations ?? $this->annotations= Reflection::meta()->annotations->ofType($this->reflect); + $this->annotations ?? $this->annotations= Reflection::meta()->annotations->ofInvocable($this->reflect); $t= strtr($type, '.', '\\'); return isset($this->annotations[$t]) ? new Annotation($t, $this->annotations[$t]) : null; diff --git a/src/test/php/lang/reflection/unittest/InvocableTest.class.php b/src/test/php/lang/reflection/unittest/InvocableTest.class.php index 6952446..1a419dc 100755 --- a/src/test/php/lang/reflection/unittest/InvocableTest.class.php +++ b/src/test/php/lang/reflection/unittest/InvocableTest.class.php @@ -3,6 +3,7 @@ use Closure, ReflectionFunction; use lang\reflection\{Invocable, CannotInvoke}; use lang\{Type, Primitive}; +use test\verify\Runtime; use test\{Assert, Expect, Test, Values}; class InvocableTest { @@ -38,12 +39,12 @@ public function with_comment() { Assert::equals('Test', (new Invocable(/** Test */ fn() => true))->comment()); } - #[Test] + #[Test, Runtime(php: '>=8.0')] public function no_annotations() { Assert::equals([], iterator_to_array((new Invocable(fn() => true))->annotations())); } - #[Test] + #[Test, Runtime(php: '>=8.0')] public function with_annotation() { Assert::equals( 'test', @@ -51,7 +52,7 @@ public function with_annotation() { ); } - #[Test] + #[Test, Runtime(php: '>=8.0')] public function with_annotations() { $annotations= []; foreach ((new Invocable(#[Test, Values] fn() => true))->annotations() as $annotation) { From 596d3cedeca9dabeb9abf3bfc694a43b3706be13 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Mon, 9 Nov 2026 21:55:46 +0100 Subject: [PATCH 08/29] PHP 7.4 syntax --- .../reflection/unittest/InvocableTest.class.php | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/test/php/lang/reflection/unittest/InvocableTest.class.php b/src/test/php/lang/reflection/unittest/InvocableTest.class.php index 1a419dc..01405b4 100755 --- a/src/test/php/lang/reflection/unittest/InvocableTest.class.php +++ b/src/test/php/lang/reflection/unittest/InvocableTest.class.php @@ -46,16 +46,22 @@ public function no_annotations() { #[Test, Runtime(php: '>=8.0')] public function with_annotation() { - Assert::equals( - 'test', - (new Invocable(#[Test] fn() => true))->annotation(Test::class)->name() - ); + $closure= + #[Test] + fn() => true + ; + Assert::equals('test', (new Invocable($closure))->annotation(Test::class)->name()); } #[Test, Runtime(php: '>=8.0')] public function with_annotations() { + $closure= + #[Test, Values] + fn() => true + ; + $annotations= []; - foreach ((new Invocable(#[Test, Values] fn() => true))->annotations() as $annotation) { + foreach ((new Invocable($closure))->annotations() as $annotation) { $annotations[]= $annotation->name(); } Assert::equals(['test', 'values'], $annotations); From 3bb212d11e12c7ea6722173fa785be37a6c430cb Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Mon, 9 Nov 2026 21:57:53 +0100 Subject: [PATCH 09/29] Wrap closure annotations in `eval()` --- .../lang/reflection/unittest/InvocableTest.class.php | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/test/php/lang/reflection/unittest/InvocableTest.class.php b/src/test/php/lang/reflection/unittest/InvocableTest.class.php index 01405b4..0b3fd0d 100755 --- a/src/test/php/lang/reflection/unittest/InvocableTest.class.php +++ b/src/test/php/lang/reflection/unittest/InvocableTest.class.php @@ -46,19 +46,13 @@ public function no_annotations() { #[Test, Runtime(php: '>=8.0')] public function with_annotation() { - $closure= - #[Test] - fn() => true - ; + $closure= eval('return #[Test] fn() => true;'); Assert::equals('test', (new Invocable($closure))->annotation(Test::class)->name()); } #[Test, Runtime(php: '>=8.0')] public function with_annotations() { - $closure= - #[Test, Values] - fn() => true - ; + $closure= eval('return #[Test, Values] fn() => true;'); $annotations= []; foreach ((new Invocable($closure))->annotations() as $annotation) { From 01cffa2a08d6deedbf03856555a2f803b5f37dca Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Mon, 9 Nov 2026 21:59:15 +0100 Subject: [PATCH 10/29] Fully qualifiy annotation types --- src/test/php/lang/reflection/unittest/InvocableTest.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/php/lang/reflection/unittest/InvocableTest.class.php b/src/test/php/lang/reflection/unittest/InvocableTest.class.php index 0b3fd0d..97588eb 100755 --- a/src/test/php/lang/reflection/unittest/InvocableTest.class.php +++ b/src/test/php/lang/reflection/unittest/InvocableTest.class.php @@ -46,13 +46,13 @@ public function no_annotations() { #[Test, Runtime(php: '>=8.0')] public function with_annotation() { - $closure= eval('return #[Test] fn() => true;'); + $closure= eval('return #[\\test\\Test] fn() => true;'); Assert::equals('test', (new Invocable($closure))->annotation(Test::class)->name()); } #[Test, Runtime(php: '>=8.0')] public function with_annotations() { - $closure= eval('return #[Test, Values] fn() => true;'); + $closure= eval('return #[\\test\\Test, \\test\\Values] fn() => true;'); $annotations= []; foreach ((new Invocable($closure))->annotations() as $annotation) { From dd21db6840a3d345342822e092302f245343ebb1 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sat, 14 Nov 2026 12:36:35 +0100 Subject: [PATCH 11/29] Add Method::invocable() --- .../php/lang/reflection/Invocable.class.php | 18 ++++------- src/main/php/lang/reflection/Method.class.php | 13 +++++++- .../reflection/unittest/MethodsTest.class.php | 32 +++++++++++++++++++ 3 files changed, 50 insertions(+), 13 deletions(-) diff --git a/src/main/php/lang/reflection/Invocable.class.php b/src/main/php/lang/reflection/Invocable.class.php index e38d46e..5717959 100755 --- a/src/main/php/lang/reflection/Invocable.class.php +++ b/src/main/php/lang/reflection/Invocable.class.php @@ -76,21 +76,15 @@ public function parameter($arg) { return null === $p ? null : new Parameter($p, $this->reflect); } - /** - * Returns all parameters - * - * @return lang.reflection.Parameters - */ - public function parameters(): Parameters { - return new Parameters($this->reflect); - } + /** Returns all parameters */ + public function parameters(): Parameters { return new Parameters($this->reflect); } /** @return lang.reflection.Constraint */ public function returns() { $present= true; - $absent= function($set) use(&$present) { $present= false; }; - - $t= Type::resolve($this->reflect->getReturnType(), [], $absent); + $t= Type::resolve($this->reflect->getReturnType(), [], function($set) use(&$present) { + $present= false; + }); return new Constraint($t ?? Type::$VAR, $present); } @@ -102,7 +96,7 @@ public function returns() { * @throws lang.reflection.CannotInvoke if prerequisites to the invocation fail * @throws lang.reflection.InvocationFailed if invocation raises an exception */ - public function invoke($args= []) { + public function invoke(array $args= []) { try { $pass= PHP_VERSION_ID < 80000 && $args ? Routine::pass($this->reflect, $args) : $args; return $this->reflect->invokeArgs($pass); diff --git a/src/main/php/lang/reflection/Method.class.php b/src/main/php/lang/reflection/Method.class.php index cfe43da..3a0257d 100755 --- a/src/main/php/lang/reflection/Method.class.php +++ b/src/main/php/lang/reflection/Method.class.php @@ -21,7 +21,7 @@ class Method extends Routine { public function closure(?object $instance= null) { try { $closure= $this->reflect->getClosure($instance); - } catch (\Throwable $e) { + } catch (Throwable $e) { throw new IllegalArgumentException($e->getMessage()); } @@ -39,6 +39,17 @@ public function closure(?object $instance= null) { return $closure; } + /** + * Returns an invocable + * + * @param ?object $instance + * @return lang.reflection.Invocable + * @throws lang.IllegalArgumentException for incorrect or missing instances + */ + public function invocable(?object $instance= null) { + return new Invocable($this->closure($instance)); + } + /** * Invokes this method * diff --git a/src/test/php/lang/reflection/unittest/MethodsTest.class.php b/src/test/php/lang/reflection/unittest/MethodsTest.class.php index f1bccc2..b8a835f 100755 --- a/src/test/php/lang/reflection/unittest/MethodsTest.class.php +++ b/src/test/php/lang/reflection/unittest/MethodsTest.class.php @@ -206,6 +206,22 @@ public function fixture() { } Assert::equals("Returns the fixture\nor NULL.", $t->method('fixture')->comment()); } + #[Test] + public function closure() { + $t= $this->declare('{ public function fixture($name) { return "@".strtolower($name); } }'); + $closure= $t->method('fixture')->closure($t->newinstance()); + + Assert::equals('@test', $closure('Test')); + } + + #[Test] + public function static_closure() { + $t= $this->declare('{ public static function fixture($name) { return "@".strtolower($name); } }'); + $closure= $t->method('fixture')->closure(null); + + Assert::equals('@test', $closure('Test')); + } + #[Test, Expect(IllegalArgumentException::class)] public function closure_with_missing_instance() { $this->declare('{ public function fixture() { } }')->method('fixture')->closure(null); @@ -216,6 +232,22 @@ public function closure_with_incorrect_instance() { $this->declare('{ public function fixture() { } }')->method('fixture')->closure($this); } + #[Test] + public function invocable() { + $t= $this->declare('{ public function fixture($name) { return "@".strtolower($name); } }'); + $invocable= $t->method('fixture')->invocable($t->newinstance()); + + Assert::equals('@test', $invocable->invoke(['Test'])); + } + + #[Test] + public function static_invocable() { + $t= $this->declare('{ public static function fixture($name) { return "@".strtolower($name); } }'); + $invocable= $t->method('fixture')->invocable(null); + + Assert::equals('@test', $invocable->invoke(['Test'])); + } + #[Test] public function hash_code() { $t= $this->declare('{ public function fixture() { } }'); From 00bd816e1d569b0ce561dbbbfd3741f7b1b42274 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sat, 14 Nov 2026 13:27:32 +0100 Subject: [PATCH 12/29] Add Reflection::invocable() --- src/main/php/lang/Reflection.class.php | 12 ++++++++++- .../unittest/ReflectionTest.class.php | 20 +++++++++++++++---- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/main/php/lang/Reflection.class.php b/src/main/php/lang/Reflection.class.php index 392c980..9907714 100755 --- a/src/main/php/lang/Reflection.class.php +++ b/src/main/php/lang/Reflection.class.php @@ -2,7 +2,7 @@ use ReflectionClass, ReflectionObject, ReflectionException; use lang\meta\{MetaInformation, FromSyntaxTree, FromAttributes}; -use lang\reflection\{Type, Package}; +use lang\reflection\{Invocable, Type, Package}; use lang\{ClassLoader, ClassNotFoundException, IllegalArgumentException}; /** @@ -32,6 +32,16 @@ public static function meta(): MetaInformation { return self::$meta ?? self::$meta= new MetaInformation(self::annotations(PHP_VERSION_ID)); } + /** + * Returns a reflection type for a given argument. + * + * @param string|callable|lang.reflection.Invocable $arg + * @return lang.reflection.Invocable + */ + public static function invocable($arg) { + return $arg instanceof Invocable ? $arg : new Invocable($arg); + } + /** * Returns a reflection type for a given argument. * diff --git a/src/test/php/lang/reflection/unittest/ReflectionTest.class.php b/src/test/php/lang/reflection/unittest/ReflectionTest.class.php index 6b58312..1d44229 100755 --- a/src/test/php/lang/reflection/unittest/ReflectionTest.class.php +++ b/src/test/php/lang/reflection/unittest/ReflectionTest.class.php @@ -2,14 +2,21 @@ use ReflectionClass, ReflectionObject; use lang\meta\{FromAttributes, FromSyntaxTree}; -use lang\reflection\Package; +use lang\reflection\{Invocable, Package}; use lang\{ClassNotFoundException, Reflection, Type}; use test\{Assert, Expect, Test, Values}; class ReflectionTest { /** @return iterable */ - private function arguments() { + private function invocables() { + yield ['strlen', 'global function']; + yield [fn() => true, 'short closure']; + yield [function() { }, 'closure']; + } + + /** @return iterable */ + private function types() { yield [self::class, 'class literal']; yield [nameof($this), 'class name']; yield [$this, 'instance']; @@ -32,12 +39,17 @@ private function packages() { yield [new ReflectionObject($this), 'reflection object']; } - #[Test, Values(from: 'arguments')] + #[Test, Values(from: 'types')] public function of($argument) { Assert::equals(nameof($this), Reflection::of($argument)->name()); } - #[Test, Values(from: 'arguments')] + #[Test, Values(from: 'invocables')] + public function invocable($argument) { + Assert::instance(Invocable::class, Reflection::invocable($argument)); + } + + #[Test, Values(from: 'types')] public function type($argument) { Assert::equals(nameof($this), Reflection::type($argument)->name()); } From 78a5137eaf59350a3edfbd667f615e52bc066c63 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sat, 14 Nov 2026 13:33:53 +0100 Subject: [PATCH 13/29] Make parameters work with both functions and methods --- .../php/lang/meta/FromAttributes.class.php | 4 +- .../php/lang/reflection/Parameter.class.php | 42 +++++++++++++------ .../php/lang/reflection/Parameters.class.php | 32 +++++++------- 3 files changed, 47 insertions(+), 31 deletions(-) diff --git a/src/main/php/lang/meta/FromAttributes.class.php b/src/main/php/lang/meta/FromAttributes.class.php index c973d7b..50ed950 100755 --- a/src/main/php/lang/meta/FromAttributes.class.php +++ b/src/main/php/lang/meta/FromAttributes.class.php @@ -1,6 +1,6 @@ annotations($reflect, $method->getDeclaringClass()); + return $this->annotations($reflect, $method instanceof ReflectionMethod ? $method->getDeclaringClass() : $method); } /** diff --git a/src/main/php/lang/reflection/Parameter.class.php b/src/main/php/lang/reflection/Parameter.class.php index 43d560f..7b40762 100755 --- a/src/main/php/lang/reflection/Parameter.class.php +++ b/src/main/php/lang/reflection/Parameter.class.php @@ -1,5 +1,6 @@ reflect= $reflect; - $this->method= $method ?? $reflect->getDeclaringFunction(); + $this->holder= $holder ?? $reflect->getDeclaringFunction(); } /** Returns parameter name */ @@ -45,7 +46,10 @@ public function default() { if ($this->reflect->isDefaultValueAvailable()) { $value= $this->reflect->getDefaultValue(); if (null === $value) { - $this->annotations ?? $this->annotations= Reflection::meta()->parameterAnnotations($this->method, $this->reflect); + $this->annotations ?? $this->annotations= $this->holder instanceof ReflectionMethod + ? Reflection::meta()->parameterAnnotations($this->holder, $this->reflect) + : Reflection::meta()->annotations->ofParameter($this->holder, $this->reflect) + ; return $this->annotations['Default'][0] ?? null; } return $value; @@ -56,13 +60,19 @@ public function default() { /** @return lang.reflection.Annotations */ public function annotations() { - $this->annotations ?? $this->annotations= Reflection::meta()->parameterAnnotations($this->method, $this->reflect); + $this->annotations ?? $this->annotations= $this->holder instanceof ReflectionMethod + ? Reflection::meta()->parameterAnnotations($this->holder, $this->reflect) + : Reflection::meta()->annotations->ofParameter($this->holder, $this->reflect) + ; return new Annotations($this->annotations); } /** @return ?lang.reflection.Annotation */ public function annotation(string $type) { - $this->annotations ?? $this->annotations= Reflection::meta()->parameterAnnotations($this->method, $this->reflect); + $this->annotations ?? $this->annotations= $this->holder instanceof ReflectionMethod + ? Reflection::meta()->parameterAnnotations($this->holder, $this->reflect) + : Reflection::meta()->annotations->ofParameter($this->holder, $this->reflect) + ; $t= strtr($type, '.', '\\'); return isset($this->annotations[$t]) ? new Annotation($t, $this->annotations[$t]) : null; @@ -73,14 +83,20 @@ public function constraint() { $present= true; // Only use meta information if necessary - $api= function($set) use(&$present, &$names) { - $present= $set; - $names= Reflection::meta()->methodParameterTypes($this->method); - return $names[$this->reflect->getPosition()] ?? null; - }; + if ($this->holder instanceof ReflectionMethod) { + $resolve= Member::resolve($this->reflect); + $api= function($set) use(&$present) { + $present= $set; + $names= Reflection::meta()->methodParameterTypes($this->holder); + return $names[$this->reflect->getPosition()] ?? null; + }; + } else { + $resolve= []; + $api= function($set) use(&$present) { $present= $set; return null; }; + } return new Constraint( - Type::resolve($this->reflect->getType(), Member::resolve($this->reflect), $api) ?? Type::$VAR, + Type::resolve($this->reflect->getType(), $resolve, $api) ?? Type::$VAR, $present ); } diff --git a/src/main/php/lang/reflection/Parameters.class.php b/src/main/php/lang/reflection/Parameters.class.php index 66d6f8b..be7e8d0 100755 --- a/src/main/php/lang/reflection/Parameters.class.php +++ b/src/main/php/lang/reflection/Parameters.class.php @@ -9,11 +9,11 @@ * @test lang.reflection.unittest.MethodsTest */ class Parameters implements \IteratorAggregate { - private $method; + private $holder; - /** @param ReflectionFunctionAbstract $method */ - public function __construct($method) { - $this->method= $method; + /** @param ReflectionFunctionAbstract $holder */ + public function __construct($holder) { + $this->holder= $holder; } /** @@ -23,7 +23,7 @@ public function __construct($method) { * @return int */ public function size($required= false) { - return $required ? $this->method->getNumberOfRequiredParameters() : $this->method->getNumberOfParameters(); + return $required ? $this->holder->getNumberOfRequiredParameters() : $this->holder->getNumberOfParameters(); } /** @@ -33,8 +33,8 @@ public function size($required= false) { * @return ?lang.reflection.Parameter */ public function at(int $position) { - $list= $this->method->getParameters(); - return isset($list[$position]) ? new Parameter($list[$position], $this->method) : null; + $list= $this->holder->getParameters(); + return isset($list[$position]) ? new Parameter($list[$position], $this->holder) : null; } /** @@ -44,22 +44,22 @@ public function at(int $position) { * @return ?lang.reflection.Parameter */ public function named(string $name) { - foreach ($this->method->getParameters() as $param) { - if ($name === $param->name) return new Parameter($param, $this->method); + foreach ($this->holder->getParameters() as $param) { + if ($name === $param->name) return new Parameter($param, $this->holder); } return null; } /** @return ?lang.reflection.Parameter */ public function first() { - $list= $this->method->getParameters(); - return $list ? new Parameter($list[0], $this->method) : null; + $list= $this->holder->getParameters(); + return $list ? new Parameter($list[0], $this->holder) : null; } /** @return iterable */ public function getIterator(): Traversable { - foreach ($this->method->getParameters() as $parameter) { - yield $parameter->name => new Parameter($parameter, $this->method); + foreach ($this->holder->getParameters() as $parameter) { + yield $parameter->name => new Parameter($parameter, $this->holder); } } @@ -73,16 +73,16 @@ public function getIterator(): Traversable { * @return bool */ public function accept(array $arguments, $count= null): bool { - $parameters= $this->method->getParameters(); + $parameters= $this->holder->getParameters(); if (null !== $count && $count !== sizeof($parameters)) return false; // Only fetch api doc types if necessary $api= function() use(&$i, &$types) { - $types ?? $types= Reflection::meta()->methodParameterTypes($this->method); + $types ?? $types= Reflection::meta()->methodParameterTypes($this->holder); return $types[$i] ?? null; }; - $context= Member::resolve($this->method); + $context= Member::resolve($this->holder); foreach ($parameters as $i => $parameter) { // If a given value is missing check whether parameter is optional From 5bcbd1db6a76faacee20b9f33ca4d59f64066054 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sat, 14 Nov 2026 13:45:31 +0100 Subject: [PATCH 14/29] Add package() --- src/main/php/lang/reflection/Invocable.class.php | 9 +++++++++ .../lang/reflection/unittest/InvocableTest.class.php | 12 +++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/main/php/lang/reflection/Invocable.class.php b/src/main/php/lang/reflection/Invocable.class.php index 5717959..3f3ea3b 100755 --- a/src/main/php/lang/reflection/Invocable.class.php +++ b/src/main/php/lang/reflection/Invocable.class.php @@ -30,6 +30,15 @@ public function name(): ?string { /** Returns a compound name consisting of `[NAME]()` */ public function compoundName(): string { return $this->reflect->name.'()'; } + /** + * Returns this type's package + * + * @return lang.reflection.Package + */ + public function package() { + return new Package($this->reflect->getNamespaceName()); + } + /** * Returns this functions's doc comment, or NULL if there is none. * diff --git a/src/test/php/lang/reflection/unittest/InvocableTest.class.php b/src/test/php/lang/reflection/unittest/InvocableTest.class.php index 97588eb..aa26af9 100755 --- a/src/test/php/lang/reflection/unittest/InvocableTest.class.php +++ b/src/test/php/lang/reflection/unittest/InvocableTest.class.php @@ -1,7 +1,7 @@ name()); } + #[Test] + public function global_function() { + Assert::equals(new Package(''), (new Invocable('strlen'))->package()); + } + + #[Test] + public function namespaced_function() { + Assert::equals(new Package(__NAMESPACE__), (new Invocable(fn() => true))->package()); + } + #[Test] public function no_comment() { Assert::null((new Invocable(fn() => true))->comment()); From 67ddd8a6231a0ee1a2323e8d40420e75a6016037 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sat, 14 Nov 2026 14:04:45 +0100 Subject: [PATCH 15/29] Consistently make anonymous invocables return NULL for their name --- .../php/lang/reflection/Invocable.class.php | 16 +++++++++---- .../unittest/InvocableTest.class.php | 24 +++++++++++++++++-- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/src/main/php/lang/reflection/Invocable.class.php b/src/main/php/lang/reflection/Invocable.class.php index 3f3ea3b..38fa75b 100755 --- a/src/main/php/lang/reflection/Invocable.class.php +++ b/src/main/php/lang/reflection/Invocable.class.php @@ -23,20 +23,28 @@ public function name(): ?string { if (PHP_VERSION_ID < 80200 ? strpos($this->reflect->name, '{closure}') : $this->reflect->isAnonymous()) { return null; } else { - return $this->reflect->name; + return strtr($this->reflect->name, '\\', '.'); } } /** Returns a compound name consisting of `[NAME]()` */ - public function compoundName(): string { return $this->reflect->name.'()'; } + public function compoundName(): string { return $this->name().'()'; } /** - * Returns this type's package + * Returns this invocable's package. Anonymous functions are always in the + * top-level package * * @return lang.reflection.Package */ public function package() { - return new Package($this->reflect->getNamespaceName()); + if (PHP_VERSION_ID < 80200) { + $ns= false === strpos($this->reflect->name, '{closure}') ? $this->reflect->getNamespaceName() : ''; + } else if (PHP_VERSION_ID < 80400) { + $ns= $this->reflect->isAnonymous() ? '' : $this->reflect->getNamespaceName(); + } else { + $ns= $this->reflect->getNamespaceName(); + } + return new Package($ns); } /** diff --git a/src/test/php/lang/reflection/unittest/InvocableTest.class.php b/src/test/php/lang/reflection/unittest/InvocableTest.class.php index aa26af9..2a40a8d 100755 --- a/src/test/php/lang/reflection/unittest/InvocableTest.class.php +++ b/src/test/php/lang/reflection/unittest/InvocableTest.class.php @@ -29,14 +29,34 @@ public function name($callable, $expected) { Assert::equals($expected, (new Invocable($callable))->name()); } + #[Test] + public function compound_name() { + Assert::equals('strlen()', (new Invocable('strlen'))->compoundName()); + } + #[Test] public function global_function() { - Assert::equals(new Package(''), (new Invocable('strlen'))->package()); + $strlen= new Invocable('strlen'); + + Assert::equals('strlen', $strlen->name()); + Assert::equals(new Package(''), $strlen->package()); + } + + #[Test] + public function anonymous_function() { + $anonymous= new Invocable(fn() => true); + + Assert::null($anonymous->name()); + Assert::equals(new Package(''), $anonymous->package()); } #[Test] public function namespaced_function() { - Assert::equals(new Package(__NAMESPACE__), (new Invocable(fn() => true))->package()); + eval('namespace test; function fixture() { }'); + $namespaced= new Invocable('test\\fixture'); + + Assert::equals('test.fixture', $namespaced->name()); + Assert::equals(new Package('test'), $namespaced->package()); } #[Test] From 16f21bb0f91cfed132f52e01c39107729fefd3db Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sat, 14 Nov 2026 14:30:57 +0100 Subject: [PATCH 16/29] Implement Annotated and Value --- .../php/lang/reflection/Invocable.class.php | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/main/php/lang/reflection/Invocable.class.php b/src/main/php/lang/reflection/Invocable.class.php index 38fa75b..f56faaa 100755 --- a/src/main/php/lang/reflection/Invocable.class.php +++ b/src/main/php/lang/reflection/Invocable.class.php @@ -1,9 +1,9 @@ reflect->name); } + + /** @return string */ + public function toString() { return nameof($this).'<'.$this->name().'>'; } + + /** + * Compares this member to another value + * + * @param var $value + * @return int + */ + public function compareTo($value) { + return $value instanceof self + ? $this->reflect->name <=> $value->reflect->name + : 1 + ; + } } \ No newline at end of file From 4dd025632e821500cd361d8cee63a7df1ea5dd65 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sat, 14 Nov 2026 15:58:58 +0100 Subject: [PATCH 17/29] Add Invocation::literal() and ::declaredName() Consistency with lang.reflection.Type --- .../php/lang/reflection/Invocable.class.php | 18 ++++++++++++++++++ .../unittest/InvocableTest.class.php | 6 ++++++ 2 files changed, 24 insertions(+) diff --git a/src/main/php/lang/reflection/Invocable.class.php b/src/main/php/lang/reflection/Invocable.class.php index f56faaa..ce26260 100755 --- a/src/main/php/lang/reflection/Invocable.class.php +++ b/src/main/php/lang/reflection/Invocable.class.php @@ -27,6 +27,24 @@ public function name(): ?string { } } + /** Returns type literal (in namespaced form) */ + public function literal(): ?string { + if (PHP_VERSION_ID < 80200 ? strpos($this->reflect->name, '{closure}') : $this->reflect->isAnonymous()) { + return null; + } else { + return $this->reflect->name; + } + } + + /** Returns declared name (without namespace) */ + public function declaredName(): ?string { + if (PHP_VERSION_ID < 80200 ? strpos($this->reflect->name, '{closure}') : $this->reflect->isAnonymous()) { + return null; + } else { + return $this->reflect->getShortName(); + } + } + /** Returns a compound name consisting of `[NAME]()` */ public function compoundName(): string { return $this->name().'()'; } diff --git a/src/test/php/lang/reflection/unittest/InvocableTest.class.php b/src/test/php/lang/reflection/unittest/InvocableTest.class.php index 2a40a8d..9be61c2 100755 --- a/src/test/php/lang/reflection/unittest/InvocableTest.class.php +++ b/src/test/php/lang/reflection/unittest/InvocableTest.class.php @@ -39,6 +39,8 @@ public function global_function() { $strlen= new Invocable('strlen'); Assert::equals('strlen', $strlen->name()); + Assert::equals('strlen', $strlen->literal()); + Assert::equals('strlen', $strlen->declaredName()); Assert::equals(new Package(''), $strlen->package()); } @@ -47,6 +49,8 @@ public function anonymous_function() { $anonymous= new Invocable(fn() => true); Assert::null($anonymous->name()); + Assert::null($anonymous->literal()); + Assert::null($anonymous->declaredName()); Assert::equals(new Package(''), $anonymous->package()); } @@ -56,6 +60,8 @@ public function namespaced_function() { $namespaced= new Invocable('test\\fixture'); Assert::equals('test.fixture', $namespaced->name()); + Assert::equals('test\\fixture', $namespaced->literal()); + Assert::equals('fixture', $namespaced->declaredName()); Assert::equals(new Package('test'), $namespaced->package()); } From 9ae247c70be0ae3c86cdf9a87662371f5058edaa Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sat, 14 Nov 2026 16:47:29 +0100 Subject: [PATCH 18/29] Make it more efficient to create invocables from methods --- .../php/lang/reflection/Invocable.class.php | 21 +++++++++++++------ src/main/php/lang/reflection/Method.class.php | 2 +- .../reflection/unittest/MethodsTest.class.php | 2 +- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/main/php/lang/reflection/Invocable.class.php b/src/main/php/lang/reflection/Invocable.class.php index ce26260..a754cf4 100755 --- a/src/main/php/lang/reflection/Invocable.class.php +++ b/src/main/php/lang/reflection/Invocable.class.php @@ -1,21 +1,27 @@ reflect= $arg; } else if (is_array($arg)) { $this->reflect= new ReflectionFunction(Closure::fromCallable($arg)); } else { $this->reflect= new ReflectionFunction($arg); } + $this->receiver= $receiver; } /** Returns the name, null for anonymous functions */ @@ -134,7 +140,10 @@ public function returns() { public function invoke(array $args= []) { try { $pass= PHP_VERSION_ID < 80000 && $args ? Routine::pass($this->reflect, $args) : $args; - return $this->reflect->invokeArgs($pass); + return $this->reflect instanceof ReflectionFunction + ? $this->reflect->invokeArgs($pass) + : $this->reflect->invokeArgs($this->receiver, $pass) + ; } catch (ReflectionException|ArgumentCountError|TypeError $e) { throw new CannotInvoke($this, $e); } catch (Throwable $e) { diff --git a/src/main/php/lang/reflection/Method.class.php b/src/main/php/lang/reflection/Method.class.php index 3a0257d..11b6e64 100755 --- a/src/main/php/lang/reflection/Method.class.php +++ b/src/main/php/lang/reflection/Method.class.php @@ -47,7 +47,7 @@ public function closure(?object $instance= null) { * @throws lang.IllegalArgumentException for incorrect or missing instances */ public function invocable(?object $instance= null) { - return new Invocable($this->closure($instance)); + return new Invocable($this->reflect, $instance); } /** diff --git a/src/test/php/lang/reflection/unittest/MethodsTest.class.php b/src/test/php/lang/reflection/unittest/MethodsTest.class.php index b8a835f..9e4a632 100755 --- a/src/test/php/lang/reflection/unittest/MethodsTest.class.php +++ b/src/test/php/lang/reflection/unittest/MethodsTest.class.php @@ -235,7 +235,7 @@ public function closure_with_incorrect_instance() { #[Test] public function invocable() { $t= $this->declare('{ public function fixture($name) { return "@".strtolower($name); } }'); - $invocable= $t->method('fixture')->invocable($t->newinstance()); + $invocable= $t->method('fixture')->invocable($t->newInstance()); Assert::equals('@test', $invocable->invoke(['Test'])); } From 83b0166cb3d9e49389e6fea2b20b1e53bd38619c Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sat, 14 Nov 2026 16:58:17 +0100 Subject: [PATCH 19/29] Verify instance --- src/main/php/lang/reflection/Method.class.php | 6 ++++++ .../php/lang/reflection/unittest/MethodsTest.class.php | 10 ++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/main/php/lang/reflection/Method.class.php b/src/main/php/lang/reflection/Method.class.php index 11b6e64..fdb97d4 100755 --- a/src/main/php/lang/reflection/Method.class.php +++ b/src/main/php/lang/reflection/Method.class.php @@ -47,6 +47,12 @@ public function closure(?object $instance= null) { * @throws lang.IllegalArgumentException for incorrect or missing instances */ public function invocable(?object $instance= null) { + if ($instance && !($instance instanceof $this->reflect->class)) { + throw new IllegalArgumentException('Instance is not an instance of the class method was declared in'); + } else if (null === $instance && !$this->reflect->isStatic()) { + throw new IllegalArgumentException('Instance cannot be null for non-static methods'); + } + return new Invocable($this->reflect, $instance); } diff --git a/src/test/php/lang/reflection/unittest/MethodsTest.class.php b/src/test/php/lang/reflection/unittest/MethodsTest.class.php index 9e4a632..e3a8371 100755 --- a/src/test/php/lang/reflection/unittest/MethodsTest.class.php +++ b/src/test/php/lang/reflection/unittest/MethodsTest.class.php @@ -248,6 +248,16 @@ public function static_invocable() { Assert::equals('@test', $invocable->invoke(['Test'])); } + #[Test, Expect(IllegalArgumentException::class)] + public function invocable_with_missing_instance() { + $this->declare('{ public function fixture() { } }')->method('fixture')->invocable(null); + } + + #[Test, Expect(IllegalArgumentException::class)] + public function invocable_with_incorrect_instance() { + $this->declare('{ public function fixture() { } }')->method('fixture')->invocable($this); + } + #[Test] public function hash_code() { $t= $this->declare('{ public function fixture() { } }'); From d7d9669131a91e24098c13d620a2e8a2e3e4e4a8 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sat, 14 Nov 2026 17:04:36 +0100 Subject: [PATCH 20/29] Make Reflection::of() accept closures and reflection functions --- src/main/php/lang/Reflection.class.php | 10 ++++++---- .../reflection/unittest/ReflectionTest.class.php | 12 +++++++++++- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/main/php/lang/Reflection.class.php b/src/main/php/lang/Reflection.class.php index 9907714..4756b95 100755 --- a/src/main/php/lang/Reflection.class.php +++ b/src/main/php/lang/Reflection.class.php @@ -1,6 +1,6 @@ reflect()->getNamespaceName()); - } else if ($arg instanceof \ReflectionClass) { + } else if ($arg instanceof ReflectionClass) { return new Package($arg->getNamespaceName()); } else if ($arg instanceof Type) { return $arg->package(); @@ -100,17 +100,19 @@ public static function package($arg) { * Creates a new reflection instance, which may either refer to a type * or to a package. * - * @param string|object|lang.XPClass|lang.reflection.Type|ReflectionClass $arg + * @param string|Closure|lang.XPClass|lang.reflection.Type|ReflectionClass|ReflectionFunction $arg * @return lang.reflection.Type|lang.reflection.Package * @throws lang.ClassNotFoundException */ public static function of($arg) { if ($arg instanceof XPClass) { return new Type($arg->reflect()); - } else if ($arg instanceof \ReflectionClass) { + } else if ($arg instanceof ReflectionClass) { return new Type($arg); } else if ($arg instanceof Type) { return $arg; + } else if ($arg instanceof Closure || $arg instanceof ReflectionFunction) { + return new Invocable($arg); } else if (is_object($arg)) { return new Type(new ReflectionObject($arg)); } else { diff --git a/src/test/php/lang/reflection/unittest/ReflectionTest.class.php b/src/test/php/lang/reflection/unittest/ReflectionTest.class.php index 1d44229..af99ee6 100755 --- a/src/test/php/lang/reflection/unittest/ReflectionTest.class.php +++ b/src/test/php/lang/reflection/unittest/ReflectionTest.class.php @@ -1,6 +1,6 @@ true)); + } + + #[Test] + public function of_function() { + Assert::instance(Invocable::class, Reflection::of(new ReflectionFunction('strlen'))); + } + #[Test] public function of_package_name() { Assert::instance(Package::class, Reflection::of('lang.reflection.unittest')); From d125652cb9d087284ec8a1bca1d093c67b0b83a9 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sat, 14 Nov 2026 17:15:06 +0100 Subject: [PATCH 21/29] Make Reflection::of() accept closures, invocables and reflection functions --- src/main/php/lang/Reflection.class.php | 21 ++++++++-------- .../unittest/ReflectionTest.class.php | 25 +++++++++++++++---- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/main/php/lang/Reflection.class.php b/src/main/php/lang/Reflection.class.php index 4756b95..fc63c31 100755 --- a/src/main/php/lang/Reflection.class.php +++ b/src/main/php/lang/Reflection.class.php @@ -23,6 +23,7 @@ abstract class Reflection { private static $meta= null; + /** Returns annotation parser */ public static function annotations($version) { return $version >= 80000 ? new FromAttributes() : new FromSyntaxTree(); } @@ -35,7 +36,7 @@ public static function meta(): MetaInformation { /** * Returns a reflection type for a given argument. * - * @param string|callable|lang.reflection.Invocable $arg + * @param string|callable $arg * @return lang.reflection.Invocable */ public static function invocable($arg) { @@ -45,14 +46,14 @@ public static function invocable($arg) { /** * Returns a reflection type for a given argument. * - * @param string|object|lang.XPClass|lang.reflection.Type|ReflectionClass $arg + * @param string|object $arg * @return lang.reflection.Type * @throws lang.ClassNotFoundException */ public static function type($arg) { if ($arg instanceof XPClass) { return new Type($arg->reflect()); - } else if ($arg instanceof \ReflectionClass) { + } else if ($arg instanceof ReflectionClass) { return new Type($arg); } else if ($arg instanceof Type) { return $arg; @@ -70,16 +71,16 @@ public static function type($arg) { /** * Returns a reflection package for a given argument. * - * @param string|object|lang.XPClass|lang.reflection.Type|ReflectionClass $arg + * @param string|object $arg * @return lang.reflection.Type * @throws lang.IllegalArgumentException */ public static function package($arg) { if ($arg instanceof XPClass) { return new Package($arg->reflect()->getNamespaceName()); - } else if ($arg instanceof ReflectionClass) { + } else if ($arg instanceof ReflectionClass || $arg instanceof ReflectionFunction) { return new Package($arg->getNamespaceName()); - } else if ($arg instanceof Type) { + } else if ($arg instanceof Type || $arg instanceof Invocable) { return $arg->package(); } else if (is_object($arg)) { $class= get_class($arg); @@ -97,11 +98,11 @@ public static function package($arg) { } /** - * Creates a new reflection instance, which may either refer to a type - * or to a package. + * Creates a new reflection instance, which may either refer to an invocable, + * a type or to a package. * - * @param string|Closure|lang.XPClass|lang.reflection.Type|ReflectionClass|ReflectionFunction $arg - * @return lang.reflection.Type|lang.reflection.Package + * @param string|object $arg + * @return lang.reflection.Invocable|lang.reflection.Type|lang.reflection.Package * @throws lang.ClassNotFoundException */ public static function of($arg) { diff --git a/src/test/php/lang/reflection/unittest/ReflectionTest.class.php b/src/test/php/lang/reflection/unittest/ReflectionTest.class.php index af99ee6..368c3b1 100755 --- a/src/test/php/lang/reflection/unittest/ReflectionTest.class.php +++ b/src/test/php/lang/reflection/unittest/ReflectionTest.class.php @@ -39,11 +39,6 @@ private function packages() { yield [new ReflectionObject($this), 'reflection object']; } - #[Test, Values(from: 'types')] - public function of($argument) { - Assert::equals(nameof($this), Reflection::of($argument)->name()); - } - #[Test, Values(from: 'invocables')] public function invocable($argument) { Assert::instance(Invocable::class, Reflection::invocable($argument)); @@ -59,6 +54,26 @@ public function package($argument) { Assert::equals(new Package('lang.reflection.unittest'), Reflection::package($argument)); } + #[Test] + public function package_of_closure() { + Assert::equals(new Package(''), Reflection::package(fn() => true)); + } + + #[Test] + public function package_of_invocable() { + Assert::equals(new Package(''), Reflection::package(Reflection::invocable('strlen'))); + } + + #[Test] + public function package_of_function() { + Assert::equals(new Package(''), Reflection::package(new ReflectionFunction('strlen'))); + } + + #[Test, Values(from: 'types')] + public function of($argument) { + Assert::equals(nameof($this), Reflection::of($argument)->name()); + } + #[Test] public function of_closure() { Assert::instance(Invocable::class, Reflection::of(fn() => true)); From 6ac08589d62e76264237a8d4d2e6d261ed055860 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sat, 14 Nov 2026 17:25:43 +0100 Subject: [PATCH 22/29] Make anonymous classes and invocables consistent --- src/main/php/lang/reflection/Type.class.php | 12 +++++++++--- .../reflection/unittest/AnonymousClassTest.class.php | 12 +++++++++++- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/main/php/lang/reflection/Type.class.php b/src/main/php/lang/reflection/Type.class.php index e8b9427..1352594 100755 --- a/src/main/php/lang/reflection/Type.class.php +++ b/src/main/php/lang/reflection/Type.class.php @@ -18,13 +18,19 @@ public function __construct($reflect) { } /** Returns type name (in dotted form) */ - public function name(): string { return strtr($this->reflect->name, '\\', '.'); } + public function name(): ?string { + return $this->reflect->isAnonymous() ? null : strtr($this->reflect->name, '\\', '.'); + } /** Returns declared name (without namespace) */ - public function declaredName(): string { return $this->reflect->getShortName(); } + public function declaredName(): ?string { + return $this->reflect->isAnonymous() ? null : $this->reflect->getShortName(); + } /** Returns type literal (in namespaced form) */ - public function literal(): string { return $this->reflect->name; } + public function literal(): ?string { + return $this->reflect->isAnonymous() ? null : $this->reflect->name; + } /** Returns corresponding lang.XPClass instance */ public function class(): XPClass { return new XPClass($this->reflect); } diff --git a/src/test/php/lang/reflection/unittest/AnonymousClassTest.class.php b/src/test/php/lang/reflection/unittest/AnonymousClassTest.class.php index 7e5205e..af60b69 100755 --- a/src/test/php/lang/reflection/unittest/AnonymousClassTest.class.php +++ b/src/test/php/lang/reflection/unittest/AnonymousClassTest.class.php @@ -7,7 +7,17 @@ class AnonymousClassTest { #[Test] public function name() { - Assert::equals('class@anonymous', strstr(Reflection::type(new class() { })->name(), "\0", true)); + Assert::null(Reflection::type(new class() { })->name()); + } + + #[Test] + public function literal() { + Assert::null(Reflection::type(new class() { })->literal()); + } + + #[Test] + public function declared_name() { + Assert::null(Reflection::type(new class() { })->declaredName()); } /** @see https://github.com/xp-framework/reflection/issues/33 */ From 6e6e518630c005c39293a5199f4e506cdc4c513f Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sat, 14 Nov 2026 18:16:41 +0100 Subject: [PATCH 23/29] Use setAccessible() to invoke private method references --- .../php/lang/reflection/Invocable.class.php | 31 ++++++++--- .../unittest/InvocableTest.class.php | 51 ++++++++++++++++--- 2 files changed, 66 insertions(+), 16 deletions(-) diff --git a/src/main/php/lang/reflection/Invocable.class.php b/src/main/php/lang/reflection/Invocable.class.php index a754cf4..9066941 100755 --- a/src/main/php/lang/reflection/Invocable.class.php +++ b/src/main/php/lang/reflection/Invocable.class.php @@ -1,6 +1,6 @@ reflect= $arg; } else if (is_array($arg)) { - $this->reflect= new ReflectionFunction(Closure::fromCallable($arg)); + $this->reflect= new ReflectionMethod(...$arg); + $receiver ?? $receiver= $arg[0]; } else { $this->reflect= new ReflectionFunction($arg); } @@ -52,7 +53,15 @@ public function declaredName(): ?string { } /** Returns a compound name consisting of `[NAME]()` */ - public function compoundName(): string { return $this->name().'()'; } + public function compoundName(): string { + if (PHP_VERSION_ID < 80200 ? strpos($this->reflect->name, '{closure}') : $this->reflect->isAnonymous()) { + return '{closure}()'; + } else if ($this->reflect instanceof ReflectionFunction) { + return strtr($this->reflect->name, '\\', '.').'()'; + } else { + return strtr($this->reflect->class.'::'.$this->reflect->name, '\\', '.').'()'; + } + } /** * Returns this invocable's package. Anonymous functions are always in the @@ -140,10 +149,16 @@ public function returns() { public function invoke(array $args= []) { try { $pass= PHP_VERSION_ID < 80000 && $args ? Routine::pass($this->reflect, $args) : $args; - return $this->reflect instanceof ReflectionFunction - ? $this->reflect->invokeArgs($pass) - : $this->reflect->invokeArgs($this->receiver, $pass) - ; + + if ($this->reflect instanceof ReflectionFunction) { + return $this->reflect->invokeArgs($pass); + } else { + + // TODO: Remove superfluous call to setAccessible() if on PHP8.1+ + // see https://wiki.php.net/rfc/make-reflection-setaccessible-no-op + PHP_VERSION_ID < 80100 && $this->reflect->setAccessible(true); + return $this->reflect->invokeArgs($this->receiver, $pass); + } } catch (ReflectionException|ArgumentCountError|TypeError $e) { throw new CannotInvoke($this, $e); } catch (Throwable $e) { @@ -158,7 +173,7 @@ public function invoke(array $args= []) { } /** @return string */ - public function hashCode() { return md5($this->reflect->name); } + public function hashCode() { return 'I'.md5($this->reflect->name); } /** @return string */ public function toString() { return nameof($this).'<'.$this->name().'>'; } diff --git a/src/test/php/lang/reflection/unittest/InvocableTest.class.php b/src/test/php/lang/reflection/unittest/InvocableTest.class.php index 9be61c2..cbe5303 100755 --- a/src/test/php/lang/reflection/unittest/InvocableTest.class.php +++ b/src/test/php/lang/reflection/unittest/InvocableTest.class.php @@ -8,12 +8,19 @@ class InvocableTest { + /** Fixture */ + private function instance($name) { return '@'.strtolower($name); } + + /** Fixture */ + private static function class($name) { return '@'.strtolower($name); } + /** @return iterable */ private function callables() { yield ['strtoupper', 'strtoupper']; yield [fn() => true, null]; yield [function() { return true; }, null]; - yield [[$this, 'name'], 'name']; + yield [[$this, 'instance'], 'instance']; + yield [[self::class, 'class'], 'class']; yield [[Closure::class, 'fromCallable'], 'fromCallable']; yield [Closure::fromCallable('strtoupper'), 'strtoupper']; yield [new ReflectionFunction('strtoupper'), 'strtoupper']; @@ -30,10 +37,26 @@ public function name($callable, $expected) { } #[Test] - public function compound_name() { + public function compound_name_of_function() { Assert::equals('strlen()', (new Invocable('strlen'))->compoundName()); } + #[Test] + public function compound_name_of_instance_method() { + Assert::equals( + 'lang.reflection.unittest.InvocableTest::instance()', + (new Invocable([$this, 'instance']))->compoundName() + ); + } + + #[Test] + public function compound_name_of_class_method() { + Assert::equals( + 'lang.reflection.unittest.InvocableTest::class()', + (new Invocable([self::class, 'class']))->compoundName() + ); + } + #[Test] public function global_function() { $strlen= new Invocable('strlen'); @@ -139,19 +162,31 @@ public function return_type() { #[Test] public function invoke() { - $Invocable= (new Invocable(fn($name) => strtoupper($name))); - Assert::equals('TEST', $Invocable->invoke(['test'])); + $invocable= (new Invocable(fn($name) => strtoupper($name))); + Assert::equals('TEST', $invocable->invoke(['test'])); } #[Test] public function invoke_named() { - $Invocable= (new Invocable(fn($name) => strtoupper($name))); - Assert::equals('TEST', $Invocable->invoke(['name' => 'test'])); + $invocable= (new Invocable(fn($name) => strtoupper($name))); + Assert::equals('TEST', $invocable->invoke(['name' => 'test'])); } #[Test, Expect(CannotInvoke::class)] public function invoke_missing_argument() { - $Invocable= (new Invocable(fn($name) => strtoupper($name))); - $Invocable->invoke([]); + $invocable= (new Invocable(fn($name) => strtoupper($name))); + $invocable->invoke([]); + } + + #[Test] + public function invoke_instance_method() { + $invocable= new Invocable([$this, 'instance']); + Assert::equals('@test', $invocable->invoke(['Test'])); + } + + #[Test] + public function invoke_class_method() { + $invocable= new Invocable([$this, 'class']); + Assert::equals('@test', $invocable->invoke(['Test'])); } } \ No newline at end of file From 823d3ebd8c41ce5d46a5a9f51cabda9f541e94a2 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sat, 14 Nov 2026 18:18:24 +0100 Subject: [PATCH 24/29] Use compound name in hashCode(), toString(), compareTo() --- src/main/php/lang/reflection/Invocable.class.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/php/lang/reflection/Invocable.class.php b/src/main/php/lang/reflection/Invocable.class.php index 9066941..dc225bf 100755 --- a/src/main/php/lang/reflection/Invocable.class.php +++ b/src/main/php/lang/reflection/Invocable.class.php @@ -173,10 +173,10 @@ public function invoke(array $args= []) { } /** @return string */ - public function hashCode() { return 'I'.md5($this->reflect->name); } + public function hashCode() { return 'I'.md5($this->compoundName()); } /** @return string */ - public function toString() { return nameof($this).'<'.$this->name().'>'; } + public function toString() { return nameof($this).'<'.$this->compoundName().'>'; } /** * Compares this member to another value @@ -186,7 +186,7 @@ public function toString() { return nameof($this).'<'.$this->name().'>'; } */ public function compareTo($value) { return $value instanceof self - ? $this->reflect->name <=> $value->reflect->name + ? $this->compoundName() <=> $value->compoundName() : 1 ; } From 916fdf83f01c1bc889e1d6b380c2a2748c9ca3e4 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sat, 14 Nov 2026 18:31:27 +0100 Subject: [PATCH 25/29] Add anonymous() to Type and Invocable --- .../php/lang/reflection/Invocable.class.php | 45 +++++++------------ src/main/php/lang/reflection/Type.class.php | 3 ++ .../unittest/AnonymousClassTest.class.php | 5 +++ 3 files changed, 25 insertions(+), 28 deletions(-) diff --git a/src/main/php/lang/reflection/Invocable.class.php b/src/main/php/lang/reflection/Invocable.class.php index dc225bf..6301732 100755 --- a/src/main/php/lang/reflection/Invocable.class.php +++ b/src/main/php/lang/reflection/Invocable.class.php @@ -16,50 +16,39 @@ class Invocable implements Target, Annotated, Value { public function __construct($arg, $receiver= null) { if ($arg instanceof ReflectionFunctionAbstract) { $this->reflect= $arg; + $this->receiver= $receiver; } else if (is_array($arg)) { $this->reflect= new ReflectionMethod(...$arg); - $receiver ?? $receiver= $arg[0]; + $this->receiver= $arg[0]; } else { $this->reflect= new ReflectionFunction($arg); + $this->receiver= $receiver; } - $this->receiver= $receiver; } - /** Returns the name, null for anonymous functions */ - public function name(): ?string { - if (PHP_VERSION_ID < 80200 ? strpos($this->reflect->name, '{closure}') : $this->reflect->isAnonymous()) { - return null; - } else { - return strtr($this->reflect->name, '\\', '.'); - } + /** Returns whether this is an anonymous function */ + public function anonymous(): bool { + return + $this->reflect instanceof ReflectionFunction && + (PHP_VERSION_ID < 80200 ? strpos($this->reflect->name, '{closure}') : $this->reflect->isAnonymous()) + ; } + /** Returns the name, null for anonymous functions */ + public function name(): ?string { return $this->anonymous() ? null : strtr($this->reflect->name, '\\', '.'); } + /** Returns type literal (in namespaced form) */ - public function literal(): ?string { - if (PHP_VERSION_ID < 80200 ? strpos($this->reflect->name, '{closure}') : $this->reflect->isAnonymous()) { - return null; - } else { - return $this->reflect->name; - } - } + public function literal(): ?string { return $this->anonymous() ? null : $this->reflect->name; } /** Returns declared name (without namespace) */ - public function declaredName(): ?string { - if (PHP_VERSION_ID < 80200 ? strpos($this->reflect->name, '{closure}') : $this->reflect->isAnonymous()) { - return null; - } else { - return $this->reflect->getShortName(); - } - } + public function declaredName(): ?string { return $this->anonymous() ? null : $this->reflect->getShortName(); } /** Returns a compound name consisting of `[NAME]()` */ public function compoundName(): string { - if (PHP_VERSION_ID < 80200 ? strpos($this->reflect->name, '{closure}') : $this->reflect->isAnonymous()) { - return '{closure}()'; - } else if ($this->reflect instanceof ReflectionFunction) { - return strtr($this->reflect->name, '\\', '.').'()'; - } else { + if ($this->reflect instanceof ReflectionMethod) { return strtr($this->reflect->class.'::'.$this->reflect->name, '\\', '.').'()'; + } else { + return $this->anonymous() ? '{closure}()' : strtr($this->reflect->name, '\\', '.').'()'; } } diff --git a/src/main/php/lang/reflection/Type.class.php b/src/main/php/lang/reflection/Type.class.php index 1352594..c2336d2 100755 --- a/src/main/php/lang/reflection/Type.class.php +++ b/src/main/php/lang/reflection/Type.class.php @@ -17,6 +17,9 @@ public function __construct($reflect) { $this->reflect= $reflect; } + /** Returns whether this is an anonymous function */ + public function anonymous(): bool { return $this->reflect->isAnonymous(); } + /** Returns type name (in dotted form) */ public function name(): ?string { return $this->reflect->isAnonymous() ? null : strtr($this->reflect->name, '\\', '.'); diff --git a/src/test/php/lang/reflection/unittest/AnonymousClassTest.class.php b/src/test/php/lang/reflection/unittest/AnonymousClassTest.class.php index af60b69..03f2ac0 100755 --- a/src/test/php/lang/reflection/unittest/AnonymousClassTest.class.php +++ b/src/test/php/lang/reflection/unittest/AnonymousClassTest.class.php @@ -5,6 +5,11 @@ class AnonymousClassTest { + #[Test] + public function anonymous() { + Assert::true(Reflection::type(new class() { })->anonymous()); + } + #[Test] public function name() { Assert::null(Reflection::type(new class() { })->name()); From 5a0d6612b0b13c364fc8f3e9a53a6ce108875cbc Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sat, 14 Nov 2026 18:33:12 +0100 Subject: [PATCH 26/29] Fix "Calling ReflectionMethod::invokeArgs() for static method [...] does not need an object parameter" --- src/main/php/lang/reflection/Invocable.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/php/lang/reflection/Invocable.class.php b/src/main/php/lang/reflection/Invocable.class.php index 6301732..36ac725 100755 --- a/src/main/php/lang/reflection/Invocable.class.php +++ b/src/main/php/lang/reflection/Invocable.class.php @@ -19,7 +19,7 @@ public function __construct($arg, $receiver= null) { $this->receiver= $receiver; } else if (is_array($arg)) { $this->reflect= new ReflectionMethod(...$arg); - $this->receiver= $arg[0]; + $this->receiver= $this->reflect->isStatic() ? null : $arg[0]; } else { $this->reflect= new ReflectionFunction($arg); $this->receiver= $receiver; From 3c04efa6a5fa668b5548b906f78e46392a1b2d66 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sat, 14 Nov 2026 18:34:13 +0100 Subject: [PATCH 27/29] Honor any given receiver --- src/main/php/lang/reflection/Invocable.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/php/lang/reflection/Invocable.class.php b/src/main/php/lang/reflection/Invocable.class.php index 36ac725..ff5de51 100755 --- a/src/main/php/lang/reflection/Invocable.class.php +++ b/src/main/php/lang/reflection/Invocable.class.php @@ -19,7 +19,7 @@ public function __construct($arg, $receiver= null) { $this->receiver= $receiver; } else if (is_array($arg)) { $this->reflect= new ReflectionMethod(...$arg); - $this->receiver= $this->reflect->isStatic() ? null : $arg[0]; + $this->receiver= $this->reflect->isStatic() ? null : $receiver ?? $arg[0]; } else { $this->reflect= new ReflectionFunction($arg); $this->receiver= $receiver; From 86a158aabe7669e8248f4e1a241351ce9263963e Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sat, 14 Nov 2026 18:36:02 +0100 Subject: [PATCH 28/29] Simplify Invocable constructor --- src/main/php/lang/reflection/Invocable.class.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/main/php/lang/reflection/Invocable.class.php b/src/main/php/lang/reflection/Invocable.class.php index ff5de51..5b09d70 100755 --- a/src/main/php/lang/reflection/Invocable.class.php +++ b/src/main/php/lang/reflection/Invocable.class.php @@ -14,14 +14,11 @@ class Invocable implements Target, Annotated, Value { * @param ?object $receiver */ public function __construct($arg, $receiver= null) { - if ($arg instanceof ReflectionFunctionAbstract) { - $this->reflect= $arg; - $this->receiver= $receiver; - } else if (is_array($arg)) { + if (is_array($arg)) { $this->reflect= new ReflectionMethod(...$arg); $this->receiver= $this->reflect->isStatic() ? null : $receiver ?? $arg[0]; } else { - $this->reflect= new ReflectionFunction($arg); + $this->reflect= $arg instanceof ReflectionFunctionAbstract ? $arg : new ReflectionFunction($arg); $this->receiver= $receiver; } } From ba14d2b70d7e793164bc9b6ce7de06d04f7e5077 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sat, 14 Nov 2026 19:00:59 +0100 Subject: [PATCH 29/29] QA: Document invocables [skip ci] --- README.md | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 8942f07..188346b 100755 --- a/README.md +++ b/README.md @@ -28,6 +28,7 @@ use org\example\{Base, Inject, Fixture}; $type= Reflection::type(Fixture::class); +$type->anonymous(); // FALSE $type->name(); // org.example.Fixture $type->declaredName(); // Fixture $type->literal(); // Fixture::class @@ -144,6 +145,7 @@ foreach ($type->methods() as $name => $method) { $method->parameters(); // Parameters $method->parameter(0); // Parameter or NULL $method->closure($instance); // Closure instance + $method->invocable($instance); // Invocable $method->invoke($instance, []); // (method return value) } ``` @@ -175,12 +177,33 @@ foreach ($parameters as $name => $parameter) { } ``` -Packages can be reflected upon by passing namespace names to `Reflection::of()`. +Closure can be reflected upon via the *Invocable* class: ```php use lang\Reflection; -$package= Reflection::of('org.example'); +$invocable= Reflection::invocable('strlen'); + +$invocable->anonymous(); // FALSE +$invocable->name(); // strlen +$invocable->declaredName(); // strlen +$invocable->literal(); // strlen +$invocable->comment(); // (api doc comment) +$invocable->package(); // Package or NULL +$invocable->annotations(); // Annotations +$invocable->annotation(Inject::class); // Annotation or NULL +$invocable->returns(); // Constraint +$invocable->parameters(); // Parameters +$invocable->parameter(0); // Parameter or NULL +$invocable->invoke(['Test']); // strlen("Test") = 4 +``` + +Packages can be reflected upon by passing namespace names to `Reflection::package()`. + +```php +use lang\Reflection; + +$package= Reflection::package('org.example'); $package->name(); // org.example $package->literal(); // 'org\example'