Skip to content
Permalink
Browse files

Fix #2470 - only parameterise constructors according to inherited params

  • Loading branch information
muglug committed Dec 14, 2019
1 parent c7a3ba9 commit ce2e5b27c093e3faac22dd013ee41583b1939e13
@@ -526,9 +526,20 @@ private static function getGenericParamForOffset(
string $fq_class_name,
string $template_name,
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 (!$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];
}

@@ -544,7 +555,8 @@ private static function getGenericParamForOffset(
$fq_class_name,
$extended_template_name,
$template_type_extends,
$found_generic_params
$found_generic_params,
true
);
}
}
@@ -2531,6 +2531,70 @@ function baz($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>'
]
],
];
}

0 comments on commit ce2e5b2

Please sign in to comment.
You can’t perform that action at this time.