Skip to content

Commit

Permalink
Updated Rector to commit 3c2100b3a8d2372094135efa9c4d0433078337b5
Browse files Browse the repository at this point in the history
rectorphp/rector-src@3c2100b Union mapper cleanup (#6094)
  • Loading branch information
TomasVotruba committed Jul 1, 2024
1 parent 738f339 commit 78890de
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 145 deletions.
4 changes: 2 additions & 2 deletions src/Application/VersionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = 'ef333502fb722ef471049b492a257781d02d0d62';
public const PACKAGE_VERSION = '3c2100b3a8d2372094135efa9c4d0433078337b5';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2024-06-30 21:58:00';
public const RELEASE_DATE = '2024-07-01 09:15:52';
/**
* @var int
*/
Expand Down
26 changes: 0 additions & 26 deletions src/PHPStanStaticTypeMapper/TypeAnalyzer/UnionTypeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,10 @@
declare (strict_types=1);
namespace Rector\PHPStanStaticTypeMapper\TypeAnalyzer;

use PHPStan\Type\ArrayType;
use PHPStan\Type\IterableType;
use PHPStan\Type\NullType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\UnionType;
use Rector\PHPStanStaticTypeMapper\ValueObject\UnionTypeAnalysis;
use Traversable;
final class UnionTypeAnalyzer
{
public function analyseForArrayAndIterable(UnionType $unionType) : ?UnionTypeAnalysis
{
$hasIterable = \false;
$hasArray = \false;
foreach ($unionType->getTypes() as $unionedType) {
if ($unionedType instanceof IterableType) {
$hasIterable = \true;
continue;
}
if ($unionedType instanceof ArrayType) {
$hasArray = \true;
continue;
}
if ($unionedType instanceof ObjectType && $unionedType->getClassName() === Traversable::class) {
$hasIterable = \true;
continue;
}
return null;
}
return new UnionTypeAnalysis($hasIterable, $hasArray);
}
public function isNullable(UnionType $unionType, bool $checkTwoTypes = \false) : bool
{
$types = $unionType->getTypes();
Expand Down
83 changes: 4 additions & 79 deletions src/PHPStanStaticTypeMapper/TypeMapper/UnionTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,16 @@
use PhpParser\Node\UnionType as PhpParserUnionType;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\IterableType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NullType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use PHPStan\Type\VoidType;
use Rector\BetterPhpDocParser\ValueObject\Type\BracketsAwareUnionTypeNode;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\Php\PhpVersionProvider;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper;
use Rector\PHPStanStaticTypeMapper\TypeAnalyzer\UnionTypeAnalyzer;
use Rector\PHPStanStaticTypeMapper\ValueObject\UnionTypeAnalysis;
use Rector\ValueObject\PhpVersionFeature;
use RectorPrefix202407\Webmozart\Assert\Assert;
use RectorPrefix202407\Webmozart\Assert\InvalidArgumentException;
Expand All @@ -40,25 +36,13 @@ final class UnionTypeMapper implements TypeMapperInterface
* @var \Rector\Php\PhpVersionProvider
*/
private $phpVersionProvider;
/**
* @readonly
* @var \Rector\PHPStanStaticTypeMapper\TypeAnalyzer\UnionTypeAnalyzer
*/
private $unionTypeAnalyzer;
/**
* @readonly
* @var \Rector\NodeNameResolver\NodeNameResolver
*/
private $nodeNameResolver;
/**
* @var \Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper
*/
private $phpStanStaticTypeMapper;
public function __construct(PhpVersionProvider $phpVersionProvider, UnionTypeAnalyzer $unionTypeAnalyzer, NodeNameResolver $nodeNameResolver)
public function __construct(PhpVersionProvider $phpVersionProvider)
{
$this->phpVersionProvider = $phpVersionProvider;
$this->unionTypeAnalyzer = $unionTypeAnalyzer;
$this->nodeNameResolver = $nodeNameResolver;
}
public function autowire(PHPStanStaticTypeMapper $phpStanStaticTypeMapper) : void
{
Expand All @@ -73,16 +57,10 @@ public function getNodeClass() : string
*/
public function mapToPHPStanPhpDocTypeNode(Type $type) : TypeNode
{
// note: cannot be handled by PHPStan as uses no-space around |
$unionTypesNodes = [];
$skipIterable = $this->shouldSkipIterable($type);
foreach ($type->getTypes() as $unionedType) {
if ($unionedType instanceof IterableType && $skipIterable) {
continue;
}
$unionTypesNodes[] = $this->phpStanStaticTypeMapper->mapToPHPStanPhpDocTypeNode($unionedType);
}
$unionTypesNodes = \array_unique($unionTypesNodes);
return new BracketsAwareUnionTypeNode($unionTypesNodes);
}
/**
Expand All @@ -91,16 +69,12 @@ public function mapToPHPStanPhpDocTypeNode(Type $type) : TypeNode
public function mapToPhpParserNode(Type $type, string $typeKind) : ?Node
{
// special case for nullable
$nullabledType = $this->matchTypeForNullableUnionType($type);
if (!$nullabledType instanceof Type) {
return $this->matchTypeForUnionedTypes($type, $typeKind);
}
return $this->mapNullabledType($nullabledType, $typeKind);
return $this->matchTypeForUnionedTypes($type, $typeKind);
}
/**
* @return PhpParserUnionType|\PhpParser\Node\NullableType|null
*/
public function resolveTypeWithNullablePHPParserUnionType(PhpParserUnionType $phpParserUnionType)
private function resolveTypeWithNullablePHPParserUnionType(PhpParserUnionType $phpParserUnionType)
{
$totalTypes = \count($phpParserUnionType->types);
if ($totalTypes === 2) {
Expand Down Expand Up @@ -140,69 +114,20 @@ private function resolveNullableType(NullableType $nullableType)
if (!$type instanceof PHPParserNodeIntersectionType) {
return $nullableType;
}
if (!$this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::INTERSECTION_TYPES)) {
if (!$this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::UNION_TYPES)) {
return null;
}
$types = [$type];
$types[] = new Identifier('null');
return new PhpParserUnionType($types);
}
/**
* @param TypeKind::* $typeKind
* @return \PhpParser\Node\NullableType|\PhpParser\Node\ComplexType|PhpParserUnionType|null
*/
private function mapNullabledType(Type $nullabledType, string $typeKind)
{
// void cannot be nullable
if ($nullabledType->isVoid()->yes()) {
return null;
}
$nullabledTypeNode = $this->phpStanStaticTypeMapper->mapToPhpParserNode($nullabledType, $typeKind);
if (!$nullabledTypeNode instanceof Node) {
return null;
}
if (\in_array(\get_class($nullabledTypeNode), [NullableType::class, ComplexType::class], \true)) {
return $nullabledTypeNode;
}
/** @var Name $nullabledTypeNode */
if (!$this->nodeNameResolver->isNames($nullabledTypeNode, ['false', 'mixed'])) {
return $this->resolveNullableType(new NullableType($nullabledTypeNode));
}
return null;
}
private function shouldSkipIterable(UnionType $unionType) : bool
{
$unionTypeAnalysis = $this->unionTypeAnalyzer->analyseForArrayAndIterable($unionType);
if (!$unionTypeAnalysis instanceof UnionTypeAnalysis) {
return \false;
}
if (!$unionTypeAnalysis->hasIterable()) {
return \false;
}
return $unionTypeAnalysis->hasArray();
}
private function resolveUnionTypes(PhpParserUnionType $phpParserUnionType) : ?PhpParserUnionType
{
if (!$this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::UNION_TYPES)) {
return null;
}
return $phpParserUnionType;
}
private function matchTypeForNullableUnionType(UnionType $unionType) : ?Type
{
if (\count($unionType->getTypes()) !== 2) {
return null;
}
$firstType = $unionType->getTypes()[0];
$secondType = $unionType->getTypes()[1];
if ($firstType instanceof NullType) {
return $secondType;
}
if ($secondType instanceof NullType) {
return $firstType;
}
return null;
}
private function hasObjectAndStaticType(PhpParserUnionType $phpParserUnionType) : bool
{
$hasAnonymousObjectType = \false;
Expand Down
31 changes: 0 additions & 31 deletions src/PHPStanStaticTypeMapper/ValueObject/UnionTypeAnalysis.php

This file was deleted.

11 changes: 6 additions & 5 deletions src/StaticTypeMapper/Naming/NameScopeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@ public function __construct(UseImportsResolver $useImportsResolver)
public function createNameScopeFromNodeWithoutTemplateTypes(Node $node) : NameScope
{
$scope = $node->getAttribute(AttributeKey::SCOPE);
$namespace = $scope instanceof Scope ? $scope->getNamespace() : null;
$uses = $this->useImportsResolver->resolve();
$usesAliasesToNames = $this->resolveUseNamesByAlias($uses);
if ($scope instanceof Scope && $scope->getClassReflection() instanceof ClassReflection) {
if ($scope instanceof Scope) {
$namespace = $scope->getNamespace();
$classReflection = $scope->getClassReflection();
$className = $classReflection->getName();
$className = $classReflection instanceof ClassReflection ? $classReflection->getName() : null;
} else {
$namespace = null;
$className = null;
}
$uses = $this->useImportsResolver->resolve();
$usesAliasesToNames = $this->resolveUseNamesByAlias($uses);
return new NameScope($namespace, $usesAliasesToNames, $className);
}
/**
Expand Down
1 change: 0 additions & 1 deletion vendor/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -1711,7 +1711,6 @@
'Rector\\PHPStanStaticTypeMapper\\TypeMapper\\UnionTypeMapper' => $baseDir . '/src/PHPStanStaticTypeMapper/TypeMapper/UnionTypeMapper.php',
'Rector\\PHPStanStaticTypeMapper\\TypeMapper\\VoidTypeMapper' => $baseDir . '/src/PHPStanStaticTypeMapper/TypeMapper/VoidTypeMapper.php',
'Rector\\PHPStanStaticTypeMapper\\Utils\\TypeUnwrapper' => $baseDir . '/src/PHPStanStaticTypeMapper/Utils/TypeUnwrapper.php',
'Rector\\PHPStanStaticTypeMapper\\ValueObject\\UnionTypeAnalysis' => $baseDir . '/src/PHPStanStaticTypeMapper/ValueObject/UnionTypeAnalysis.php',
'Rector\\PHPStan\\NodeVisitor\\ExprScopeFromStmtNodeVisitor' => $baseDir . '/src/PHPStan/NodeVisitor/ExprScopeFromStmtNodeVisitor.php',
'Rector\\PHPStan\\NodeVisitor\\UnreachableStatementNodeVisitor' => $baseDir . '/src/PHPStan/NodeVisitor/UnreachableStatementNodeVisitor.php',
'Rector\\PHPStan\\NodeVisitor\\WrappedNodeRestoringNodeVisitor' => $baseDir . '/src/PHPStan/NodeVisitor/WrappedNodeRestoringNodeVisitor.php',
Expand Down
1 change: 0 additions & 1 deletion vendor/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -1930,7 +1930,6 @@ class ComposerStaticInita0d93d8f497cc171aacd63896104eb68
'Rector\\PHPStanStaticTypeMapper\\TypeMapper\\UnionTypeMapper' => __DIR__ . '/../..' . '/src/PHPStanStaticTypeMapper/TypeMapper/UnionTypeMapper.php',
'Rector\\PHPStanStaticTypeMapper\\TypeMapper\\VoidTypeMapper' => __DIR__ . '/../..' . '/src/PHPStanStaticTypeMapper/TypeMapper/VoidTypeMapper.php',
'Rector\\PHPStanStaticTypeMapper\\Utils\\TypeUnwrapper' => __DIR__ . '/../..' . '/src/PHPStanStaticTypeMapper/Utils/TypeUnwrapper.php',
'Rector\\PHPStanStaticTypeMapper\\ValueObject\\UnionTypeAnalysis' => __DIR__ . '/../..' . '/src/PHPStanStaticTypeMapper/ValueObject/UnionTypeAnalysis.php',
'Rector\\PHPStan\\NodeVisitor\\ExprScopeFromStmtNodeVisitor' => __DIR__ . '/../..' . '/src/PHPStan/NodeVisitor/ExprScopeFromStmtNodeVisitor.php',
'Rector\\PHPStan\\NodeVisitor\\UnreachableStatementNodeVisitor' => __DIR__ . '/../..' . '/src/PHPStan/NodeVisitor/UnreachableStatementNodeVisitor.php',
'Rector\\PHPStan\\NodeVisitor\\WrappedNodeRestoringNodeVisitor' => __DIR__ . '/../..' . '/src/PHPStan/NodeVisitor/WrappedNodeRestoringNodeVisitor.php',
Expand Down

0 comments on commit 78890de

Please sign in to comment.