Added support for Laravel 7 custom casts (#913)

* added support for laravel 7 custom casts

* refactored tests; added test cases

* added test case

* resolved method dublication
This commit is contained in:
belamov
2020-04-22 06:32:40 +02:00
committed by GitHub
parent dcf76c1a81
commit cfa9a81d65
11 changed files with 340 additions and 1 deletions
+31 -1
View File
@@ -16,7 +16,7 @@ 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\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Str;
@@ -322,6 +322,8 @@ class ModelsCommand extends Command
if (!isset($this->properties[$name])) {
continue;
} else {
$realType = $this->checkForCustomLaravelCasts($realType);
$this->properties[$name]['type'] = $this->getTypeOverride($realType);
if (isset($this->nullableColumns[$name])) {
@@ -942,4 +944,32 @@ class ModelsCommand extends Command
return $keyword;
}
/**
* @param string $type
* @return string|null
* @throws \ReflectionException
*/
protected function checkForCustomLaravelCasts(string $type): ?string
{
if (!class_exists($type)) {
return $type;
}
$reflection = new \ReflectionClass($type);
if (!$reflection->implementsInterface(CastsAttributes::class)) {
return $type;
}
$methodReflection = new \ReflectionMethod($type, 'get');
$type = $this->getReturnTypeFromReflection($methodReflection);
if ($type === null) {
$type = $this->getReturnTypeFromDocBlock($methodReflection);
}
return $type;
}
}