rector/packages/StaticTypeMapper/PhpDocParser/IdentifierTypeMapper.php

110 lines
4.4 KiB
PHP
Raw Normal View History

<?php
declare (strict_types=1);
namespace Rector\StaticTypeMapper\PhpDocParser;
use PhpParser\Node;
use PHPStan\Analyser\NameScope;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\ClassStringType;
use PHPStan\Type\IterableType;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StaticType;
2021-03-21 21:31:18 +00:00
use PHPStan\Type\SubtractableType;
use PHPStan\Type\Type;
2021-03-17 21:54:40 +00:00
use Rector\NodeCollector\ScopeResolver\ParentClassScopeResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\StaticTypeMapper\Contract\PhpDocParser\PhpDocTypeMapperInterface;
use Rector\StaticTypeMapper\Mapper\ScalarStringToTypeMapper;
use Rector\StaticTypeMapper\ValueObject\Type\ParentStaticType;
use Rector\StaticTypeMapper\ValueObject\Type\SelfObjectType;
use Rector\TypeDeclaration\PHPStan\Type\ObjectTypeSpecifier;
final class IdentifierTypeMapper implements \Rector\StaticTypeMapper\Contract\PhpDocParser\PhpDocTypeMapperInterface
{
/**
* @var ScalarStringToTypeMapper
*/
private $scalarStringToTypeMapper;
/**
* @var ObjectTypeSpecifier
*/
private $objectTypeSpecifier;
2021-03-17 21:54:40 +00:00
/**
* @var ParentClassScopeResolver
*/
private $parentClassScopeResolver;
public function __construct(\Rector\TypeDeclaration\PHPStan\Type\ObjectTypeSpecifier $objectTypeSpecifier, \Rector\StaticTypeMapper\Mapper\ScalarStringToTypeMapper $scalarStringToTypeMapper, \Rector\NodeCollector\ScopeResolver\ParentClassScopeResolver $parentClassScopeResolver)
{
$this->scalarStringToTypeMapper = $scalarStringToTypeMapper;
$this->objectTypeSpecifier = $objectTypeSpecifier;
2021-03-17 21:54:40 +00:00
$this->parentClassScopeResolver = $parentClassScopeResolver;
}
/**
* @return class-string<TypeNode>
*/
public function getNodeType() : string
{
return \PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode::class;
}
/**
* @param IdentifierTypeNode $typeNode
*/
public function mapToPHPStanType(\PHPStan\PhpDocParser\Ast\Type\TypeNode $typeNode, \PhpParser\Node $node, \PHPStan\Analyser\NameScope $nameScope) : \PHPStan\Type\Type
{
$type = $this->scalarStringToTypeMapper->mapScalarStringToType($typeNode->name);
if (!$type instanceof \PHPStan\Type\MixedType) {
return $type;
}
if ($type->isExplicitMixed()) {
return $type;
}
$loweredName = \strtolower($typeNode->name);
if ($loweredName === 'class-string') {
return new \PHPStan\Type\ClassStringType();
}
if ($loweredName === 'self') {
return $this->mapSelf($node);
}
if ($loweredName === 'parent') {
return $this->mapParent($node);
}
if ($loweredName === 'static') {
return $this->mapStatic($node);
}
if ($loweredName === 'iterable') {
return new \PHPStan\Type\IterableType(new \PHPStan\Type\MixedType(), new \PHPStan\Type\MixedType());
}
$objectType = new \PHPStan\Type\ObjectType($typeNode->name);
2020-12-11 13:55:24 +00:00
return $this->objectTypeSpecifier->narrowToFullyQualifiedOrAliasedObjectType($node, $objectType);
}
private function mapSelf(\PhpParser\Node $node) : \PHPStan\Type\SubtractableType
{
/** @var string|null $className */
$className = $node->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::CLASS_NAME);
if ($className === null) {
2020-02-01 16:04:38 +00:00
// self outside the class, e.g. in a function
return new \PHPStan\Type\MixedType();
}
return new \Rector\StaticTypeMapper\ValueObject\Type\SelfObjectType($className);
}
private function mapParent(\PhpParser\Node $node) : \PHPStan\Type\Type
{
2021-03-17 21:54:40 +00:00
$parentClassName = $this->parentClassScopeResolver->resolveParentClassName($node);
if ($parentClassName !== null) {
return new \Rector\StaticTypeMapper\ValueObject\Type\ParentStaticType($parentClassName);
}
return new \PHPStan\Type\MixedType();
}
private function mapStatic(\PhpParser\Node $node) : \PHPStan\Type\Type
{
/** @var string|null $className */
$className = $node->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::CLASS_NAME);
if ($className === null) {
return new \PHPStan\Type\MixedType();
}
return new \PHPStan\Type\StaticType($className);
}
}