Skip to content

Infer template from get_class checks #2580

@muglug

Description

@muglug

Code snippet that reproduces the problem

/**
 * @template T
 * @param class-string<T> $typeName
 * @param mixed $value
 * @return T
 */
function cast($value, string $typeName) {
  if (is_object($value) && get_class($value) === $typeName) {
    return $value;
  }

  throw new RuntimeException();
}

https://phpstan.org/r/78d89125-d555-48b2-82bf-1a1f2f7f31a2

Expected output

No issues

Actual output

Function cast() should return T but returns object

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions