From 71cb03ca6aef83ec6bc2361d14a233085d96641d Mon Sep 17 00:00:00 2001 From: Bogdan Padalko Date: Tue, 6 Sep 2016 12:24:42 +0300 Subject: [PATCH] Use accessor docblock return type to guess property type --- src/Console/ModelsCommand.php | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index e736b90..70f1605 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -386,7 +386,9 @@ class ModelsCommand extends Command //Magic getAttribute $name = Str::snake(substr($method, 3, -9)); if (!empty($name)) { - $this->setProperty($name, null, true, null); + $reflection = new \ReflectionMethod($model, $method); + $type = $this->getReturnTypeFromDocBlock($reflection); + $this->setProperty($name, $type, true, null); } } elseif (Str::startsWith($method, 'set') && Str::endsWith( $method, @@ -669,4 +671,23 @@ class ModelsCommand extends Command { return $this->laravel['config']->get('ide-helper.model_camel_case_properties', false); } + + /** + * Get method return type based on it DocBlock comment + * + * @param \ReflectionMethod $reflection + * + * @return null|string + */ + protected function getReturnTypeFromDocBlock(\ReflectionMethod $reflection) + { + $type = null; + $phpdoc = new DocBlock($reflection); + + if ($phpdoc->hasTag('return')) { + $type = $phpdoc->getTagsByName('return')[0]->getContent(); + } + + return $type; + } }