Skip to content

Commit 2261273

Browse files
authored
Merge pull request #389 from pinepain/return_type
Use accessor docblock return type to guess property type
2 parents dc476f9 + 71cb03c commit 2261273

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

src/Console/ModelsCommand.php

+22-1
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,9 @@ protected function getPropertiesFromMethods($model)
386386
//Magic get<name>Attribute
387387
$name = Str::snake(substr($method, 3, -9));
388388
if (!empty($name)) {
389-
$this->setProperty($name, null, true, null);
389+
$reflection = new \ReflectionMethod($model, $method);
390+
$type = $this->getReturnTypeFromDocBlock($reflection);
391+
$this->setProperty($name, $type, true, null);
390392
}
391393
} elseif (Str::startsWith($method, 'set') && Str::endsWith(
392394
$method,
@@ -669,4 +671,23 @@ protected function hasCamelCaseModelProperties()
669671
{
670672
return $this->laravel['config']->get('ide-helper.model_camel_case_properties', false);
671673
}
674+
675+
/**
676+
* Get method return type based on it DocBlock comment
677+
*
678+
* @param \ReflectionMethod $reflection
679+
*
680+
* @return null|string
681+
*/
682+
protected function getReturnTypeFromDocBlock(\ReflectionMethod $reflection)
683+
{
684+
$type = null;
685+
$phpdoc = new DocBlock($reflection);
686+
687+
if ($phpdoc->hasTag('return')) {
688+
$type = $phpdoc->getTagsByName('return')[0]->getContent();
689+
}
690+
691+
return $type;
692+
}
672693
}

0 commit comments

Comments
 (0)