1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

Preserve type params when negating array on iterable type

Fixes #2005
This commit is contained in:
Matthew Brown 2019-08-12 00:44:26 -04:00
parent 2ed28764d6
commit 62451762fa
2 changed files with 15 additions and 1 deletions

View File

@ -223,8 +223,20 @@ class NegatedAssertionReconciler extends Reconciler
} elseif (strtolower($assertion) === 'array'
&& isset($existing_var_type->getTypes()['iterable'])
) {
$iterable = $existing_var_type->getTypes()['iterable'];
$existing_var_type->removeType('iterable');
$existing_var_type->addType(new TNamedObject('Traversable'));
if ($iterable instanceof Atomic\TIterable
&& $iterable->type_params
&& (!$iterable->type_params[0]->isMixed() || !$iterable->type_params[1]->isMixed())
) {
$traversable = new Atomic\TGenericObject('Traversable', $iterable->type_params);
} else {
$traversable = new TNamedObject('Traversable');
}
$existing_var_type->addType($traversable);
} elseif (strtolower($assertion) === 'string'
&& isset($existing_var_type->getTypes()['array-key'])
) {

View File

@ -136,6 +136,8 @@ class TypeReconciliationTest extends TestCase
'callableToCallableArray' => ['callable-array{0: string|object, 1: string}', 'array', 'callable'],
'callableOrArrayToCallableArray' => ['array<array-key, mixed>|callable-array{0: string|object, 1: string}', 'array', 'callable|array'],
'traversableToIntersection' => ['Countable&Traversable', 'Traversable', 'Countable'],
'iterableWithoutParamsToTraversableWithoutParams' => ['Traversable', '!array', 'iterable'],
'iterableWithParamsToTraversableWithParams' => ['Traversable<int, string>', '!array', 'iterable<int, string>'],
];
}