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; } diff --git a/src/main/php/lang/reflection/Methods.class.php b/src/main/php/lang/reflection/Methods.class.php index 8e6fec8..425ec91 100755 --- a/src/main/php/lang/reflection/Methods.class.php +++ b/src/main/php/lang/reflection/Methods.class.php @@ -17,7 +17,34 @@ 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); + } + } + + /** + * 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); } } @@ -30,10 +57,9 @@ public function getIterator(): Traversable { 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); } } 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('{ }');