mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-03 01:40:49 +00:00
fc10fce13d
* re-enable rectify and ecs * [Rectify] [Php81] Enable Rectify on Readonly Property only * comment * [ci-review] Rector Rectify * [ci-review] Rector Rectify * [ci-review] Rector Rectify * [ci-review] Rector Rectify * [ci-review] Rector Rectify Co-authored-by: GitHub Action <action@github.com>
37 lines
1.2 KiB
PHP
37 lines
1.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Rector\NodeTypeResolver\NodeTypeCorrector;
|
|
|
|
use PHPStan\Reflection\ReflectionProvider;
|
|
use PHPStan\Type\Constant\ConstantStringType;
|
|
use PHPStan\Type\Generic\GenericClassStringType;
|
|
use PHPStan\Type\ObjectType;
|
|
use PHPStan\Type\Type;
|
|
use PHPStan\Type\TypeTraverser;
|
|
|
|
final class GenericClassStringTypeCorrector
|
|
{
|
|
public function __construct(
|
|
private readonly ReflectionProvider $reflectionProvider
|
|
) {
|
|
}
|
|
|
|
public function correct(Type $mainType): Type
|
|
{
|
|
// inspired from https://github.com/phpstan/phpstan-src/blob/94e3443b2d21404a821e05b901dd4b57fcbd4e7f/src/Type/Generic/TemplateTypeHelper.php#L18
|
|
return TypeTraverser::map($mainType, function (Type $traversedType, callable $traverseCallback): Type {
|
|
if (! $traversedType instanceof ConstantStringType) {
|
|
return $traverseCallback($traversedType);
|
|
}
|
|
|
|
if (! $this->reflectionProvider->hasClass($traversedType->getValue())) {
|
|
return $traverseCallback($traversedType);
|
|
}
|
|
|
|
return new GenericClassStringType(new ObjectType($traversedType->getValue()));
|
|
});
|
|
}
|
|
}
|