1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Fix #1055 - improve reconciliation of class-strings

This commit is contained in:
Brown 2018-11-02 17:38:20 -04:00
parent 07934b8333
commit 3fc67782a9
4 changed files with 20 additions and 12 deletions

View File

@ -134,13 +134,10 @@ class TNamedObject extends Atomic
return;
}
$keys_to_unset = [];
$new_types = [];
foreach ($this->extra_types as $i => $extra_type) {
if ($extra_type instanceof TGenericParam && isset($template_types[$extra_type->param_name])) {
$keys_to_unset[] = $i;
$template_type = clone $template_types[$extra_type->param_name];
foreach ($template_type->getTypes() as $template_type_part) {

View File

@ -404,11 +404,17 @@ class Reconciler
}
} else {
$did_remove_type = true;
if ($existing_var_type->hasType('class-string')) {
$existing_var_type->removeType('class-string');
}
if ($existing_var_type->hasType('string')) {
$existing_var_type->removeType('string');
$existing_var_type->addType(new Type\Atomic\TLiteralString(''));
$existing_var_type->addType(new Type\Atomic\TLiteralString('0'));
}
}
}
if ($existing_var_type->hasInt()) {
$existing_int_types = $existing_var_type->getLiteralInts();

View File

@ -280,17 +280,20 @@ class ClassStringTest extends TestCase
],
'reconcileToFalsy' => [
'<?php
/** @psalm-param ?class-string $s */
function bar(?string $s) : void {}
class A {}
/** @psalm-return ?class-string */
function foo() : ?string {
function bat() {
if (rand(0, 1)) return null;
return A::class;
}
$a = foo();
$a ? 1 : 0;',
$a = bat();
$a ? 1 : 0;
bar($a);',
],
];
}

View File

@ -222,6 +222,8 @@ class TypeReconciliationTest extends TestCase
'2dArray' => ['array<mixed, array<mixed, string>>', 'array', 'array<array<string>>|null'],
'numeric' => ['string', 'numeric', 'string'],
'nullableClassString' => ['null', 'falsy', '?class-string'],
];
}