psalm icon indicating copy to clipboard operation
psalm copied to clipboard

false positive InvalidPropertyAssignmentValue for templated arrays with named keys

Open GrandLTU opened this issue 6 years ago • 5 comments

https://psalm.dev/r/6325c38df8

GrandLTU avatar Apr 16 '20 20:04 GrandLTU

I found these snippets:

https://psalm.dev/r/6325c38df8
<?php declare(strict_types=1);

/** @template TValue */
trait A
{
    /** @psalm-var callable(): array{0: TValue} */
    private $foo;
    
    /**
     * @psalm-param callable(): array{0: TValue} $foo
     */
    public function __construct(callable $foo)
    {
        $this->foo = $foo;
    }
}

/** @template TValue of object */
class B
{
    /** @use A<TValue> */
	use A;
}
Psalm output (using commit 6f28ee6):

ERROR: InvalidPropertyAssignmentValue - 14:22 - $this->foo with declared type 'callable():array{0: object}' cannot be assigned type 'callable():array{0: TValue:A as mixed}'

psalm-github-bot[bot] avatar Apr 16 '20 20:04 psalm-github-bot[bot]

@GrandLTU , Not sure if I correct or not but I noticed you are using union types in incorrect manner. Check if it makes sense: https://psalm.dev/r/fbe1f7665b

SCIF avatar Apr 16 '20 23:04 SCIF

I found these snippets:

https://psalm.dev/r/fbe1f7665b
<?php declare(strict_types=1);

/** @template TValue
* @template ReturnT array{0: TValue}
*/
trait A
{
    /** @psalm-var Closure():ReturnT */
    private $foo;
    
    /**
     * @psalm-param Closure():ReturnT $foo
     */
    public function __construct(callable $foo)
    {
        $this->foo = $foo;
    }
}

/**
* @template TValue of object
* @template ReturnInt of array{0: int}
*/
class B
{
    /** @use A<TValue, ReturnInt> */
	use A;
}
Psalm output (using commit 6f28ee6):

No issues!

psalm-github-bot[bot] avatar Apr 16 '20 23:04 psalm-github-bot[bot]

@GrandLTU , Not sure if I correct or not but I noticed you are using union types in incorrect manner. Check if it makes sense: https://psalm.dev/r/fbe1f7665b

why is it incorrect?

GrandLTU avatar Apr 17 '20 07:04 GrandLTU

I found these snippets:

https://psalm.dev/r/fbe1f7665b
<?php declare(strict_types=1);

/** @template TValue
* @template ReturnT array{0: TValue}
*/
trait A
{
    /** @psalm-var Closure():ReturnT */
    private $foo;
    
    /**
     * @psalm-param Closure():ReturnT $foo
     */
    public function __construct(callable $foo)
    {
        $this->foo = $foo;
    }
}

/**
* @template TValue of object
* @template ReturnInt of array{0: int}
*/
class B
{
    /** @use A<TValue, ReturnInt> */
	use A;
}
Psalm output (using commit 5ef82b5):

No issues!

psalm-github-bot[bot] avatar Apr 17 '20 07:04 psalm-github-bot[bot]