Skip to content

Commit

Permalink
Fix new template class array key coercion to mixed - when we don’t kn…
Browse files Browse the repository at this point in the history
…ow, assume bottom type
  • Loading branch information
muglug committed Apr 26, 2019
1 parent 82e0bca commit 59e53ac
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
Expand Up @@ -364,7 +364,7 @@ public static function analyze(
? explode('::', $declaring_method_id)[0] ? explode('::', $declaring_method_id)[0]
: $fq_class_name; : $fq_class_name;


foreach ($storage->template_types as $template_name => $_) { foreach ($storage->template_types as $template_name => $base_type) {
if (isset($found_generic_params[$template_name][$fq_class_name])) { if (isset($found_generic_params[$template_name][$fq_class_name])) {
$generic_param_types[] = $found_generic_params[$template_name][$fq_class_name][0]; $generic_param_types[] = $found_generic_params[$template_name][$fq_class_name][0];
} elseif ($storage->template_type_extends && $found_generic_params) { } elseif ($storage->template_type_extends && $found_generic_params) {
Expand All @@ -375,7 +375,7 @@ public static function analyze(
$found_generic_params $found_generic_params
); );
} else { } else {
$generic_param_types[] = Type::getMixed(); $generic_param_types[] = array_values($base_type)[0][0];
} }
} }
} }
Expand Down
28 changes: 28 additions & 0 deletions tests/Template/TemplateTest.php
Expand Up @@ -2070,6 +2070,34 @@ public function foo($t): void {
} }
}' }'
], ],
'allowTemplateParamsToCoerceToMinimumTypes' => [
'<?php
/**
* @psalm-template TKey of array-key
* @psalm-template T
*/
class ArrayCollection
{
/**
* @var array<TKey,T>
*/
private $elements;
/**
* @param array<TKey,T> $elements
*/
public function __construct(array $elements = [])
{
$this->elements = $elements;
}
}
/** @psalm-suppress MixedArgument */
$c = new ArrayCollection($_GET["a"]);',
[
'$c' => 'ArrayCollection<array-key, mixed>',
],
],
]; ];
} }


Expand Down

0 comments on commit 59e53ac

Please sign in to comment.