Skip to content

Commit

Permalink
refactor(util.metadata): add throws, improve return/params
Browse files Browse the repository at this point in the history
  • Loading branch information
adhocore committed Sep 20, 2018
1 parent 72bc7fa commit a8898a6
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/Util/Metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public function forReflectionMethod(\ReflectionMethod $method): array
$parser = new DocBlock($method);
$texts = $parser->texts();
$title = \array_shift($texts);
$throws = $parser->first('throws');

$metadata = [
'name' => $method->name,
Expand All @@ -73,33 +74,34 @@ public function forReflectionMethod(\ReflectionMethod $method): array
'isPublic' => $method->isPublic(),
'isAbstract' => $method->isAbstract(),
'maybeMagic' => \substr($method->name, 0, 2) === '__',
'throws' => $throws ? \explode(' ', \trim($throws->getValue()), 2) : [],
'title' => $title,
'texts' => $texts,
];

$params = [];
foreach ($parser->find('param') as $param) {
if (\preg_match('/(.*)\$(\w+)/', $param->getValue(), $match)) {
$params[$match[2]] = \trim($match[1]);
if (\preg_match('/(.*)\$(\w+)(.*)/', $param->getValue(), $match)) {
$params[$match[2]] = [\trim($match[1]), \trim($match[3])];
}
}

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

return $metadata + $this->getMethodParameters($method, $params, $return ?? '');
return $metadata + $this->getMethodParameters($method, $params, $return ?? []);
}

protected function getMethodParameters(\ReflectionMethod $method, array $docParams, string $return)
protected function getMethodParameters(\ReflectionMethod $method, array $docParams, array $return)
{
$params = [];
$parser = new DocBlock($method);

foreach ($method->getParameters() as $param) {
$name = $param->name;
if (!$param->hasType()) {
$params[] = \trim(($docParams[$name] ?? '') . " \$$name");
$params[] = [\trim(($docParams[$name][0] ?? '') . " \$$name"), $docParams[$name][1] ?? ''];

continue;
}
Expand All @@ -108,7 +110,7 @@ protected function getMethodParameters(\ReflectionMethod $method, array $docPara
}

if ($returnType = $method->getReturnType()) {
$return = $this->getRealType($returnType);
$return[0] = $this->getRealType($returnType);
}

return \compact('params', 'return');
Expand Down

0 comments on commit a8898a6

Please sign in to comment.