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-true cannot be used with imported types. #8263

Closed
rzvc opened this issue Jul 14, 2022 · 1 comment · Fixed by #8705
Closed

@psalm-assert-if-true cannot be used with imported types. #8263

rzvc opened this issue Jul 14, 2022 · 1 comment · Fixed by #8705

Comments

@rzvc
Copy link

rzvc commented Jul 14, 2022

https://psalm.dev/r/a9678dc68b

In the same file, it doesn't work if the imported type is also aliased to something else. Probably because the original type is available to the calling code anyway. When the class is used from a different file, it just doesn't work, even if the type is imported in the current scope and it has the correct name.

@psalm-github-bot
Copy link

I found these snippets:

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

/**
* @psalm-type SomeItemTypex = object{text: string}
*/
class TypesContainer
{
}

/**
* @psalm-import-type SomeItemTypex from TypesContainer as SomeItemType
*/
class ItemContainer
{
    /** @var ?SomeItemType */
    private $item = null;
    
    /**
     * @psalm-assert-if-true SomeItemType $this->get()
     * @psalm-mutation-free
     */
	public function has() : bool
	{
        return $this->item !== null;
	}

    /**
     * @psalm-mutation-free
     * @return ?SomeItemType
     */
	public function get()
	{
        return $this->item;
	}
}

$container = new ItemContainer();
print_r($container->get()->text);

if ($container->has())
    print_r($container->get()->text);
Psalm output (using commit 2a72a24):

ERROR: PossiblyNullPropertyFetch - 38:9 - Cannot get property on possibly null variable $container->get() of type null|object{text:string}

ERROR: UndefinedClass - 41:13 - Cannot get properties of undefined class SomeItemType

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

Successfully merging a pull request may close this issue.

1 participant