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-assert-if-false annotation does not work as expected #11094

Open
AlexMinaev19 opened this issue Sep 10, 2024 · 1 comment
Open

psalm-assert-if-false annotation does not work as expected #11094

AlexMinaev19 opened this issue Sep 10, 2024 · 1 comment

Comments

@AlexMinaev19
Copy link

psalm-assert-if-false does not work as expected.

The annotation's location (on interface or class directly) does not impact the result.

https://psalm.dev/r/c8e0097627

Copy link

I found these snippets:

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

interface ContainerInterface
{
    /**
     * @return list<int>
     */
    public function getAll(): array;
    
    /**
     * @psalm-assert-if-false non-empty-list<int> $this->getAll()
     */
    public function isEmpty(): bool;
}

class Container implements ContainerInterface
{
    /** @var list<int> */
    private array $storage;
    
    /**
     * @param list<int> $storage
     */
    public function __construct(array $storage)
    {
        $this->storage = $storage;
    }

    public function getAll(): array
    {
        return $this->storage;
    }

    public function isEmpty(): bool
    {
        return 0 === \count($this->storage);
    }
}

/**
 * @param non-empty-list<int> $array
 */
function foo(array $array): int
{
    return count($array);
}

$container = new Container([]);

if (false === $container->isEmpty()) {
    foo($container->getAll());
}
Psalm output (using commit 16b24bd):

ERROR: InvalidArgument - 51:9 - Argument 1 of foo expects non-empty-list<int>, but list<int> provided

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant