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;
|
2021-10-26 20:57:53 +00:00
|
|
|
use PHPStan\Analyser\Scope;
|
2020-01-28 16:39:05 +00:00
|
|
|
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
|
|
|
|
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
|
2021-10-26 20:57:53 +00:00
|
|
|
use PHPStan\Reflection\ClassReflection;
|
2020-01-28 16:39:05 +00:00
|
|
|
use PHPStan\Type\ClassStringType;
|
|
|
|
use PHPStan\Type\IterableType;
|
|
|
|
use PHPStan\Type\MixedType;
|
|
|
|
use PHPStan\Type\ObjectType;
|
|
|
|
use PHPStan\Type\StaticType;
|
|
|
|
use PHPStan\Type\Type;
|
2021-10-25 13:44:53 +00:00
|
|
|
use Rector\Core\Enum\ObjectReference;
|
2021-10-26 20:57:53 +00:00
|
|
|
use Rector\Core\Exception\ShouldNotHappenException;
|
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
|
|
|
{
|
|
|
|
/**
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\TypeDeclaration\PHPStan\Type\ObjectTypeSpecifier
|
2020-01-28 16:39:05 +00:00
|
|
|
*/
|
2021-05-10 23:39:21 +00:00
|
|
|
private $objectTypeSpecifier;
|
2020-01-28 16:39:05 +00:00
|
|
|
/**
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\StaticTypeMapper\Mapper\ScalarStringToTypeMapper
|
2020-01-28 16:39:05 +00:00
|
|
|
*/
|
2021-05-10 23:39:21 +00:00
|
|
|
private $scalarStringToTypeMapper;
|
2021-03-17 21:54:40 +00:00
|
|
|
/**
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\NodeCollector\ScopeResolver\ParentClassScopeResolver
|
2021-03-17 21:54:40 +00:00
|
|
|
*/
|
|
|
|
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->objectTypeSpecifier = $objectTypeSpecifier;
|
2021-05-10 23:39:21 +00:00
|
|
|
$this->scalarStringToTypeMapper = $scalarStringToTypeMapper;
|
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-08-23 00:20:32 +00:00
|
|
|
* @param \PHPStan\PhpDocParser\Ast\Type\TypeNode $typeNode
|
|
|
|
* @param \PhpParser\Node $node
|
|
|
|
* @param \PHPStan\Analyser\NameScope $nameScope
|
2020-01-28 16:39:05 +00:00
|
|
|
*/
|
2021-07-05 22:50:18 +00:00
|
|
|
public function mapToPHPStanType($typeNode, $node, $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
|
|
|
}
|
2021-10-26 20:57:53 +00:00
|
|
|
$scope = $node->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::SCOPE);
|
2021-10-25 13:44:53 +00:00
|
|
|
if ($loweredName === \Rector\Core\Enum\ObjectReference::SELF()->getValue()) {
|
2020-01-28 16:39:05 +00:00
|
|
|
return $this->mapSelf($node);
|
|
|
|
}
|
2021-10-25 13:44:53 +00:00
|
|
|
if ($loweredName === \Rector\Core\Enum\ObjectReference::PARENT()->getValue()) {
|
2021-10-26 23:10:48 +00:00
|
|
|
return $this->mapParent($scope);
|
2020-01-28 16:39:05 +00:00
|
|
|
}
|
2021-10-25 13:44:53 +00:00
|
|
|
if ($loweredName === \Rector\Core\Enum\ObjectReference::STATIC()->getValue()) {
|
2021-10-26 20:57:53 +00:00
|
|
|
return $this->mapStatic($node, $scope);
|
2020-01-28 16:39:05 +00:00
|
|
|
}
|
|
|
|
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);
|
2021-10-26 13:57:56 +00:00
|
|
|
$scope = $node->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::SCOPE);
|
|
|
|
return $this->objectTypeSpecifier->narrowToFullyQualifiedOrAliasedObjectType($node, $objectType, $scope);
|
2020-01-28 16:39:05 +00:00
|
|
|
}
|
2021-06-18 17:31:16 +00:00
|
|
|
/**
|
|
|
|
* @return \PHPStan\Type\MixedType|\Rector\StaticTypeMapper\ValueObject\Type\SelfObjectType
|
|
|
|
*/
|
2021-07-05 23:06:30 +00:00
|
|
|
private function mapSelf(\PhpParser\Node $node)
|
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-06-18 17:31:16 +00:00
|
|
|
/**
|
2021-10-30 14:18:31 +00:00
|
|
|
* @return \PHPStan\Type\MixedType|\Rector\StaticTypeMapper\ValueObject\Type\ParentStaticType
|
2021-06-18 17:31:16 +00:00
|
|
|
*/
|
2021-10-26 23:10:48 +00:00
|
|
|
private function mapParent(\PHPStan\Analyser\Scope $scope)
|
2020-01-28 16:39:05 +00:00
|
|
|
{
|
2021-10-26 23:10:48 +00:00
|
|
|
$parentClassReflection = $this->parentClassScopeResolver->resolveParentClassReflection($scope);
|
|
|
|
if (!$parentClassReflection instanceof \PHPStan\Reflection\ClassReflection) {
|
2021-10-26 20:57:53 +00:00
|
|
|
return new \PHPStan\Type\MixedType();
|
2020-01-28 16:39:05 +00:00
|
|
|
}
|
2021-10-26 23:10:48 +00:00
|
|
|
return new \Rector\StaticTypeMapper\ValueObject\Type\ParentStaticType($parentClassReflection);
|
2020-01-28 16:39:05 +00:00
|
|
|
}
|
2021-06-18 17:31:16 +00:00
|
|
|
/**
|
|
|
|
* @return \PHPStan\Type\MixedType|\PHPStan\Type\StaticType
|
|
|
|
*/
|
2021-10-26 20:57:53 +00:00
|
|
|
private function mapStatic(\PhpParser\Node $node, \PHPStan\Analyser\Scope $scope)
|
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-10-26 20:57:53 +00:00
|
|
|
$classReflection = $scope->getClassReflection();
|
|
|
|
if (!$classReflection instanceof \PHPStan\Reflection\ClassReflection) {
|
|
|
|
throw new \Rector\Core\Exception\ShouldNotHappenException();
|
|
|
|
}
|
|
|
|
return new \PHPStan\Type\StaticType($classReflection);
|
2020-01-28 16:39:05 +00:00
|
|
|
}
|
|
|
|
}
|