rector/packages/StaticTypeMapper/PhpDocParser/IdentifierTypeMapper.php

121 lines
4.9 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;
use PHPStan\Type\Type;
use Rector\Core\Enum\ObjectReference;
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 \Rector\TypeDeclaration\PHPStan\Type\ObjectTypeSpecifier
*/
private $objectTypeSpecifier;
/**
* @var \Rector\StaticTypeMapper\Mapper\ScalarStringToTypeMapper
*/
private $scalarStringToTypeMapper;
2021-03-17 21:54:40 +00:00
/**
* @var \Rector\NodeCollector\ScopeResolver\ParentClassScopeResolver
2021-03-17 21:54:40 +00:00
*/
private $parentClassScopeResolver;
public function __construct(\Rector\TypeDeclaration\PHPStan\Type\ObjectTypeSpecifier $objectTypeSpecifier, \Rector\StaticTypeMapper\Mapper\ScalarStringToTypeMapper $scalarStringToTypeMapper, \Rector\NodeCollector\ScopeResolver\ParentClassScopeResolver $parentClassScopeResolver)
{
$this->objectTypeSpecifier = $objectTypeSpecifier;
$this->scalarStringToTypeMapper = $scalarStringToTypeMapper;
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 \PHPStan\PhpDocParser\Ast\Type\TypeNode $typeNode
* @param \PhpParser\Node $node
* @param \PHPStan\Analyser\NameScope $nameScope
*/
public function mapToPHPStanType($typeNode, $node, $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 === \Rector\Core\Enum\ObjectReference::SELF()->getValue()) {
return $this->mapSelf($node);
}
if ($loweredName === \Rector\Core\Enum\ObjectReference::PARENT()->getValue()) {
return $this->mapParent($node);
}
if ($loweredName === \Rector\Core\Enum\ObjectReference::STATIC()->getValue()) {
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);
}
/**
* @return \PHPStan\Type\MixedType|\Rector\StaticTypeMapper\ValueObject\Type\SelfObjectType
*/
private function mapSelf(\PhpParser\Node $node)
{
/** @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);
}
/**
* @return \Rector\StaticTypeMapper\ValueObject\Type\ParentStaticType|\PHPStan\Type\MixedType
*/
private function mapParent(\PhpParser\Node $node)
{
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();
}
/**
* @return \PHPStan\Type\MixedType|\PHPStan\Type\StaticType
*/
private function mapStatic(\PhpParser\Node $node)
{
/** @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);
}
}