mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-23 19:24:48 +01:00
do not reflect on invalid types
This commit is contained in:
parent
92b23cc6d2
commit
b7019cece8
@ -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();
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user