From a8898a6626b4b4ad13934dde69a8a62c8976ab99 Mon Sep 17 00:00:00 2001 From: Jitendra Adhikari Date: Thu, 20 Sep 2018 21:54:36 +0700 Subject: [PATCH] refactor(util.metadata): add throws, improve return/params --- src/Util/Metadata.php | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/Util/Metadata.php b/src/Util/Metadata.php index a6c6dc6..f43803e 100644 --- a/src/Util/Metadata.php +++ b/src/Util/Metadata.php @@ -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, @@ -73,25 +74,26 @@ 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); @@ -99,7 +101,7 @@ protected function getMethodParameters(\ReflectionMethod $method, array $docPara 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; } @@ -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');