mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-03 09:50:49 +00:00
aae549741f
0cb3fd0feb
[Php73] Handle crash Type Error on JsonThrowOnErrorRector (#4626)
66 lines
2.1 KiB
PHP
66 lines
2.1 KiB
PHP
<?php
|
|
|
|
declare (strict_types=1);
|
|
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
|
|
|
|
use PHPStan\Type\ObjectType;
|
|
use PHPStan\Reflection\ClassReflection;
|
|
use RectorPrefix202308\Nette\Utils\Strings;
|
|
use PhpParser\Node;
|
|
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
|
|
use PHPStan\Type\ConditionalType;
|
|
use PHPStan\Type\Type;
|
|
use PHPStan\Type\TypeCombinator;
|
|
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
|
|
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
|
|
use Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper;
|
|
use RectorPrefix202308\Symfony\Contracts\Service\Attribute\Required;
|
|
use PHPStan\Type\TypeTraverser;
|
|
/**
|
|
* @implements TypeMapperInterface<ConditionalType>
|
|
*/
|
|
final class ConditionalTypeMapper implements TypeMapperInterface
|
|
{
|
|
/**
|
|
* @var \Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper
|
|
*/
|
|
private $phpStanStaticTypeMapper;
|
|
/**
|
|
* @required
|
|
*/
|
|
public function autowire(PHPStanStaticTypeMapper $phpStanStaticTypeMapper) : void
|
|
{
|
|
$this->phpStanStaticTypeMapper = $phpStanStaticTypeMapper;
|
|
}
|
|
/**
|
|
* @return class-string<Type>
|
|
*/
|
|
public function getNodeClass() : string
|
|
{
|
|
return ConditionalType::class;
|
|
}
|
|
/**
|
|
* @param ConditionalType $type
|
|
*/
|
|
public function mapToPHPStanPhpDocTypeNode(Type $type) : TypeNode
|
|
{
|
|
$type = TypeTraverser::map($type, static function (Type $type, callable $traverse) : Type {
|
|
if ($type instanceof ObjectType && !$type->getClassReflection() instanceof ClassReflection) {
|
|
$newClassName = (string) Strings::after($type->getClassName(), '\\', -1);
|
|
return $traverse(new ObjectType($newClassName));
|
|
}
|
|
return $traverse($type);
|
|
});
|
|
return $type->toPhpDocNode();
|
|
}
|
|
/**
|
|
* @param ConditionalType $type
|
|
* @param TypeKind::* $typeKind
|
|
*/
|
|
public function mapToPhpParserNode(Type $type, string $typeKind) : ?Node
|
|
{
|
|
$type = TypeCombinator::union($type->getIf(), $type->getElse());
|
|
return $this->phpStanStaticTypeMapper->mapToPhpParserNode($type, $typeKind);
|
|
}
|
|
}
|