Skip to content

Commit

Permalink
Only perform assertions where the property type is known
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Apr 12, 2020
1 parent 110df3e commit c733d6d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -492,12 +492,14 @@ public static function analyze(

$class_storage_for_method = $codebase->methods->getClassLikeStorageForMethod($method_id);
$plain_getter_property = null;

if ((isset($class_storage_for_method->methods[$method_name_lc]))
&& !$class_storage_for_method->methods[$method_name_lc]->overridden_somewhere
&& !$class_storage_for_method->methods[$method_name_lc]->overridden_downstream
&& ($plain_getter_property = $class_storage_for_method->methods[$method_name_lc]->plain_getter)
&& isset($context->vars_in_scope[$getter_var_id = $lhs_var_id . '->' . $plain_getter_property])) {
$return_type_candidate = $context->vars_in_scope[$getter_var_id];
&& isset($context->vars_in_scope[$getter_var_id = $lhs_var_id . '->' . $plain_getter_property])
) {
$return_type_candidate = clone $context->vars_in_scope[$getter_var_id];
} else {
$return_type_candidate = MethodCallReturnTypeFetcher::fetch(
$statements_analyzer,
Expand Down
21 changes: 16 additions & 5 deletions src/Psalm/Internal/PhpVisitor/ReflectorVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -1864,11 +1864,22 @@ private function registerFunctionLike(PhpParser\Node\FunctionLike $stmt, $fake_m
$storage->mutation_free_inferred = true;

if ($stmt->stmts[0]->expr->name instanceof PhpParser\Node\Identifier) {
$storage->plain_getter = $stmt->stmts[0]->expr->name->name;
$storage->if_true_assertions[] = new \Psalm\Storage\Assertion(
'$this->' . $storage->plain_getter,
[['!falsy']]
);
$property_name = $stmt->stmts[0]->expr->name->name;

if (isset($class_storage->properties[$property_name])
&& $class_storage->properties[$property_name]->type
&& ($class_storage->properties[$property_name]->type->isNullable()
|| $class_storage->properties[$property_name]->type->isFalsable()
|| $class_storage->properties[$property_name]->type->hasArray()
)
) {
$storage->plain_getter = $property_name;

$storage->if_true_assertions[] = new \Psalm\Storage\Assertion(
'$this->' . $property_name,
[['!falsy']]
);
}
}
} elseif (strpos($stmt->name->name, 'assert') === 0) {
$var_assertions = [];
Expand Down

0 comments on commit c733d6d

Please sign in to comment.