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

Psalm doesn't accept generic invokable class as callable #4550

Closed
klimick opened this issue Nov 14, 2020 · 4 comments
Closed

Psalm doesn't accept generic invokable class as callable #4550

klimick opened this issue Nov 14, 2020 · 4 comments
Labels

Comments

@klimick
Copy link
Contributor

klimick commented Nov 14, 2020

Bug or type inference limitation?

https://psalm.dev/r/6df07c8d07

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/6df07c8d07
<?php

class OrderId
{
    public string $value;

	public function __construct(string $value)
    {
    	$this->value = $value;
    }
}

/**
 * @template A
 * @template B
 */
class _Callable
{
    /** @var callable(A): B */
    private $fn;

    /**
     * @param callable(A): B $fn
     */
    public function __construct(callable $fn)
    {
        $this->fn = $fn;
    }

    /**
     * @param A $a
     * @return B
     */
	public function __invoke($a)
    {
        return ($this->fn)($a);
    }

    /**
     * @return callable(A): B
     */
    public function unwrap(): callable
    {
    	return $this->fn;
    }
}

/**
 * @param callable(string): OrderId $_
 */
function testCallable(callable $_): void
{
}

testCallable(
	fn(string $value) => new OrderId($value)
);

testCallable((
    new _Callable(fn(string $value) => new OrderId($value))
)->unwrap());

testCallable(
	new _Callable(fn(string $value) => new OrderId($value))
);
Psalm output (using commit f65868c):

ERROR: InvalidArgument - 64:2 - Argument 1 of testCallable expects callable(string):OrderId, _Callable<string, OrderId> provided

@klimick klimick changed the title Psalm doesn't take generic invokable class as callable Psalm doesn't accept generic invokable class as callable Nov 14, 2020
@weirdan weirdan added the bug label Nov 14, 2020
@thomasvargiu
Copy link
Contributor

Even with functions: https://psalm.dev/r/a51288e0e0

@psalm-github-bot
Copy link

I found these snippets:

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

/**
 * @template B
 */
class A {
    /** @var B */
    private $value;
    /** @param B $value */
    public function __construct($value) {
        $this->value = $value;
    }
    /** @return B */
    public function __invoke()
    {
        return $this->value;
    }
}

$a = new A('foo');
/** @psalm-trace $v */
$v = Closure::fromCallable($a);

/** @psalm-trace $r */
$r = $v();
Psalm output (using commit fd53192):

INFO: Trace - 22:1 - $v: impure-Closure():B:A as mixed

INFO: Trace - 25:1 - $r: B:A as mixed

@klimick
Copy link
Contributor Author

klimick commented Jan 21, 2022

Fixed in #7417

@klimick klimick closed this as completed Jan 21, 2022
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