Skip to content

Commit

Permalink
Fix #3929 - merge expanded types where necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Aug 16, 2020
1 parent e972319 commit 38af5db
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/Psalm/Internal/Type/TypeExpander.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public static function expandUnion(

$new_return_type_parts = [];

$has_array_output = false;

foreach ($return_type->getAtomicTypes() as $return_type_part) {
$parts = self::expandAtomic(
$codebase,
Expand All @@ -53,12 +55,20 @@ public static function expandUnion(

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;
Expand Down
36 changes: 36 additions & 0 deletions tests/TypeAnnotationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,42 @@ class A {
}',
'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'
],
];
}
}

0 comments on commit 38af5db

Please sign in to comment.