1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Fix removal of traversable from iterable

Fixes #2509
This commit is contained in:
Matthew Brown 2019-12-27 13:01:44 -05:00
parent 068afa09d3
commit 4a28525275
2 changed files with 19 additions and 3 deletions

View File

@ -238,13 +238,15 @@ class NegatedAssertionReconciler extends Reconciler
}
if (strtolower($assertion) === 'traversable'
&& isset($existing_var_type->getTypes()['iterable'])
&& isset($existing_var_atomic_types['iterable'])
) {
/** @var Type\Atomic\TIterable */
$iterable = $existing_var_atomic_types['iterable'];
$existing_var_type->removeType('iterable');
$existing_var_type->addType(new TArray(
[
new Type\Union([new TArrayKey]),
new Type\Union([new TMixed]),
$iterable->type_params[0],
$iterable->type_params[1],
]
));
} elseif (strtolower($assertion) === 'int'

View File

@ -2376,6 +2376,20 @@ class ConditionalTest extends \Psalm\Tests\TestCase
return $foo;
}'
],
'isNotTraversable' => [
'<?php
/**
* @psalm-param iterable<string> $collection
* @psalm-return array<string>
*/
function order(iterable $collection): array {
if ($collection instanceof \Traversable) {
$collection = iterator_to_array($collection, false);
}
return $collection;
}'
],
];
}