1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 12:55:26 +01:00

Fix #4812 - reconcile array assertion on template as type correctly

This commit is contained in:
Matt Brown 2020-12-08 14:17:37 -05:00
parent 1e0bcc1876
commit be3f30ff1d
2 changed files with 34 additions and 1 deletions

View File

@ -1642,7 +1642,7 @@ class SimpleAssertionReconciler extends \Psalm\Type\Reconciler
$existing_var_atomic_types = $existing_var_type->getAtomicTypes();
if ($existing_var_type->hasMixed() || $existing_var_type->hasTemplate()) {
if ($existing_var_type->hasMixed()) {
return Type::getArray();
}
@ -1666,6 +1666,24 @@ class SimpleAssertionReconciler extends \Psalm\Type\Reconciler
$array_types[] = new TArray($clone_type->type_params);
$did_remove_type = true;
} elseif ($type instanceof TTemplateParam) {
if ($type->as->hasArray() || $type->as->hasMixed()) {
$type = clone $type;
$type->as = self::reconcileArray(
$type->as,
null,
false,
null,
$suppressed_issues,
$failed_reconciliation,
$is_equality
);
$array_types[] = $type;
}
$did_remove_type = true;
} else {
$did_remove_type = true;

View File

@ -653,6 +653,21 @@ class ConditionalReturnTypeTest extends TestCase
'$expect_mixed_from_literal' => 'mixed',
]
],
'isArryCheckOnTemplate' => [
'<?php
/**
* @template TResult as string|list<string>
* @param TResult $result
* @return (TResult is array ? list<string> : string)
*/
function recursion($result) {
if (\is_array($result)) {
return $result;
}
return strtoupper($result);
}'
],
];
}
}