2020-01-14 17:59:15 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2020-01-14 17:59:15 +00:00
|
|
|
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Name;
|
|
|
|
use PhpParser\Node\Name\FullyQualified;
|
2021-03-19 14:33:58 +00:00
|
|
|
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
|
2020-01-14 17:59:15 +00:00
|
|
|
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
|
|
|
|
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
|
|
|
|
use PHPStan\Type\Generic\GenericObjectType;
|
2021-03-13 15:15:10 +00:00
|
|
|
use PHPStan\Type\MixedType;
|
2020-01-14 17:59:15 +00:00
|
|
|
use PHPStan\Type\ObjectType;
|
|
|
|
use PHPStan\Type\Type;
|
|
|
|
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
|
2020-08-12 23:04:23 +00:00
|
|
|
use Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper;
|
2020-12-24 16:31:24 +00:00
|
|
|
use Rector\StaticTypeMapper\ValueObject\Type\AliasedObjectType;
|
2021-03-08 14:52:36 +00:00
|
|
|
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedGenericObjectType;
|
2020-12-24 16:31:24 +00:00
|
|
|
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
|
|
|
|
use Rector\StaticTypeMapper\ValueObject\Type\SelfObjectType;
|
|
|
|
use Rector\StaticTypeMapper\ValueObject\Type\ShortenedObjectType;
|
2021-07-03 10:57:19 +00:00
|
|
|
use RectorPrefix20210703\Symfony\Contracts\Service\Attribute\Required;
|
2021-06-30 11:36:37 +00:00
|
|
|
/**
|
|
|
|
* @implements TypeMapperInterface<ObjectType>
|
|
|
|
*/
|
2021-05-12 13:15:45 +00:00
|
|
|
final class ObjectTypeMapper implements \Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface
|
2020-01-14 17:59:15 +00:00
|
|
|
{
|
2020-08-12 23:04:23 +00:00
|
|
|
/**
|
2021-05-12 13:15:45 +00:00
|
|
|
* @var \Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper
|
2020-08-12 23:04:23 +00:00
|
|
|
*/
|
|
|
|
private $phpStanStaticTypeMapper;
|
2021-03-06 21:16:18 +00:00
|
|
|
/**
|
|
|
|
* @return class-string<Type>
|
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeClass() : string
|
2020-01-14 17:59:15 +00:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
return \PHPStan\Type\ObjectType::class;
|
2020-01-14 17:59:15 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param ObjectType $type
|
|
|
|
*/
|
2021-06-26 20:24:36 +00:00
|
|
|
public function mapToPHPStanPhpDocTypeNode(\PHPStan\Type\Type $type, ?string $kind = null) : \PHPStan\PhpDocParser\Ast\Type\TypeNode
|
2020-01-14 17:59:15 +00:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
if ($type instanceof \Rector\StaticTypeMapper\ValueObject\Type\ShortenedObjectType) {
|
|
|
|
return new \PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode($type->getClassName());
|
2020-02-02 18:15:36 +00:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
if ($type instanceof \Rector\StaticTypeMapper\ValueObject\Type\AliasedObjectType) {
|
|
|
|
return new \PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode($type->getClassName());
|
2020-02-02 18:15:36 +00:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
if ($type instanceof \PHPStan\Type\Generic\GenericObjectType) {
|
2021-03-13 15:15:10 +00:00
|
|
|
return $this->mapGenericObjectType($type);
|
2020-08-12 23:04:23 +00:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
return new \PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode('\\' . $type->getClassName());
|
2020-01-14 17:59:15 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param ObjectType $type
|
|
|
|
*/
|
2021-05-10 22:23:08 +00:00
|
|
|
public function mapToPhpParserNode(\PHPStan\Type\Type $type, ?string $kind = null) : ?\PhpParser\Node
|
2020-01-14 17:59:15 +00:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
if ($type instanceof \Rector\StaticTypeMapper\ValueObject\Type\SelfObjectType) {
|
|
|
|
return new \PhpParser\Node\Name('self');
|
2020-01-15 02:16:22 +00:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
if ($type instanceof \Rector\StaticTypeMapper\ValueObject\Type\ShortenedObjectType) {
|
|
|
|
return new \PhpParser\Node\Name\FullyQualified($type->getFullyQualifiedName());
|
2020-01-14 17:59:15 +00:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
if ($type instanceof \Rector\StaticTypeMapper\ValueObject\Type\AliasedObjectType) {
|
|
|
|
return new \PhpParser\Node\Name($type->getClassName());
|
2020-01-14 17:59:15 +00:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
if ($type instanceof \Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType) {
|
|
|
|
return new \PhpParser\Node\Name\FullyQualified($type->getClassName());
|
2020-01-14 17:59:15 +00:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
if (!$type instanceof \PHPStan\Type\Generic\GenericObjectType) {
|
2021-02-21 09:32:45 +00:00
|
|
|
// fallback
|
2021-05-10 22:23:08 +00:00
|
|
|
return new \PhpParser\Node\Name\FullyQualified($type->getClassName());
|
2020-01-14 17:59:15 +00:00
|
|
|
}
|
2021-03-08 14:52:36 +00:00
|
|
|
if ($type->getClassName() === 'iterable') {
|
|
|
|
// fallback
|
2021-05-10 22:23:08 +00:00
|
|
|
return new \PhpParser\Node\Name('iterable');
|
2021-03-08 14:52:36 +00:00
|
|
|
}
|
2021-02-21 09:32:45 +00:00
|
|
|
if ($type->getClassName() !== 'object') {
|
|
|
|
// fallback
|
2021-05-10 22:23:08 +00:00
|
|
|
return new \PhpParser\Node\Name\FullyQualified($type->getClassName());
|
2021-02-21 09:32:45 +00:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
return new \PhpParser\Node\Name('object');
|
2020-01-14 17:59:15 +00:00
|
|
|
}
|
2021-05-12 13:15:45 +00:00
|
|
|
/**
|
|
|
|
* @required
|
|
|
|
*/
|
|
|
|
public function autowireObjectTypeMapper(\Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper $phpStanStaticTypeMapper) : void
|
2020-08-12 23:04:23 +00:00
|
|
|
{
|
|
|
|
$this->phpStanStaticTypeMapper = $phpStanStaticTypeMapper;
|
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
private function mapGenericObjectType(\PHPStan\Type\Generic\GenericObjectType $genericObjectType) : \PHPStan\PhpDocParser\Ast\Type\TypeNode
|
2021-03-13 15:15:10 +00:00
|
|
|
{
|
|
|
|
$name = $this->resolveGenericObjectTypeName($genericObjectType);
|
2021-05-10 22:23:08 +00:00
|
|
|
$identifierTypeNode = new \PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode($name);
|
2021-03-13 15:15:10 +00:00
|
|
|
$genericTypeNodes = [];
|
|
|
|
foreach ($genericObjectType->getTypes() as $key => $genericType) {
|
|
|
|
// mixed type on 1st item in iterator has no value
|
2021-05-10 22:23:08 +00:00
|
|
|
if ($name === 'Iterator' && $genericType instanceof \PHPStan\Type\MixedType && $key === 0) {
|
2021-03-13 15:15:10 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$typeNode = $this->phpStanStaticTypeMapper->mapToPHPStanPhpDocTypeNode($genericType);
|
|
|
|
$genericTypeNodes[] = $typeNode;
|
|
|
|
}
|
2021-03-19 14:33:58 +00:00
|
|
|
if ($genericTypeNodes === []) {
|
|
|
|
return $identifierTypeNode;
|
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
return new \PHPStan\PhpDocParser\Ast\Type\GenericTypeNode($identifierTypeNode, $genericTypeNodes);
|
2021-03-13 15:15:10 +00:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
private function resolveGenericObjectTypeName(\PHPStan\Type\Generic\GenericObjectType $genericObjectType) : string
|
2021-03-13 15:15:10 +00:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
if ($genericObjectType instanceof \Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedGenericObjectType) {
|
2021-03-13 15:15:10 +00:00
|
|
|
return '\\' . $genericObjectType->getClassName();
|
|
|
|
}
|
2021-05-29 22:10:59 +00:00
|
|
|
if (\strpos($genericObjectType->getClassName(), '\\') !== \false) {
|
2021-03-13 15:15:10 +00:00
|
|
|
return '\\' . $genericObjectType->getClassName();
|
|
|
|
}
|
|
|
|
return $genericObjectType->getClassName();
|
|
|
|
}
|
2020-01-14 17:59:15 +00:00
|
|
|
}
|