Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expects T:fn-a as object, object provided #5279

Closed
simPod opened this issue Feb 25, 2021 · 3 comments
Closed

Expects T:fn-a as object, object provided #5279

simPod opened this issue Feb 25, 2021 · 3 comments
Labels

Comments

@simPod
Copy link
Contributor

simPod commented Feb 25, 2021

https://psalm.dev/r/b70f1af09b

callback expects an object and I passed an object. It is not very clear what is the issue in the code / psalm bug?

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/b70f1af09b
<?php

interface Serializer
{
    /**
     * @param mixed $data
     */
    public function serialize($data): string;

    /**
     * @return mixed
     *
     * @psalm-template T
     * @psalm-param class-string<T> $type
     * @psalm-return T
     */
    public function deserialize(string $data, string $type);
}

/**
 * @template T of object
 * @psalm-param    T $serializable
 * @psalm-param    callable(T, T): void $cmp
 */
function a(Serializer $serializer, $serializable, callable $cmp) : void
{
    $serialized = $serializer->serialize($serializable);

    $deserialized = $serializer->deserialize($serialized, get_class($serializable));

    $cmp($serializable, $deserialized);
}
Psalm output (using commit ed6fdaa):

ERROR: InvalidArgument - 31:25 - Argument 2 expects T:fn-a as object, object provided

@weirdan weirdan added the bug label Feb 25, 2021
@vimeo vimeo deleted a comment from psalm-github-bot bot Feb 25, 2021
@vimeo vimeo deleted a comment from weirdan Feb 25, 2021
@muglug
Copy link
Collaborator

muglug commented Feb 25, 2021

Simplified: https://psalm.dev/r/93c8173378

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/93c8173378
<?php

/**
 * @template T of object
 * @psalm-param T $obj
 * @return T
 */
function a(string $str, object $obj) {
    $class = get_class($obj);
    return deserialize_object($str, $class);
}

/**
 * @psalm-template T
 * @psalm-param class-string<T> $type
 * @psalm-return T
 * @psalm-suppress InvalidReturnType
 */
function deserialize_object(string $data, string $type) {}
Psalm output (using commit 924f6b6):

ERROR: InvalidReturnStatement - 10:12 - The inferred type 'object' does not match the declared return type 'T:fn-a as object' for a

ERROR: InvalidReturnType - 6:12 - The declared return type 'T:fn-a as object' for a is incorrect, got 'object'

INFO: UnusedParam - 19:36 - Param $data is never referenced in this method

INFO: UnusedParam - 19:50 - Param $type is never referenced in this method

@muglug muglug closed this as completed in d484199 Feb 25, 2021
This was referenced Mar 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants