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: 2
path: src/Type/UnionType.php

-
Expand Down
6 changes: 3 additions & 3 deletions src/Type/IterableType.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ public function isSubTypeOf(Type $otherType): IsSuperTypeOfResult
if ($otherType instanceof IntersectionType || $otherType instanceof UnionType) {
return $otherType->isSuperTypeOf(new UnionType([
new ArrayType($this->keyType, $this->itemType),
new IntersectionType([
new ObjectType(Traversable::class),
$this,
new GenericObjectType(Traversable::class, [
$this->keyType,
$this->itemType,
]),
]));
}
Expand Down
12 changes: 12 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 @@ -1073,6 +1075,16 @@ public function toCoercedArgumentType(bool $strictTypes): Type

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

$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