mirror of
https://github.com/danog/psalm.git
synced 2024-12-02 09:37:59 +01:00
Fix #3929 - merge expanded types where necessary
This commit is contained in:
parent
e972319f8c
commit
38af5db8f0
@ -39,6 +39,8 @@ class TypeExpander
|
|||||||
|
|
||||||
$new_return_type_parts = [];
|
$new_return_type_parts = [];
|
||||||
|
|
||||||
|
$has_array_output = false;
|
||||||
|
|
||||||
foreach ($return_type->getAtomicTypes() as $return_type_part) {
|
foreach ($return_type->getAtomicTypes() as $return_type_part) {
|
||||||
$parts = self::expandAtomic(
|
$parts = self::expandAtomic(
|
||||||
$codebase,
|
$codebase,
|
||||||
@ -53,12 +55,20 @@ class TypeExpander
|
|||||||
|
|
||||||
if (is_array($parts)) {
|
if (is_array($parts)) {
|
||||||
$new_return_type_parts = array_merge($new_return_type_parts, $parts);
|
$new_return_type_parts = array_merge($new_return_type_parts, $parts);
|
||||||
|
$has_array_output = true;
|
||||||
} else {
|
} else {
|
||||||
$new_return_type_parts[] = $parts;
|
$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->from_docblock = $return_type->from_docblock;
|
||||||
$fleshed_out_type->ignore_nullable_issues = $return_type->ignore_nullable_issues;
|
$fleshed_out_type->ignore_nullable_issues = $return_type->ignore_nullable_issues;
|
||||||
|
@ -397,6 +397,42 @@ class TypeAnnotationTest extends TestCase
|
|||||||
}',
|
}',
|
||||||
'error_message' => 'InvalidDocblock',
|
'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'
|
||||||
|
],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user