Skip to content

Commit

Permalink
Fix #2470 - only parameterise constructors according to inherited params
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Dec 14, 2019
1 parent c7a3ba9 commit ce2e5b2
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
Expand Up @@ -526,9 +526,20 @@ private static function getGenericParamForOffset(
string $fq_class_name, string $fq_class_name,
string $template_name, string $template_name,
array $template_type_extends, array $template_type_extends,
array $found_generic_params array $found_generic_params,
bool $mapped = false
) { ) {
if (isset($found_generic_params[$template_name][$fq_class_name])) { if (isset($found_generic_params[$template_name][$fq_class_name])) {
if (!$mapped && isset($template_type_extends[$fq_class_name][$template_name])) {
foreach ($template_type_extends[$fq_class_name][$template_name]->getTypes() as $t) {
if ($t instanceof Type\Atomic\TTemplateParam) {
if ($t->param_name !== $template_name) {
return $t->as;
}
}
}
}

return $found_generic_params[$template_name][$fq_class_name][0]; return $found_generic_params[$template_name][$fq_class_name][0];
} }


Expand All @@ -544,7 +555,8 @@ private static function getGenericParamForOffset(
$fq_class_name, $fq_class_name,
$extended_template_name, $extended_template_name,
$template_type_extends, $template_type_extends,
$found_generic_params $found_generic_params,
true
); );
} }
} }
Expand Down
64 changes: 64 additions & 0 deletions tests/Template/ClassTemplateExtendsTest.php
Expand Up @@ -2531,6 +2531,70 @@ function baz($t) {
return new BarOfFoo($t); return new BarOfFoo($t);
}' }'
], ],
'inheritTemplateParamViaConstructorSameName' => [
'<?php
class Dog {}
/**
* @template T
*/
class Collection {
/** @var array<T> */
protected $arr = [];
/**
* @param array<T> $arr
*/
public function __construct(array $arr) {
$this->arr = $arr;
}
}
/**
* @template T
* @template V
* @extends Collection<V>
*/
class CollectionChild extends Collection {
}
$dogs = new CollectionChild([new Dog(), new Dog()]);',
[
'$dogs' => 'CollectionChild<mixed, Dog>'
]
],
'inheritTemplateParamViaConstructorDifferentName' => [
'<?php
class Dog {}
/**
* @template T
*/
class Collection {
/** @var array<T> */
protected $arr = [];
/**
* @param array<T> $arr
*/
public function __construct(array $arr) {
$this->arr = $arr;
}
}
/**
* @template U
* @template V
* @extends Collection<V>
*/
class CollectionChild extends Collection {
}
$dogs = new CollectionChild([new Dog(), new Dog()]);',
[
'$dogs' => 'CollectionChild<mixed, Dog>'
]
],
]; ];
} }


Expand Down

0 comments on commit ce2e5b2

Please sign in to comment.