From 322f3ad55cf6a8670a1d86e06c5d75581f59e2cb Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Fri, 10 May 2024 03:29:37 +0000 Subject: [PATCH] Updated Rector to commit 48fc55d89350f24521a197c04b5512452a3b50fc https://github.com/rectorphp/rector-src/commit/48fc55d89350f24521a197c04b5512452a3b50fc [StaticTypeMapper] Clean up NameNodeMapper check Scalar and class exists (#5865) --- src/Application/VersionResolver.php | 4 +- .../LazyContainerFactory.php | 2 +- .../Mapper/PhpParserNodeMapper.php | 11 +-- .../PhpParser/NameNodeMapper.php | 76 ++----------------- 4 files changed, 11 insertions(+), 82 deletions(-) diff --git a/src/Application/VersionResolver.php b/src/Application/VersionResolver.php index 3a200f4ebbce..6d5422e7978a 100644 --- a/src/Application/VersionResolver.php +++ b/src/Application/VersionResolver.php @@ -19,12 +19,12 @@ final class VersionResolver * @api * @var string */ - public const PACKAGE_VERSION = '558f44d5939be4314d76f4850a34be3590e65bfd'; + public const PACKAGE_VERSION = '48fc55d89350f24521a197c04b5512452a3b50fc'; /** * @api * @var string */ - public const RELEASE_DATE = '2024-05-10 10:10:29'; + public const RELEASE_DATE = '2024-05-10 10:25:34'; /** * @var int */ diff --git a/src/DependencyInjection/LazyContainerFactory.php b/src/DependencyInjection/LazyContainerFactory.php index 2831594d534a..0887fedb13f8 100644 --- a/src/DependencyInjection/LazyContainerFactory.php +++ b/src/DependencyInjection/LazyContainerFactory.php @@ -234,7 +234,7 @@ final class LazyContainerFactory /** * @var array> */ - private const PHP_PARSER_NODE_MAPPER_CLASSES = [ExprNodeMapper::class, FullyQualifiedNodeMapper::class, IdentifierNodeMapper::class, IntersectionTypeNodeMapper::class, NameNodeMapper::class, NullableTypeNodeMapper::class, StringNodeMapper::class, UnionTypeNodeMapper::class]; + private const PHP_PARSER_NODE_MAPPER_CLASSES = [FullyQualifiedNodeMapper::class, IdentifierNodeMapper::class, IntersectionTypeNodeMapper::class, NameNodeMapper::class, NullableTypeNodeMapper::class, StringNodeMapper::class, UnionTypeNodeMapper::class, ExprNodeMapper::class]; /** * @var array> */ diff --git a/src/StaticTypeMapper/Mapper/PhpParserNodeMapper.php b/src/StaticTypeMapper/Mapper/PhpParserNodeMapper.php index 93db7be01018..d0f27004d078 100644 --- a/src/StaticTypeMapper/Mapper/PhpParserNodeMapper.php +++ b/src/StaticTypeMapper/Mapper/PhpParserNodeMapper.php @@ -4,10 +4,8 @@ namespace Rector\StaticTypeMapper\Mapper; use PhpParser\Node; -use PhpParser\Node\Expr; use PhpParser\Node\Name; use PhpParser\Node\Name\FullyQualified; -use PhpParser\Node\Scalar\String_; use PHPStan\Type\Type; use Rector\Exception\NotImplementedYetException; use Rector\NodeTypeResolver\Node\AttributeKey; @@ -33,14 +31,7 @@ public function mapToPHPStanType(Node $node) : Type if (!\is_a($nameOrExpr, $phpParserNodeMapper->getNodeType())) { continue; } - // do not let Expr collect all the types - // note: can be solve later with priorities on mapper interface, making this last - if ($phpParserNodeMapper->getNodeType() !== Expr::class) { - return $phpParserNodeMapper->mapToPHPStan($nameOrExpr); - } - if (!$nameOrExpr instanceof String_) { - return $phpParserNodeMapper->mapToPHPStan($nameOrExpr); - } + return $phpParserNodeMapper->mapToPHPStan($nameOrExpr); } throw new NotImplementedYetException(\get_class($nameOrExpr)); } diff --git a/src/StaticTypeMapper/PhpParser/NameNodeMapper.php b/src/StaticTypeMapper/PhpParser/NameNodeMapper.php index 00e2abeda065..1f1eaf4a63fc 100644 --- a/src/StaticTypeMapper/PhpParser/NameNodeMapper.php +++ b/src/StaticTypeMapper/PhpParser/NameNodeMapper.php @@ -6,23 +6,13 @@ use PhpParser\Node; use PhpParser\Node\Name; use PHPStan\Reflection\ClassReflection; -use PHPStan\Reflection\ReflectionProvider; -use PHPStan\Type\ArrayType; -use PHPStan\Type\BooleanType; -use PHPStan\Type\Constant\ConstantBooleanType; -use PHPStan\Type\FloatType; -use PHPStan\Type\IntegerType; use PHPStan\Type\MixedType; use PHPStan\Type\ObjectWithoutClassType; use PHPStan\Type\StaticType; -use PHPStan\Type\StringType; -use PHPStan\Type\ThisType; use PHPStan\Type\Type; -use Rector\Configuration\RenamedClassesDataCollector; use Rector\Enum\ObjectReference; use Rector\Reflection\ReflectionResolver; use Rector\StaticTypeMapper\Contract\PhpParser\PhpParserNodeMapperInterface; -use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType; use Rector\StaticTypeMapper\ValueObject\Type\ParentObjectWithoutClassType; use Rector\StaticTypeMapper\ValueObject\Type\ParentStaticType; use Rector\StaticTypeMapper\ValueObject\Type\SelfStaticType; @@ -31,25 +21,13 @@ */ final class NameNodeMapper implements PhpParserNodeMapperInterface { - /** - * @readonly - * @var \Rector\Configuration\RenamedClassesDataCollector - */ - private $renamedClassesDataCollector; - /** - * @readonly - * @var \PHPStan\Reflection\ReflectionProvider - */ - private $reflectionProvider; /** * @readonly * @var \Rector\Reflection\ReflectionResolver */ private $reflectionResolver; - public function __construct(RenamedClassesDataCollector $renamedClassesDataCollector, ReflectionProvider $reflectionProvider, ReflectionResolver $reflectionResolver) + public function __construct(ReflectionResolver $reflectionResolver) { - $this->renamedClassesDataCollector = $renamedClassesDataCollector; - $this->reflectionProvider = $reflectionProvider; $this->reflectionResolver = $reflectionResolver; } public function getNodeType() : string @@ -62,22 +40,10 @@ public function getNodeType() : string public function mapToPHPStan(Node $node) : Type { $name = $node->toString(); - if ($this->isExistingClass($name)) { - return new FullyQualifiedObjectType($name); - } - if (\in_array($name, [ObjectReference::STATIC, ObjectReference::SELF, ObjectReference::PARENT], \true)) { + if ($node->isSpecialClassName()) { return $this->createClassReferenceType($node, $name); } - return $this->createScalarType($name); - } - private function isExistingClass(string $name) : bool - { - if ($this->reflectionProvider->hasClass($name)) { - return \true; - } - // to be existing class names - $oldToNewClasses = $this->renamedClassesDataCollector->getOldToNewClasses(); - return \in_array($name, $oldToNewClasses, \true); + return new MixedType(); } /** * @return \PHPStan\Type\MixedType|\PHPStan\Type\StaticType|\Rector\StaticTypeMapper\ValueObject\Type\SelfStaticType|\PHPStan\Type\ObjectWithoutClassType @@ -94,38 +60,10 @@ private function createClassReferenceType(Name $name, string $reference) if ($reference === ObjectReference::SELF) { return new SelfStaticType($classReflection); } - if ($reference === ObjectReference::PARENT) { - $parentClassReflection = $classReflection->getParentClass(); - if ($parentClassReflection instanceof ClassReflection) { - return new ParentStaticType($parentClassReflection); - } - return new ParentObjectWithoutClassType(); + $parentClassReflection = $classReflection->getParentClass(); + if ($parentClassReflection instanceof ClassReflection) { + return new ParentStaticType($parentClassReflection); } - return new ThisType($classReflection); - } - /** - * @return \PHPStan\Type\ArrayType|\PHPStan\Type\IntegerType|\PHPStan\Type\FloatType|\PHPStan\Type\StringType|\PHPStan\Type\Constant\ConstantBooleanType|\PHPStan\Type\BooleanType|\PHPStan\Type\MixedType - */ - private function createScalarType(string $name) - { - if ($name === 'array') { - return new ArrayType(new MixedType(), new MixedType()); - } - if ($name === 'int') { - return new IntegerType(); - } - if ($name === 'float') { - return new FloatType(); - } - if ($name === 'string') { - return new StringType(); - } - if ($name === 'false') { - return new ConstantBooleanType(\false); - } - if ($name === 'bool') { - return new BooleanType(); - } - return new MixedType(); + return new ParentObjectWithoutClassType(); } }