Skip to content
Closed
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
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1776,7 +1776,7 @@ parameters:
-
rawMessage: 'Doing instanceof PHPStan\Type\IterableType is error-prone and deprecated. Use Type::isIterable() instead.'
identifier: phpstanApi.instanceofType
count: 1
count: 3
path: src/Type/UnionType.php

-
Expand Down
22 changes: 22 additions & 0 deletions src/Type/UnionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use PHPStan\TrinaryLogic;
use PHPStan\Type\Enum\EnumCaseObjectType;
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\Generic\TemplateIterableType;
use PHPStan\Type\Generic\TemplateMixedType;
use PHPStan\Type\Generic\TemplateType;
Expand All @@ -33,6 +34,7 @@
use PHPStan\Type\Generic\TemplateUnionType;
use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
use Throwable;
use Traversable;
use function array_diff_assoc;
use function array_fill_keys;
use function array_map;
Expand Down Expand Up @@ -194,6 +196,16 @@ public function getConstantStrings(): array

public function accepts(Type $type, bool $strictTypes): AcceptsResult
{
if ($type instanceof IterableType && !$type instanceof TemplateIterableType) {
return $this->accepts(new UnionType([
new ArrayType($type->getIterableKeyType(), $type->getIterableValueType()),
new GenericObjectType(Traversable::class, [
$type->getIterableKeyType(),
$type->getIterableValueType(),
]),
]), $strictTypes);
}

foreach (self::EQUAL_UNION_CLASSES as $baseClass => $classes) {
if (!$type->equals(new ObjectType($baseClass))) {
continue;
Expand Down Expand Up @@ -1073,6 +1085,16 @@ public function toCoercedArgumentType(bool $strictTypes): Type

public function inferTemplateTypes(Type $receivedType): TemplateTypeMap
{
if ($receivedType instanceof IterableType && !$receivedType instanceof TemplateIterableType) {
$receivedType = new UnionType([
new ArrayType($receivedType->getIterableKeyType(), $receivedType->getIterableValueType()),
new GenericObjectType(Traversable::class, [
$receivedType->getIterableKeyType(),
$receivedType->getIterableValueType(),
]),
]);
}

$types = TemplateTypeMap::createEmpty();
if ($receivedType instanceof UnionType) {
$myTypes = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2735,4 +2735,9 @@ public function testBug10559(): void
$this->analyse([__DIR__ . '/data/bug-10559.php'], []);
}

public function testBug13247(): void
{
$this->analyse([__DIR__ . '/data/bug-13247.php'], []);
}

}
35 changes: 35 additions & 0 deletions tests/PHPStan/Rules/Functions/data/bug-13247.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php // lint >= 8.0

declare(strict_types = 1);

namespace Bug13247;

use Traversable;

/**
* @template K as array-key
* @template V
*
* @param array<K, V>|Traversable<K, V> $input
*
* @return array<K, V>
*/
function as_array(array|Traversable $input): array {
if ($input instanceof Traversable) {
return iterator_to_array($input);
}

return $input;
}

/**
* @template K as array-key
* @template V
*
* @param iterable<K, V> $input
*
* @return array<K, V>
*/
function iter_as_array(iterable $input): array {
return as_array($input);
}
Loading