Fix broken ReflectionUnionTypes (#1132)

* Fix exceptions on ReflectionUnionTypes
this commit fixes exceptions thrown on ReflectionUnionType::isBuiltIn() and ReflectionUnionType::getNme() called on php8 union types

* Fix exceptions on ReflectionUnionTypes
this commit fixes exceptions thrown on ReflectionUnionType::isBuiltIn() and ReflectionUnionType::getNme() called on php8 union types

* add check for php8.0

* fix static analysis error

* fix $types variable undefined

* fix failing test

* fix failing test

* fixed style with php-cs-fixer

* add test for union types in parameters and return type

* add test for nullable union types in parameters and return type

* updated CHANGELOG.md
This commit is contained in:
Fabio Ivona
2021-01-11 09:15:22 +01:00
committed by GitHub
parent 3376522d68
commit 73c502d5c5
5 changed files with 157 additions and 18 deletions
+45 -17
View File
@@ -35,7 +35,9 @@ use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Str;
use phpDocumentor\Reflection\Types\ContextFactory;
use ReflectionClass;
use ReflectionNamedType;
use ReflectionObject;
use ReflectionType;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
@@ -578,7 +580,7 @@ class ModelsCommand extends Command
$reflection = new \ReflectionMethod($model, $method);
if ($returnType = $reflection->getReturnType()) {
$type = $returnType instanceof \ReflectionNamedType
$type = $returnType instanceof ReflectionNamedType
? $returnType->getName()
: (string)$returnType;
} else {
@@ -1013,16 +1015,12 @@ class ModelsCommand extends Command
return null;
}
$type = $returnType instanceof \ReflectionNamedType
? $returnType->getName()
: (string)$returnType;
$types = $this->extractReflectionTypes($returnType);
if (!$returnType->isBuiltin()) {
$type = '\\' . $type;
}
$type = implode('|', $types);
if ($returnType->allowsNull()) {
$type .= '|null';
if($returnType->allowsNull()){
$type .='|null';
}
return $type;
@@ -1215,17 +1213,19 @@ class ModelsCommand extends Command
protected function getParamType(\ReflectionMethod $method, \ReflectionParameter $parameter): ?string
{
if ($paramType = $parameter->getType()) {
$parameterName = $paramType->getName();
$types = $this->extractReflectionTypes($paramType);
if (!$paramType->isBuiltin()) {
$parameterName = '\\' . $parameterName;
$type = implode('|', $types);
if($paramType->allowsNull()){
if(count($types)==1){
$type = '?' . $type;
}else{
$type .='|null';
}
}
if ($paramType->allowsNull()) {
return '?' . $parameterName;
}
return $parameterName;
return $type;
}
$docComment = $method->getDocComment();
@@ -1290,4 +1290,32 @@ class ModelsCommand extends Command
// then we have found the type of the variable if not we return null
return $type;
}
protected function extractReflectionTypes(ReflectionType $reflection_type)
{
if($reflection_type instanceof ReflectionNamedType){
$types[] = $this->getReflectionNamedType($reflection_type);
}else{
$types = [];
foreach ($reflection_type->getTypes() as $named_type){
if($named_type->getName()==='null'){
continue;
}
$types[] = $this->getReflectionNamedType($named_type);
}
}
return $types;
}
protected function getReflectionNamedType(ReflectionNamedType $paramType): string
{
$parameterName = $paramType->getName();
if (!$paramType->isBuiltin()) {
$parameterName = '\\' . $parameterName;
}
return $parameterName;
}
}