Skip to content

Commit

Permalink
[PHPStan 1.0] Remove sorted union types, now handled by default (#1112)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Oct 30, 2021
1 parent 95ad690 commit 6a894b4
Show file tree
Hide file tree
Showing 30 changed files with 67 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
use PHPStan\Type\NullType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use PHPStan\Type\VerbosityLevel;
use Rector\StaticTypeMapper\TypeFactory\UnionTypeFactory;
use Rector\Tests\NodeTypeResolver\PerNodeTypeResolver\AbstractNodeTypeResolverTest;
use Rector\Tests\NodeTypeResolver\PerNodeTypeResolver\PropertyTypeResolver\Source\ClassThatExtendsHtml;
use Rector\Tests\NodeTypeResolver\PerNodeTypeResolver\PropertyTypeResolver\Source\Html;
Expand Down Expand Up @@ -42,14 +42,11 @@ public function test(string $file, int $nodePosition, Type $expectedType): void

public function provideData(): Iterator
{
$unionTypeFactory = new UnionTypeFactory();

yield [__DIR__ . '/Source/MethodParamDocBlock.php', 0, new ObjectType(Html::class)];

yield [__DIR__ . '/Source/MethodParamDocBlock.php', 1, new ObjectType(ClassThatExtendsHtml::class)];

// mimics failing test from DomainDrivenDesign set
$unionType = $unionTypeFactory->createUnionObjectType([SomeChild::class, new NullType()]);
$unionType = new UnionType([new ObjectType(SomeChild::class), new NullType()]);
yield [__DIR__ . '/Source/ActionClass.php', 0, $unionType];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@

namespace Rector\Tests\NodeTypeResolver\PerNodeTypeResolver\TraitTypeResolver;

use Iterator;
use PhpParser\Node\Stmt\Trait_;
use PHPStan\Type\Type;
use PHPStan\Type\ObjectType;
use PHPStan\Type\UnionType;
use Rector\StaticTypeMapper\TypeFactory\UnionTypeFactory;
use Rector\Tests\NodeTypeResolver\PerNodeTypeResolver\AbstractNodeTypeResolverTest;
use Rector\Tests\NodeTypeResolver\PerNodeTypeResolver\TraitTypeResolver\Source\AnotherTrait;
use Rector\Tests\NodeTypeResolver\PerNodeTypeResolver\TraitTypeResolver\Source\TraitWithTrait;
Expand All @@ -18,28 +16,21 @@
*/
final class TraitTypeResolverTest extends AbstractNodeTypeResolverTest
{
/**
* @dataProvider provideData()
*/
public function test(string $file, int $nodePosition, Type $expectedType): void
public function test(): void
{
$variableNodes = $this->getNodesForFileOfType($file, Trait_::class);
$variableNodes = $this->getNodesForFileOfType(__DIR__ . '/Source/TraitWithTrait.php', Trait_::class);

$resolvedType = $this->nodeTypeResolver->getType($variableNodes[$nodePosition]);
$this->assertEquals($expectedType, $resolvedType);
$resolvedType = $this->nodeTypeResolver->getType($variableNodes[0]);
$expectedUnionType = $this->createExpectedType();

$this->assertEquals($expectedUnionType, $resolvedType);
}

/**
* @return Iterator<int[]|string[]|UnionType[]>
*/
public function provideData(): Iterator
private function createExpectedType(): UnionType
{
$unionTypeFactory = new UnionTypeFactory();
$anotherTraitObjectType = new ObjectType(AnotherTrait::class);
$traitWithTraitObjectType = new ObjectType(TraitWithTrait::class);

yield [
__DIR__ . '/Source/TraitWithTrait.php',
0,
$unionTypeFactory->createUnionObjectType([AnotherTrait::class, TraitWithTrait::class]),
];
return new UnionType([$anotherTraitObjectType, $traitWithTraitObjectType]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Rector\Tests\NodeTypeResolver\TypeComparator;

use Iterator;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\ArrayType;
use PHPStan\Type\ClassStringType;
use PHPStan\Type\Constant\ConstantArrayType;
Expand All @@ -13,47 +13,47 @@
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StringType;
use PHPStan\Type\UnionType;
use Rector\NodeTypeResolver\TypeComparator\ArrayTypeComparator;
use Rector\StaticTypeMapper\TypeFactory\UnionTypeFactory;
use Rector\Testing\PHPUnit\AbstractTestCase;
use Rector\Tests\NodeTypeResolver\TypeComparator\Source\SomeGenericTypeObject;

final class ArrayTypeComparatorTest extends AbstractTestCase
{
private ArrayTypeComparator $arrayTypeComparator;

private ReflectionProvider $reflectionProvider;

protected function setUp(): void
{
$this->boot();
$this->arrayTypeComparator = $this->getService(ArrayTypeComparator::class);
$this->reflectionProvider = $this->getService(ReflectionProvider::class);
}

/**
* @dataProvider provideData()
*/
public function test(ArrayType $firstArrayType, ArrayType $secondArrayType, bool $areExpectedEqual): void
public function testClassStringSubtype(): void
{
$areEqual = $this->arrayTypeComparator->isSubtype($firstArrayType, $secondArrayType);
$this->assertSame($areExpectedEqual, $areEqual);
$classStringKeysArrayType = new ArrayType(new StringType(), new ClassStringType());
$stringArrayType = new ArrayType(new StringType(), new MixedType());

$isSubtypeActual = $this->arrayTypeComparator->isSubtype($classStringKeysArrayType, $stringArrayType);
$this->assertTrue($isSubtypeActual);
}

/**
* @return Iterator<ArrayType[]|bool[]>
*/
public function provideData(): Iterator
public function testGenericObjectType(): void
{
$unionTypeFactory = new UnionTypeFactory();
$someGenericTypeObjectClassReflection = $this->reflectionProvider->getClass(SomeGenericTypeObject::class);
$objectType = new ObjectType(SomeGenericTypeObject::class, null, $someGenericTypeObjectClassReflection);
$genericClassStringType = new GenericClassStringType($objectType);

$classStringKeysArrayType = new ArrayType(new StringType(), new ClassStringType());
$stringArrayType = new ArrayType(new StringType(), new MixedType());
yield [$stringArrayType, $classStringKeysArrayType, false];

$genericClassStringType = new GenericClassStringType(new ObjectType(SomeGenericTypeObject::class));
$constantArrayType = new ConstantArrayType(
[new ConstantIntegerType(0)],
[$unionTypeFactory->createUnionObjectType([$genericClassStringType, $genericClassStringType])]
[new UnionType([$genericClassStringType, $genericClassStringType])]
);

yield [$constantArrayType, $stringArrayType, false];
$stringArrayType = new ArrayType(new StringType(), new MixedType());

$isSubtypeActual = $this->arrayTypeComparator->isSubtype($constantArrayType, $stringArrayType);
$this->assertFalse($isSubtypeActual);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,19 @@
use PHPStan\Type\ObjectType;
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use Rector\Core\Enum\ObjectReference;
use Rector\Core\NodeAnalyzer\ClassAnalyzer;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Contract\NodeTypeResolverInterface;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\StaticTypeMapper\TypeFactory\UnionTypeFactory;
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;

final class NewTypeResolver implements NodeTypeResolverInterface
{
public function __construct(
private NodeNameResolver $nodeNameResolver,
private ClassAnalyzer $classAnalyzer,
private UnionTypeFactory $unionTypeFactory
) {
}

Expand Down Expand Up @@ -89,7 +88,7 @@ private function resolveAnonymousClassType(New_ $new): ObjectWithoutClassType
}

if (count($types) > 1) {
$unionType = $this->unionTypeFactory->createUnionObjectType($types);
$unionType = new UnionType($types);
return new ObjectWithoutClassType($unionType);
}

Expand Down
5 changes: 2 additions & 3 deletions packages/NodeTypeResolver/PHPStan/Type/TypeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeUtils;
use PHPStan\Type\UnionType;
use Rector\NodeTypeResolver\PHPStan\TypeHasher;
use Rector\StaticTypeMapper\TypeFactory\UnionTypeFactory;

final class TypeFactory
{
public function __construct(
private UnionTypeFactory $unionTypeFactory,
private TypeHasher $typeHasher,
) {
}
Expand Down Expand Up @@ -122,7 +121,7 @@ private function createUnionOrSingleType(array $types): Type
return $types[0];
}

return $this->unionTypeFactory->createUnionObjectType($types);
return new UnionType($types);
}

private function removeValueFromConstantType(Type $type): Type
Expand Down
6 changes: 1 addition & 5 deletions packages/NodeTypeResolver/PHPStan/TypeHasher.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use PHPStan\Type\TypeTraverser;
use PHPStan\Type\TypeWithClassName;
use PHPStan\Type\UnionType;
use PHPStan\Type\UnionTypeHelper;
use PHPStan\Type\VerbosityLevel;
use Rector\StaticTypeMapper\ValueObject\Type\AliasedObjectType;
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
Expand Down Expand Up @@ -87,15 +86,12 @@ private function resolveUniqueTypeWithClassNameHash(TypeWithClassName $typeWithC

private function createUnionTypeHash(UnionType $unionType): string
{
$sortedTypes = UnionTypeHelper::sortTypes($unionType->getTypes());
$sortedUnionType = new UnionType($sortedTypes);

$booleanType = new BooleanType();
if ($booleanType->isSuperTypeOf($unionType)->yes()) {
return $booleanType->describe(VerbosityLevel::precise());
}

$normalizedUnionType = clone $sortedUnionType;
$normalizedUnionType = clone $unionType;

// change alias to non-alias
$normalizedUnionType = TypeTraverser::map(
Expand Down
8 changes: 1 addition & 7 deletions packages/PHPStanStaticTypeMapper/Utils/TypeUnwrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,9 @@
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeWithClassName;
use PHPStan\Type\UnionType;
use Rector\StaticTypeMapper\TypeFactory\UnionTypeFactory;

final class TypeUnwrapper
{
public function __construct(
private UnionTypeFactory $unionTypeFactory
) {
}

/**
* E.g. null|ClassType → ClassType
*/
Expand Down Expand Up @@ -54,6 +48,6 @@ public function removeNullTypeFromUnionType(UnionType $unionType): UnionType
$unionedTypesWithoutNullType[] = $type;
}

return $this->unionTypeFactory->createUnionObjectType($unionedTypesWithoutNullType);
return new UnionType($unionedTypesWithoutNullType);
}
}
36 changes: 0 additions & 36 deletions packages/StaticTypeMapper/TypeFactory/UnionTypeFactory.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace Rector\Tests\CodeQuality\Rector\Class_\CompleteDynamicPropertiesRector

class MultipleTypes
{
public int|string|bool $value;
public bool|int|string $value;
public function set()
{
$this->value = 5;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ $ids = array_map(
$ids
);

?>
?>
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use PhpParser\Node\Name;

if (! function_exists('some_helper')) {
/**
* @param \PhpParser\Node\Name|mixed[] $item
* @param mixed[]|\PhpParser\Node\Name $item
*/
function some_helper($item)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace Rector\Tests\Php74\Rector\Property\TypedPropertyRector\FixtureUnionTyp

class SetIfElse
{
public string|int $stringOrInteger = 'hi';
public int|string $stringOrInteger = 'hi';

public function setNumber()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Rector\Tests\Php80\Rector\FunctionLike\UnionTypesRector\Fixture;

final class RemoveDocArrayTyped
{
public function normalizeNodeValue($value): bool|float|int|string|array
public function normalizeNodeValue($value): array|bool|float|int|string
{
return $value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Rector\Tests\Php80\Rector\FunctionLike\UnionTypesRector\Fixture;

final class RemoveDocArrayTyped2
{
public function normalizeNodeValue(bool|float|int|string|array $value)
public function normalizeNodeValue(array|bool|float|int|string $value)
{
return $value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Rector\Tests\Php80\Rector\FunctionLike\UnionTypesRector\Fixture;

final class FalsePseudoType
{
public function go($value): false|int
public function go($value): int|false
{
return (int) $value ?? false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use PhpParser\Node\Stmt\Property;

final class ParamDoc
{
public function addAsFirstMethod(Class_ $class, \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Property|\PhpParser\Node\Stmt\ClassConst $stmt): void
public function addAsFirstMethod(Class_ $class, \PhpParser\Node\Stmt\ClassConst|\PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Property $stmt): void
{
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddArrayReturnDocTypeR
final class HasOffset
{
/**
* @return mixed|mixed[]
* @return mixed[]|mixed
*/
private function convertArguments($service)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ final class RespectArrayReturnType
private $items = [];

/**
* @return mixed[]|array<string, mixed[]>|array<string, int>
* @return array<string, mixed[]>
*/
public function process($message): array
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ final class DoNotDuplicateArrayReturn
/**
* @return bool|mixed[]|float|string[]
*/
public function go($number): bool|array|float
public function go($number): array|bool|float
{
return execute();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ class PrivateVarNullUnused
private $config = null;
}

?>
?>

0 comments on commit 6a894b4

Please sign in to comment.