1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Fix #3929 - merge expanded types where necessary

This commit is contained in:
Matthew Brown 2020-08-16 16:12:29 -04:00
parent e972319f8c
commit 38af5db8f0
2 changed files with 47 additions and 1 deletions

View File

@ -39,6 +39,8 @@ class TypeExpander
$new_return_type_parts = [];
$has_array_output = false;
foreach ($return_type->getAtomicTypes() as $return_type_part) {
$parts = self::expandAtomic(
$codebase,
@ -53,12 +55,20 @@ class TypeExpander
if (is_array($parts)) {
$new_return_type_parts = array_merge($new_return_type_parts, $parts);
$has_array_output = true;
} else {
$new_return_type_parts[] = $parts;
}
}
$fleshed_out_type = new Type\Union($new_return_type_parts);
if ($has_array_output) {
$fleshed_out_type = TypeCombination::combineTypes(
$new_return_type_parts,
$codebase
);
} else {
$fleshed_out_type = new Type\Union($new_return_type_parts);
}
$fleshed_out_type->from_docblock = $return_type->from_docblock;
$fleshed_out_type->ignore_nullable_issues = $return_type->ignore_nullable_issues;

View File

@ -397,6 +397,42 @@ class TypeAnnotationTest extends TestCase
}',
'error_message' => 'InvalidDocblock',
],
'mergeImportedTypes' => [
'<?php
namespace A\B;
/**
* @psalm-type _A=array{
* id:int
* }
*
* @psalm-type _B=array{
* id:int,
* something:int
* }
*/
class Types
{
}
namespace A;
/**
* @psalm-import-type _A from \A\B\Types as _AA
* @psalm-import-type _B from \A\B\Types as _BB
*/
class Id
{
/**
* @psalm-param _AA|_BB $_item
*/
public function ff(array $_item): int
{
return $_item["something"];
}
}',
'error_message' => 'PossiblyUndefinedArrayOffset'
],
];
}
}