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