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;
|
2021-11-07 04:15:35 +00:00
|
|
|
use PhpParser\Node\Stmt\ClassLike;
|
2020-01-28 16:39:05 +00:00
|
|
|
use PHPStan\Analyser\NameScope;
|
|
|
|
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
|
|
|
|
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
|
2021-10-26 20:57:53 +00:00
|
|
|
use PHPStan\Reflection\ClassReflection;
|
2021-11-16 11:06:23 +00:00
|
|
|
use PHPStan\Reflection\ReflectionProvider;
|
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-11-07 04:15:35 +00:00
|
|
|
use Rector\Core\PhpParser\Node\BetterNodeFinder;
|
2021-11-09 13:36:07 +00:00
|
|
|
use Rector\NodeNameResolver\NodeNameResolver;
|
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-11-28 18:05:13 +00:00
|
|
|
/**
|
|
|
|
* @implements PhpDocTypeMapperInterface<IdentifierTypeNode>
|
|
|
|
*/
|
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-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
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-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
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-11-07 04:15:35 +00:00
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-11-07 04:15:35 +00:00
|
|
|
* @var \Rector\Core\PhpParser\Node\BetterNodeFinder
|
|
|
|
*/
|
|
|
|
private $betterNodeFinder;
|
2021-11-09 13:36:07 +00:00
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-11-09 13:36:07 +00:00
|
|
|
* @var \Rector\NodeNameResolver\NodeNameResolver
|
|
|
|
*/
|
|
|
|
private $nodeNameResolver;
|
2021-11-16 11:06:23 +00:00
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-11-16 11:06:23 +00:00
|
|
|
* @var \PHPStan\Reflection\ReflectionProvider
|
|
|
|
*/
|
|
|
|
private $reflectionProvider;
|
|
|
|
public function __construct(\Rector\TypeDeclaration\PHPStan\Type\ObjectTypeSpecifier $objectTypeSpecifier, \Rector\StaticTypeMapper\Mapper\ScalarStringToTypeMapper $scalarStringToTypeMapper, \Rector\Core\PhpParser\Node\BetterNodeFinder $betterNodeFinder, \Rector\NodeNameResolver\NodeNameResolver $nodeNameResolver, \PHPStan\Reflection\ReflectionProvider $reflectionProvider)
|
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-11-07 04:15:35 +00:00
|
|
|
$this->betterNodeFinder = $betterNodeFinder;
|
2021-11-09 13:36:07 +00:00
|
|
|
$this->nodeNameResolver = $nodeNameResolver;
|
2021-11-16 11:06:23 +00:00
|
|
|
$this->reflectionProvider = $reflectionProvider;
|
2020-01-28 16:39:05 +00:00
|
|
|
}
|
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-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-11-16 11:06:23 +00:00
|
|
|
return $this->mapParent($node);
|
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-11-16 11:06:23 +00:00
|
|
|
return $this->mapStatic($node);
|
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-11-15 09:34:38 +00:00
|
|
|
$scope = $node->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::SCOPE);
|
2021-10-26 13:57:56 +00:00
|
|
|
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
|
|
|
{
|
2021-11-07 04:15:35 +00:00
|
|
|
// @todo check FQN
|
2021-11-16 11:06:23 +00:00
|
|
|
$className = $this->resolveClassName($node);
|
2021-11-07 04:15:35 +00:00
|
|
|
if (!\is_string($className)) {
|
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-11-16 11:06:23 +00:00
|
|
|
private function mapParent(\PhpParser\Node $node)
|
2020-01-28 16:39:05 +00:00
|
|
|
{
|
2021-11-16 11:06:23 +00:00
|
|
|
$className = $this->resolveClassName($node);
|
|
|
|
if (!\is_string($className)) {
|
|
|
|
// parent outside the class, e.g. in a function
|
|
|
|
return new \PHPStan\Type\MixedType();
|
|
|
|
}
|
|
|
|
/** @var ClassReflection $classReflection */
|
|
|
|
$classReflection = $this->reflectionProvider->getClass($className);
|
|
|
|
$parentClassReflection = $classReflection->getParentClass();
|
2021-10-26 23:10:48 +00:00
|
|
|
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-11-16 11:06:23 +00:00
|
|
|
private function mapStatic(\PhpParser\Node $node)
|
2020-01-28 16:39:05 +00:00
|
|
|
{
|
2021-11-16 11:06:23 +00:00
|
|
|
$className = $this->resolveClassName($node);
|
|
|
|
if (!\is_string($className)) {
|
|
|
|
// static outside the class, e.g. in a function
|
2021-11-07 04:15:35 +00:00
|
|
|
return new \PHPStan\Type\MixedType();
|
2021-10-26 20:57:53 +00:00
|
|
|
}
|
2021-11-16 11:06:23 +00:00
|
|
|
/** @var ClassReflection $classReflection */
|
|
|
|
$classReflection = $this->reflectionProvider->getClass($className);
|
2021-10-26 20:57:53 +00:00
|
|
|
return new \PHPStan\Type\StaticType($classReflection);
|
2020-01-28 16:39:05 +00:00
|
|
|
}
|
2021-11-16 11:06:23 +00:00
|
|
|
private function resolveClassName(\PhpParser\Node $node) : ?string
|
|
|
|
{
|
|
|
|
$classLike = $node instanceof \PhpParser\Node\Stmt\ClassLike ? $node : $this->betterNodeFinder->findParentType($node, \PhpParser\Node\Stmt\ClassLike::class);
|
|
|
|
if (!$classLike instanceof \PhpParser\Node\Stmt\ClassLike) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$className = $this->nodeNameResolver->getName($classLike);
|
|
|
|
if (!\is_string($className)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (!$this->reflectionProvider->hasClass($className)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $className;
|
|
|
|
}
|
2020-01-28 16:39:05 +00:00
|
|
|
}
|