Skip to content
Permalink
Browse files

Allow is_subclass_of/is_a to inform shape of mixed arg

  • Loading branch information...
muglug committed May 6, 2019
1 parent ecb7a6c commit 57a585240c33cd1cbe4068a3f382c19b68a1a854
Showing with 36 additions and 4 deletions.
  1. +17 −4 src/Psalm/Type/Reconciler.php
  2. +19 −0 tests/ToStringTest.php
@@ -969,10 +969,6 @@ public static function reconcileTypes(
}
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 @@ public static function reconcileTypes(
$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 @@ function maybeShow(?string $message): void {
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',
],
];
}
}

0 comments on commit 57a5852

Please sign in to comment.
You can’t perform that action at this time.