Skip to content

Commit a8898a6

Browse files
committed
refactor(util.metadata): add throws, improve return/params
1 parent 72bc7fa commit a8898a6

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/Util/Metadata.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public function forReflectionMethod(\ReflectionMethod $method): array
6464
$parser = new DocBlock($method);
6565
$texts = $parser->texts();
6666
$title = \array_shift($texts);
67+
$throws = $parser->first('throws');
6768

6869
$metadata = [
6970
'name' => $method->name,
@@ -73,33 +74,34 @@ public function forReflectionMethod(\ReflectionMethod $method): array
7374
'isPublic' => $method->isPublic(),
7475
'isAbstract' => $method->isAbstract(),
7576
'maybeMagic' => \substr($method->name, 0, 2) === '__',
77+
'throws' => $throws ? \explode(' ', \trim($throws->getValue()), 2) : [],
7678
'title' => $title,
7779
'texts' => $texts,
7880
];
7981

8082
$params = [];
8183
foreach ($parser->find('param') as $param) {
82-
if (\preg_match('/(.*)\$(\w+)/', $param->getValue(), $match)) {
83-
$params[$match[2]] = \trim($match[1]);
84+
if (\preg_match('/(.*)\$(\w+)(.*)/', $param->getValue(), $match)) {
85+
$params[$match[2]] = [\trim($match[1]), \trim($match[3])];
8486
}
8587
}
8688

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

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

94-
protected function getMethodParameters(\ReflectionMethod $method, array $docParams, string $return)
96+
protected function getMethodParameters(\ReflectionMethod $method, array $docParams, array $return)
9597
{
9698
$params = [];
9799
$parser = new DocBlock($method);
98100

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

104106
continue;
105107
}
@@ -108,7 +110,7 @@ protected function getMethodParameters(\ReflectionMethod $method, array $docPara
108110
}
109111

110112
if ($returnType = $method->getReturnType()) {
111-
$return = $this->getRealType($returnType);
113+
$return[0] = $this->getRealType($returnType);
112114
}
113115

114116
return \compact('params', 'return');

0 commit comments

Comments
 (0)