Skip to content

Commit

Permalink
Merge pull request #6946 from sebkehr/fix_cannot_extend_constrained_w…
Browse files Browse the repository at this point in the history
…ith_imported_alias
  • Loading branch information
weirdan committed Nov 19, 2021
2 parents 6097e02 + 511ed99 commit cd48940
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
38 changes: 36 additions & 2 deletions src/Psalm/Internal/Type/Comparator/UnionTypeComparator.php
Expand Up @@ -13,6 +13,7 @@
use Psalm\Type\Atomic\TNull;
use Psalm\Type\Atomic\TNumeric;
use Psalm\Type\Atomic\TTemplateParam;
use Psalm\Type\Atomic\TTypeAlias;

use function array_merge;

Expand Down Expand Up @@ -59,7 +60,7 @@ public static function isContainedBy(

$container_has_template = $container_type->hasTemplateOrStatic();

$input_atomic_types = \array_reverse($input_type->getAtomicTypes());
$input_atomic_types = \array_reverse(self::getTypeParts($codebase, $input_type));

while ($input_type_part = \array_pop($input_atomic_types)) {
if ($input_type_part instanceof TNull && $ignore_null) {
Expand Down Expand Up @@ -134,7 +135,7 @@ public static function isContainedBy(
}
}

foreach ($container_type->getAtomicTypes() as $container_type_part) {
foreach (self::getTypeParts($codebase, $container_type) as $container_type_part) {
if ($ignore_null
&& $container_type_part instanceof TNull
&& !$input_type_part instanceof TNull
Expand Down Expand Up @@ -469,4 +470,37 @@ public static function canExpressionTypesBeIdentical(

return false;
}

/**
* @return list<Type\Atomic>
*/
private static function getTypeParts(
Codebase $codebase,
Type\Union $union_type
): array {
$atomic_types = [];
foreach ($union_type->getAtomicTypes() as $atomic_type) {
if (!$atomic_type instanceof TTypeAlias) {
\array_push($atomic_types, $atomic_type);
continue;
}
$expanded = TypeExpander::expandAtomic(
$codebase,
$atomic_type,
$atomic_type->declaring_fq_classlike_name,
$atomic_type->declaring_fq_classlike_name,
null,
true,
true
);
if ($expanded instanceof Type\Atomic) {
\array_push($atomic_types, $expanded);
continue;
}

\array_push($atomic_types, ...$expanded);
}

return $atomic_types;
}
}
19 changes: 19 additions & 0 deletions tests/TypeAnnotationTest.php
Expand Up @@ -426,6 +426,25 @@ class B {}
*/
class C implements A {}',
],
'importedTypeAliasAsConstrainedTypeParameterForImplementation' => [
'<?php
namespace Bar;
/** @template T of string */
interface A {}
/**
* @psalm-type Foo = "foo"
*/
class B {}
/**
* @psalm-import-type Foo from B
* @implements A<Foo>
*/
class C implements A {}
'
],
'importedTypeAliasAsTypeParameterForExtendedClass' => [
'<?php
namespace Bar;
Expand Down

0 comments on commit cd48940

Please sign in to comment.