Skip to content
Permalink
Browse files

Preserve type params when negating array on iterable type

Fixes #2005
  • Loading branch information...
muglug committed Aug 12, 2019
1 parent 2ed2876 commit 62451762fa480364c831b68f27934fa9c4cb2866
Showing with 15 additions and 1 deletion.
  1. +13 −1 src/Psalm/Internal/Type/NegatedAssertionReconciler.php
  2. +2 −0 tests/TypeReconciliationTest.php
@@ -223,8 +223,20 @@ public static function reconcile(
} 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'])
) {
@@ -136,6 +136,8 @@ public function providerTestReconcilation()
'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>'],
];
}

0 comments on commit 6245176

Please sign in to comment.
You can’t perform that action at this time.