do not reflect on invalid types

This commit is contained in:
TomasVotruba 2017-12-20 03:20:38 +01:00
parent 92b23cc6d2
commit b7019cece8
3 changed files with 42 additions and 2 deletions

View File

@ -42,6 +42,11 @@ final class SmartClassReflector
public function reflect(string $className): ?ReflectionClass
{
// invalid class types
if (in_array($className, ['self', 'null', 'array', 'string', 'bool'])) {
return null;
}
try {
if ($this->shouldCreateNewClassReflector()) {
$this->createNewClassReflector();

View File

@ -12,6 +12,11 @@ use Rector\ReflectionDocBlock\NodeAnalyzer\DocBlockAnalyzer;
final class PropertyTypeResolver implements PerNodeTypeResolverInterface
{
/**
* @var string[]
*/
private $scalarTypes = ['string', 'bool', 'array', 'int', 'resource', 'iterable', 'callable', 'object'];
/**
* @var TypeContext
*/
@ -58,10 +63,11 @@ final class PropertyTypeResolver implements PerNodeTypeResolverInterface
}
$propertyTypes = $this->docBlockAnalyzer->getVarTypes($propertyNode);
if ($propertyTypes === null) {
if ($propertyTypes === null || $propertyTypes === ['string']) {
return [];
}
$propertyTypes = $this->filterOutScalarTypes($propertyTypes);
$propertyTypes = $this->addParentClasses($propertyTypes);
$this->typeContext->addPropertyTypes($propertyName, $propertyTypes);
@ -85,4 +91,25 @@ final class PropertyTypeResolver implements PerNodeTypeResolverInterface
return array_values(array_unique($propertyTypes));
}
/**
* @param string[] $propertyTypes
* @return string[]
*/
private function filterOutScalarTypes(array $propertyTypes): array
{
foreach ($propertyTypes as $key => $type) {
if (! in_array($type, $this->scalarTypes, true)) {
continue;
}
unset($propertyTypes[$key]);
}
if ($propertyTypes === ['null']) {
return [];
}
return $propertyTypes;
}
}

View File

@ -166,9 +166,17 @@ final class DocBlockAnalyzer
*/
private function normalizeTypes(array $types): array
{
return array_map(function (string $type) {
// remove preslash: {\]SomeClass
$types = array_map(function (string $type) {
return ltrim(trim($type), '\\');
}, $types);
// remove arrays: Type{[][][]}
$types = array_map(function (string $type) {
return rtrim($type, '[]');
}, $types);
return $types;
}
/**