Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/NodeTypeResolver/PHPStan/Type/TypeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use PHPStan\Type\ObjectType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeUtils;
use PHPStan\Type\UnionType;
use Rector\NodeTypeResolver\PHPStan\ObjectWithoutClassTypeWithParentTypes;
use Rector\NodeTypeResolver\PHPStan\TypeHasher;
Expand Down Expand Up @@ -127,7 +126,7 @@ private function unwrapUnionedTypes(array $types): array
// unwrap union types
$unwrappedTypes = [];
foreach ($types as $type) {
$flattenTypes = TypeUtils::flattenTypes($type);
$flattenTypes = TypeFlattener::flatten($type);

foreach ($flattenTypes as $flattenType) {
if ($flattenType instanceof ConstantArrayType) {
Expand Down Expand Up @@ -202,8 +201,8 @@ private function unwrapConstantArrayTypes(ConstantArrayType $constantArrayType):
{
$unwrappedTypes = [];

$flattenKeyTypes = TypeUtils::flattenTypes($constantArrayType->getIterableKeyType());
$flattenItemTypes = TypeUtils::flattenTypes($constantArrayType->getIterableValueType());
$flattenKeyTypes = TypeFlattener::flatten($constantArrayType->getIterableKeyType());
$flattenItemTypes = TypeFlattener::flatten($constantArrayType->getIterableValueType());

foreach ($flattenItemTypes as $position => $nestedFlattenItemType) {
$nestedFlattenKeyType = $flattenKeyTypes[$position] ?? null;
Expand Down
95 changes: 95 additions & 0 deletions src/NodeTypeResolver/PHPStan/Type/TypeFlattener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

declare(strict_types=1);

namespace Rector\NodeTypeResolver\PHPStan\Type;

use PHPStan\Type\NeverType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\UnionType;

/**
* Local reimplementation of the deprecated PHPStan TypeUtils::flattenTypes().
*/
final class TypeFlattener
{
/**
* @return Type[]
*/
public static function flatten(Type $type): array
{
if ($type instanceof UnionType) {
$types = [];
foreach ($type->getTypes() as $innerType) {
foreach (self::flatten($innerType) as $flattenType) {
$types[] = $flattenType;
}
}

return $types;
}

$constantArrays = $type->getConstantArrays();
if ($constantArrays === []) {
return [$type];
}

// each ConstantArrayType with N optional keys expands to 2^N variants via getAllArrays(),
// and the cartesian product across arrays multiplies those counts - bail out before it explodes
$estimatedCount = 1;
foreach ($constantArrays as $constantArray) {
$optionalCount = count($constantArray->getOptionalKeys());
$arrayCount = $optionalCount <= 20 ? 1 << $optionalCount : PHP_INT_MAX;

if ($arrayCount > 16384 || $estimatedCount > 16384 / max($arrayCount, 1)) {
return [$type];
}

$estimatedCount *= $arrayCount;
}

$allArrays = [];
foreach ($constantArrays as $constantArray) {
$allArrays[] = $constantArray->getAllArrays();
}

$result = [];
foreach (self::combinations($allArrays) as $combination) {
$intersected = $combination[0];
for ($i = 1, $count = count($combination); $i < $count; ++$i) {
$intersected = TypeCombinator::intersect($intersected, $combination[$i]);
}

if ($intersected instanceof NeverType) {
continue;
}

$result[] = $intersected;
}

return $result;
}

/**
* @param array<array<Type>> $arrays
* @return iterable<array<Type>>
*/
private static function combinations(array $arrays): iterable
{
if ($arrays === []) {
yield [];
return;
}

$last = array_pop($arrays);
foreach (self::combinations($arrays) as $combination) {
foreach ($last as $value) {
$next = $combination;
$next[] = $value;

yield $next;
}
}
}
}
Loading