mirror of
https://github.com/danog/psalm.git
synced 2024-11-26 20:34:47 +01:00
Fix #1055 - improve reconciliation of class-strings
This commit is contained in:
parent
07934b8333
commit
3fc67782a9
@ -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) {
|
||||
|
@ -404,9 +404,15 @@ class Reconciler
|
||||
}
|
||||
} else {
|
||||
$did_remove_type = true;
|
||||
$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->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'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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 {
|
||||
if (rand(0, 1)) return null;
|
||||
return A::class;
|
||||
function bat() {
|
||||
if (rand(0, 1)) return null;
|
||||
return A::class;
|
||||
}
|
||||
|
||||
$a = foo();
|
||||
|
||||
$a ? 1 : 0;',
|
||||
$a = bat();
|
||||
$a ? 1 : 0;
|
||||
bar($a);',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
@ -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'],
|
||||
];
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user