From ac6530b55ada4f7c025f5b751807dbd1d12f1e61 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sun, 15 Nov 2026 11:44:40 +0100 Subject: [PATCH 1/3] Add Methods::class() and Methods::instance() to enumerate methods --- .../php/lang/reflection/Methods.class.php | 26 ++++++++++++ .../reflection/unittest/MethodsTest.class.php | 40 +++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/src/main/php/lang/reflection/Methods.class.php b/src/main/php/lang/reflection/Methods.class.php index 8e6fec8..0c3497a 100755 --- a/src/main/php/lang/reflection/Methods.class.php +++ b/src/main/php/lang/reflection/Methods.class.php @@ -21,6 +21,32 @@ public function getIterator(): Traversable { } } + /** + * Return class methods + * + * @param int $mask + * @return iterable + */ + public function class($mask= MODIFIER_PUBLIC | MODIFIER_PROTECTED | MODIFIER_PRIVATE) { + foreach ($this->reflect->getMethods($mask) as $method) { + if (!$method->isStatic() || 0 === strncmp($method->name, '__', 2)) continue; + yield $method->name => new Method($method); + } + } + + /** + * Return instance methods + * + * @param int $mask + * @return iterable + */ + public function instance($mask= MODIFIER_PUBLIC | MODIFIER_PROTECTED | MODIFIER_PRIVATE) { + foreach ($this->reflect->getMethods($mask) as $method) { + if ($method->isStatic() || 0 === strncmp($method->name, '__', 2)) continue; + yield $method->name => new Method($method); + } + } + /** * Return methods annotated with a given annotation * diff --git a/src/test/php/lang/reflection/unittest/MethodsTest.class.php b/src/test/php/lang/reflection/unittest/MethodsTest.class.php index f1bccc2..ff59f04 100755 --- a/src/test/php/lang/reflection/unittest/MethodsTest.class.php +++ b/src/test/php/lang/reflection/unittest/MethodsTest.class.php @@ -60,6 +60,46 @@ public function c() { } ); } + #[Test] + public function instance() { + $type= $this->declare('{ + public static function create() { } + + private function concat() { } + + public function name() { } + }'); + + Assert::equals( + ['concat' => $type->method('concat'), 'name' => $type->method('name')], + iterator_to_array($type->methods()->instance()) + ); + Assert::equals( + ['name' => $type->method('name')], + iterator_to_array($type->methods()->instance(MODIFIER_PUBLIC)) + ); + } + + #[Test] + public function class() { + $type= $this->declare('{ + public static function create() { } + + private static function newInstance() { } + + public function name() { } + }'); + + Assert::equals( + ['create' => $type->method('create'), 'newInstance' => $type->method('newInstance')], + iterator_to_array($type->methods()->class()) + ); + Assert::equals( + ['newInstance' => $type->method('newInstance')], + iterator_to_array($type->methods()->class(MODIFIER_PRIVATE)) + ); + } + #[Test] public function non_existant() { $type= $this->declare('{ }'); From e70279ad4d16102e2ab9d9d7fd0b8221fdb5fd24 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sun, 15 Nov 2026 11:49:51 +0100 Subject: [PATCH 2/3] QA: Make implementations consistent --- src/main/php/lang/reflection/Methods.class.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/php/lang/reflection/Methods.class.php b/src/main/php/lang/reflection/Methods.class.php index 0c3497a..425ec91 100755 --- a/src/main/php/lang/reflection/Methods.class.php +++ b/src/main/php/lang/reflection/Methods.class.php @@ -17,7 +17,8 @@ class Methods extends Members { */ public function getIterator(): Traversable { foreach ($this->reflect->getMethods() as $method) { - if (0 !== strncmp($method->name, '__', 2)) yield $method->name => new Method($method); + if (0 === strncmp($method->name, '__', 2)) continue; + yield $method->name => new Method($method); } } @@ -56,10 +57,9 @@ public function instance($mask= MODIFIER_PUBLIC | MODIFIER_PROTECTED | MODIFIER_ public function annotated($annotation) { $t= strtr($annotation, '.', '\\'); foreach ($this->reflect->getMethods() as $method) { - if (0 !== strncmp($method->name, '__', 2)) { - $annotations= Reflection::meta()->methodAnnotations($method); - if (isset($annotations[$t])) yield $method->name => new Method($method, $annotations); - } + if (0 === strncmp($method->name, '__', 2)) continue; + $annotations= Reflection::meta()->methodAnnotations($method); + if (isset($annotations[$t])) yield $method->name => new Method($method, $annotations); } } From 153788a05db63a93b675961a51122129f801b273 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sun, 15 Nov 2026 12:23:17 +0100 Subject: [PATCH 3/3] Fix constructor parameter type --- src/main/php/lang/reflection/Members.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/php/lang/reflection/Members.class.php b/src/main/php/lang/reflection/Members.class.php index 7f2dd70..7623494 100755 --- a/src/main/php/lang/reflection/Members.class.php +++ b/src/main/php/lang/reflection/Members.class.php @@ -6,7 +6,7 @@ abstract class Members implements IteratorAggregate { protected $reflect; - /** @param ReflectionClassConstant|ReflectionProperty|ReflectionMethod $reflect */ + /** @param ReflectionClass $reflect */ public function __construct($reflect) { $this->reflect= $reflect; }