Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/php/lang/reflection/Members.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
36 changes: 31 additions & 5 deletions src/main/php/lang/reflection/Methods.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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);
}
}

Expand Down
40 changes: 40 additions & 0 deletions src/test/php/lang/reflection/unittest/MethodsTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('{ }');
Expand Down
Loading