1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

Fix #1680 - narrow nullable templated matching

This commit is contained in:
Matthew Brown 2019-05-25 12:44:47 -04:00
parent 0bcf61dee7
commit 052599192a
2 changed files with 28 additions and 0 deletions

View File

@ -1009,10 +1009,16 @@ class Union
if ($input_type) {
$generic_param = clone $input_type;
if ($this->isNullable() && $generic_param->isNullable()) {
$generic_param->removeType('null');
}
$generic_param->setFromDocblock();
if (isset($generic_params[$key][$atomic_type->defining_class ?: ''][0])) {
$existing_depth = $generic_params[$key][$atomic_type->defining_class ?: ''][1] ?? -1;
if ($existing_depth > $depth) {
continue;
}

View File

@ -2572,6 +2572,28 @@ class TemplateTest extends TestCase
return get(B::class, $mapB, $id);
}'
],
'dontGeneraliseBoundParamWithWiderCallable' => [
'<?php
class C {
public function foo() : void {}
}
/**
* @psalm-template T
* @psalm-param T $t
* @psalm-param callable(?T):void $callable
* @return T
*/
function makeConcrete($t, callable $callable) {
$callable(rand(0, 1) ? $t : null);
return $t;
}
$c = makeConcrete(new C(), function (?C $c) : void {});',
[
'$c' => 'C',
]
],
];
}