mirror of
https://github.com/danog/psalm.git
synced 2025-01-22 05:41:20 +01:00
Allow is_subclass_of/is_a to inform shape of mixed arg
This commit is contained in:
parent
ecb7a6c069
commit
57a585240c
@ -969,10 +969,6 @@ class Reconciler
|
||||
}
|
||||
|
||||
if (substr($new_var_type, 0, 4) === 'isa-') {
|
||||
if ($existing_var_type->hasMixed()) {
|
||||
return Type::getMixed();
|
||||
}
|
||||
|
||||
$new_var_type = substr($new_var_type, 4);
|
||||
|
||||
$allow_string_comparison = false;
|
||||
@ -982,6 +978,23 @@ class Reconciler
|
||||
$allow_string_comparison = true;
|
||||
}
|
||||
|
||||
if ($existing_var_type->hasMixed()) {
|
||||
$type = new Type\Union([
|
||||
new Type\Atomic\TNamedObject($new_var_type)
|
||||
]);
|
||||
|
||||
if ($allow_string_comparison) {
|
||||
$type->addType(
|
||||
new Type\Atomic\TClassString(
|
||||
$new_var_type,
|
||||
new Type\Atomic\TNamedObject($new_var_type)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return $type;
|
||||
}
|
||||
|
||||
$existing_has_object = $existing_var_type->hasObjectType();
|
||||
$existing_has_string = $existing_var_type->hasString();
|
||||
|
||||
|
@ -219,6 +219,25 @@ class ToStringTest extends TestCase
|
||||
maybeShow(new ClassWithToString());',
|
||||
'error_message' => 'ImplicitToStringCast',
|
||||
],
|
||||
'possiblyInvalidCastOnIsSubclassOf' => [
|
||||
'<?php
|
||||
class Foo {}
|
||||
|
||||
/**
|
||||
* @param mixed $a
|
||||
*/
|
||||
function bar($a) : ?string {
|
||||
/**
|
||||
* @psalm-suppress MixedArgument
|
||||
*/
|
||||
if (is_subclass_of($a, Foo::class)) {
|
||||
return "hello" . $a;
|
||||
}
|
||||
|
||||
return null;
|
||||
}',
|
||||
'error_message' => 'PossiblyInvalidOperand',
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user