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

Incorrect type definition in call chain using question mark #11141

Open
mj4444ru opened this issue Oct 24, 2024 · 1 comment
Open

Incorrect type definition in call chain using question mark #11141

mj4444ru opened this issue Oct 24, 2024 · 1 comment

Comments

@mj4444ru
Copy link

https://psalm.dev/r/25589661a1

The result for $r1, $r2, $r3 should be the same, but it is different.

Copy link

I found these snippets:

https://psalm.dev/r/25589661a1
<?php

class A
{
    function getB(): B|null
    {
        return isset($GLOBALS['X']) ? new B() : null;
    }
}

class B
{
    function getC(): C
    {
        return new C();
    }
}

class C
{
    function get(): int
    {
        return 0;
    }
}

$a = new A();
$c = $a->getB()?->getC();
$r1 = $c?->get();
$r2 = $a->getB()?->getC()->get();
$r3 = $a->getB()?->getC()?->get();

/** @psalm-trace $c, $r1, $r2, $r3 */
Psalm output (using commit 03ee02c):

INFO: Trace - 33:38 - $c: C|null

INFO: Trace - 33:38 - $r1: int|null

INFO: Trace - 33:38 - $r2: int

INFO: Trace - 33:38 - $r3: int|null

INFO: UnusedVariable - 29:1 - $r1 is never referenced or the value is not used

INFO: UnusedVariable - 30:1 - $r2 is never referenced or the value is not used

INFO: UnusedVariable - 31:1 - $r3 is never referenced or the value is not used

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