Merge pull request #906 from mfn/mfn-return-reflection

Infer return type from reflection if no phpdoc given
This commit is contained in:
Barry vd. Heuvel
2020-04-21 21:16:27 +02:00
committed by GitHub
3 changed files with 153 additions and 13 deletions
+40 -7
View File
@@ -10,20 +10,20 @@
namespace Barryvdh\LaravelIdeHelper\Console;
use Barryvdh\Reflection\DocBlock;
use Barryvdh\Reflection\DocBlock\Context;
use Barryvdh\Reflection\DocBlock\Serializer as DocBlockSerializer;
use Barryvdh\Reflection\DocBlock\Tag;
use Composer\Autoload\ClassMapGenerator;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Str;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Str;
use ReflectionClass;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Barryvdh\Reflection\DocBlock;
use Barryvdh\Reflection\DocBlock\Context;
use Barryvdh\Reflection\DocBlock\Tag;
use Barryvdh\Reflection\DocBlock\Serializer as DocBlockSerializer;
/**
* A command to generate autocomplete information for your IDE
@@ -440,7 +440,7 @@ class ModelsCommand extends Command
$name = Str::snake(substr($method, 3, -9));
if (!empty($name)) {
$reflection = new \ReflectionMethod($model, $method);
$type = $this->getReturnTypeFromDocBlock($reflection);
$type = $this->getReturnType($reflection);
$this->setProperty($name, $type, true, null);
}
} elseif (Str::startsWith($method, 'set') && Str::endsWith(
@@ -826,6 +826,16 @@ class ModelsCommand extends Command
return $this->laravel['config']->get('ide-helper.model_camel_case_properties', false);
}
protected function getReturnType(\ReflectionMethod $reflection): ?string
{
$type = $this->getReturnTypeFromDocBlock($reflection);
if ($type) {
return $type;
}
return $this->getReturnTypeFromReflection($reflection);
}
/**
* Get method return type based on it DocBlock comment
*
@@ -845,6 +855,29 @@ class ModelsCommand extends Command
return $type;
}
protected function getReturnTypeFromReflection(\ReflectionMethod $reflection): ?string
{
$returnType = $reflection->getReturnType();
if (!$returnType) {
return null;
}
$type = $returnType instanceof \ReflectionNamedType
? $returnType->getName()
: (string)$returnType;
if (!$returnType->isBuiltin()) {
$type = '\\' . $type;
}
if ($returnType->allowsNull()) {
$type .= '|null';
}
return $type;
}
/**
* Generates methods provided by the SoftDeletes trait
* @param \Illuminate\Database\Eloquent\Model $model