Skip to content

Commit

Permalink
feat(util.meta): add method metadata util
Browse files Browse the repository at this point in the history
  • Loading branch information
adhocore committed Aug 25, 2018
1 parent 9883ab7 commit 5f8dabe
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/Util/Metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,43 @@ public function forReflectionClass(\ReflectionClass $class, bool $docblock = fal

return $metadata;
}

public function forMethod(string $classFqcn, string $method, bool $docblock = false): array
{
$reflMethod = (new \ReflectionClass($classFqcn))->getMethod($method);

return $this->forReflectionMethod($reflMethod);
}

public function forReflectionMethod(\ReflectionMethod $method, bool $docblock = false): array
{
$params = [];
$parser = new DocBlock($method);

foreach ($parser->find('param') as $param) {
$params[] = \preg_replace(['/(.*\$\w+)(.*)/', '/ +/'], ['$1', ' '], $param->getValue());
}

if (null !== $return = $parser->first('return')) {
$return = \preg_replace('/(\S+)(.*)/', '$1', $return->getValue());
}

$metadata = [
'isStatic' => $method->isStatic(),
'isFinal' => $method->isFinal(),
'isPublic' => $method->isPublic(),
'isAbstract' => $method->isAbstract(),
'params' => $params,
'return' => $return,
];

if ($docblock) {
$texts = $parser->texts();
$title = \array_shift($texts);

$metadata += \compact('title', 'texts');
}

return $metadata;
}
}

0 comments on commit 5f8dabe

Please sign in to comment.