Navigation Menu

Skip to content

Commit

Permalink
Allow multiple method_exists checks to inform type
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Aug 19, 2019
1 parent 920c2d8 commit a3e9dec
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/Psalm/Internal/Type/AssertionReconciler.php
Expand Up @@ -800,7 +800,7 @@ private static function reconcileNonEmptyCountable(
*/ */
private static function reconcileHasMethod( private static function reconcileHasMethod(
Codebase $codebase, Codebase $codebase,
string $method_id, string $method_name,
Union $existing_var_type, Union $existing_var_type,
?string $key, ?string $key,
?CodeLocation $code_location, ?CodeLocation $code_location,
Expand All @@ -819,18 +819,25 @@ private static function reconcileHasMethod(
) { ) {
$object_types[] = $type; $object_types[] = $type;


if (!$codebase->methodExists($type->value . '::' . $method_id)) { if (!$codebase->methodExists($type->value . '::' . $method_name)) {
$obj = new Atomic\TObjectWithProperties( $obj = new Atomic\TObjectWithProperties(
[], [],
[$method_id => $type->value . '::' . $method_id] [$method_name => $type->value . '::' . $method_name]
); );
$type->extra_types[$obj->getKey()] = $obj; $type->extra_types[$obj->getKey()] = $obj;
$did_remove_type = true; $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) { } elseif ($type instanceof TObject || $type instanceof TMixed) {
$object_types[] = new Atomic\TObjectWithProperties( $object_types[] = new Atomic\TObjectWithProperties(
[], [],
[$method_id => 'object::' . $method_id] [$method_name => 'object::' . $method_name]
); );
$did_remove_type = true; $did_remove_type = true;
} elseif ($type instanceof TTemplateParam) { } elseif ($type instanceof TTemplateParam) {
Expand All @@ -847,7 +854,7 @@ private static function reconcileHasMethod(
$existing_var_type, $existing_var_type,
$old_var_type_string, $old_var_type_string,
$key, $key,
'object with method ' . $method_id, 'object with method ' . $method_name,
!$did_remove_type, !$did_remove_type,
$code_location, $code_location,
$suppressed_issues $suppressed_issues
Expand Down
13 changes: 13 additions & 0 deletions tests/MethodCallTest.php
Expand Up @@ -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();
}'
],
]; ];
} }


Expand Down

0 comments on commit a3e9dec

Please sign in to comment.