Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

always combine the result of expansions #9562

Merged
merged 1 commit into from Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 4 additions & 14 deletions src/Psalm/Internal/Type/TypeExpander.php
Expand Up @@ -77,8 +77,6 @@ public static function expandUnion(
): Union {
$new_return_type_parts = [];

$had_split_values = false;

foreach ($return_type->getAtomicTypes() as $return_type_part) {
$parts = self::expandAtomic(
$codebase,
Expand All @@ -94,21 +92,13 @@ public static function expandUnion(
$throw_on_unresolvable_constant,
);

if ($return_type_part instanceof TTypeAlias || count($parts) > 1) {
$had_split_values = true;
}

$new_return_type_parts = [...$new_return_type_parts, ...$parts];
}

if ($had_split_values) {
$fleshed_out_type = TypeCombiner::combine(
$new_return_type_parts,
$codebase,
);
} else {
$fleshed_out_type = new Union($new_return_type_parts);
}
$fleshed_out_type = TypeCombiner::combine(
$new_return_type_parts,
$codebase,
);

$fleshed_out_type->from_docblock = $return_type->from_docblock;
$fleshed_out_type->ignore_nullable_issues = $return_type->ignore_nullable_issues;
Expand Down
32 changes: 32 additions & 0 deletions tests/TypeAnnotationTest.php
Expand Up @@ -771,6 +771,38 @@ class Foo {
'$output===' => 'array{phone: string}',
],
],
'combineAliasOfArrayAndArrayCorrectly' => [
'code' => '<?php

/**
* @psalm-type C = array{c: string}
*/
class A {}

/**
* @template F
*/
class D {
/**
* @param F $data
*/
public function __construct(public $data) {}
}


/**
* @psalm-import-type C from A
*/
class G {

/**
* @param D<array{b: bool}>|D<C> $_doesNotWork
*/
public function doesNotWork($_doesNotWork): void {
/** @psalm-check-type-exact $_doesNotWork = D<array{b?: bool, c?: string}> */;
}
}',
],
];
}

Expand Down