Skip to content
Permalink
Browse files

Allow multiple method_exists checks to inform type

  • Loading branch information...
muglug committed Aug 19, 2019
1 parent 920c2d8 commit a3e9dec925d595b1701bc0669b7d3364749be0e4
Showing with 25 additions and 5 deletions.
  1. +12 −5 src/Psalm/Internal/Type/AssertionReconciler.php
  2. +13 −0 tests/MethodCallTest.php
@@ -800,7 +800,7 @@ private static function reconcileNonEmptyCountable(
*/
private static function reconcileHasMethod(
Codebase $codebase,
string $method_id,
string $method_name,
Union $existing_var_type,
?string $key,
?CodeLocation $code_location,
@@ -819,18 +819,25 @@ private static function reconcileHasMethod(
) {
$object_types[] = $type;
if (!$codebase->methodExists($type->value . '::' . $method_id)) {
if (!$codebase->methodExists($type->value . '::' . $method_name)) {
$obj = new Atomic\TObjectWithProperties(
[],
[$method_id => $type->value . '::' . $method_id]
[$method_name => $type->value . '::' . $method_name]
);
$type->extra_types[$obj->getKey()] = $obj;
$did_remove_type = true;
}
} elseif ($type instanceof Atomic\TObjectWithProperties) {
$object_types[] = $type;
if (!isset($type->methods[$method_name])) {
$type->methods[$method_name] = 'object::' . $method_name;
$did_remove_type = true;
}
} elseif ($type instanceof TObject || $type instanceof TMixed) {
$object_types[] = new Atomic\TObjectWithProperties(
[],
[$method_id => 'object::' . $method_id]
[$method_name => 'object::' . $method_name]
);
$did_remove_type = true;
} elseif ($type instanceof TTemplateParam) {
@@ -847,7 +854,7 @@ private static function reconcileHasMethod(
$existing_var_type,
$old_var_type_string,
$key,
'object with method ' . $method_id,
'object with method ' . $method_name,
!$did_remove_type,
$code_location,
$suppressed_issues
@@ -366,6 +366,19 @@ function foo(A $a) : void {
}
}'
],
'callManyMethodsAfterCheckingExistence' => [
'<?php
function foo(object $object) : void {
if (!method_exists($object, "foo")) {
return;
}
if (!method_exists($object, "bar")) {
return;
}
$object->foo();
$object->bar();
}'
],
];
}

0 comments on commit a3e9dec

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