Skip to content

InvalidDocblock when passing object to a function with @psalm-assert-if-true #11386

@mgiuffrida

Description

@mgiuffrida

Simplified repro: https://psalm.dev/r/d55f65b8e2

/** @psalm-trace $obj */
$obj = (object)['hasFoo' => true];  // Correctly traced as an object

/**
 @param object{hasFoo: bool} $obj
 @psalm-assert-if-true true $obj->hasFoo
 @return bool
 */
function validateFoo(object $obj): bool {
    return $obj->hasFoo;
}

validateFoo($obj);  // "Variable obj is not an object so the assertion cannot be applied"

My actual use case is to array_filter() an array of objects, returning the objects with two specific fields set, but I was able to reduce the Psalm problem down to the above. (The actual PHP runs as expected.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions