Skip to content

Commit

Permalink
Fix #4958 - honour template as inheritance when comparing template types
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Jan 9, 2021
1 parent 6ef0f2e commit e299e91
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/Psalm/Internal/Type/Comparator/ObjectComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,24 @@ public static function isShallowlyContainedBy(
continue;
}

if (\substr($intersection_container_type->defining_class, 0, 3) === 'fn-') {
foreach ($intersection_input_types as $intersection_input_type) {
if ($intersection_input_type instanceof TTemplateParam
&& \substr($intersection_input_type->defining_class, 0, 3) === 'fn-'
foreach ($intersection_input_types as $intersection_input_type) {
if ($intersection_input_type instanceof TTemplateParam
&& (\substr($intersection_container_type->defining_class, 0, 3) === 'fn-'
|| \substr($intersection_input_type->defining_class, 0, 3) === 'fn-')
) {
if (\substr($intersection_input_type->defining_class, 0, 3) === 'fn-'
&& \substr($intersection_container_type->defining_class, 0, 3) === 'fn-'
&& $intersection_input_type->defining_class
!== $intersection_container_type->defining_class
) {
continue 2;
}

foreach ($intersection_input_type->as->getAtomicTypes() as $input_as_atomic) {
if ($input_as_atomic->equals($intersection_container_type)) {
continue 3;
}
}
}
}

Expand Down
23 changes: 23 additions & 0 deletions tests/Template/FunctionTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1485,6 +1485,29 @@ function takesNullableString(string|int $s) : string {
[],
'8.0'
],
'templateChildClass' => [
'<?php
/** @template T */
class Collection {
/**
* @param T $t
*/
private function add($t) : void {}
/**
* @template TChild as T
* @param TChild $default
*
* @return TChild
*/
public function get($default)
{
$this->add($default);
return $default;
}
}'
],
];
}

Expand Down

0 comments on commit e299e91

Please sign in to comment.