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

Bug when using "@template" #10901

Open
mj4444ru opened this issue Apr 11, 2024 · 2 comments
Open

Bug when using "@template" #10901

mj4444ru opened this issue Apr 11, 2024 · 2 comments

Comments

@mj4444ru
Copy link

In the first and second examples everything is fine.
In the third, something goes wrong.
Using template results in unexpected NullableReturnStatement, InvalidNullableReturnType errors.

https://psalm.dev/r/23d995c8fa
https://psalm.dev/r/449c3a0338
https://psalm.dev/r/a80b2963c6

Copy link

I found these snippets:

https://psalm.dev/r/23d995c8fa
<?php

interface TestInterface
{
    public function test(): void;
}

function f1(?string $p): string
{
    if ($p === null) {
        throw new Exception('Error');
    }
    
    return $p;
}

function f2(?string $p): string
{
    return $p ?? throw new Exception('Error');
}

function f3(?TestInterface $p): TestInterface
{
    return $p ?? throw new Exception('Error');
}


$r1 = f1(null);
$r2 = f2(null);
$r3 = f3(null);

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

INFO: Trace - 32:34 - $r1: string

INFO: Trace - 32:34 - $r2: string

INFO: Trace - 32:34 - $r3: TestInterface

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

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

INFO: UnusedVariable - 30:1 - $r3 is never referenced or the value is not used
https://psalm.dev/r/449c3a0338
<?php

interface TestInterface
{
    public function test(): void;
}

class Test
{
    public function __construct(
        public TestInterface|null $value
    ) {
    }
    
    public function getValue(): ?TestInterface
    {
        return $this->value;
    }

    public function getValue2(): TestInterface
    {
        if ($this->value === null) {
            throw new Exception('Error');
        }
        
        return $this->value;
    }

    public function getValue3(): TestInterface
    {
        return $this->value ?? throw new Exception('Error');
    }
}

$obj = new Test(null);
$r1 = $obj->getValue();
$r2 = $obj->getValue2();
$r3 = $obj->getValue3();

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

INFO: Trace - 40:34 - $r1: TestInterface|null

INFO: Trace - 40:34 - $r2: TestInterface

INFO: Trace - 40:34 - $r3: TestInterface

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

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

INFO: UnusedVariable - 38:1 - $r3 is never referenced or the value is not used
https://psalm.dev/r/a80b2963c6
<?php

interface TestInterface
{
    public function test(): void;
}

/**
 * @template T of TestInterface|null
 */
class Test
{
    /**
     * @psalm-param T $value
     */
    public function __construct(
        public TestInterface|null $value
    ) {
    }
    
    public function getValue(): ?TestInterface
    {
        return $this->value;
    }

    public function getValue2(): TestInterface
    {
        if ($this->value === null) {
            throw new Exception('Error');
        }
        
        return $this->value;
    }

    public function getValue3(): TestInterface
    {
        return $this->value ?? throw new Exception('Error');
    }
}

$obj = new Test(null);
$r1 = $obj->getValue();
$r2 = $obj->getValue2();
$r3 = $obj->getValue3();

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

INFO: Trace - 46:34 - $r1: TestInterface|null

INFO: Trace - 46:34 - $r2: TestInterface

INFO: Trace - 46:34 - $r3: TestInterface

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

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

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

ERROR: NullableReturnStatement - 37:16 - The declared return type 'TestInterface' for Test::getValue3 is not nullable, but the function returns 'T:Test as TestInterface|null'

ERROR: InvalidNullableReturnType - 35:34 - The declared return type 'TestInterface' for Test::getValue3 is not nullable, but 'T' contains null

@kkmuffme
Copy link
Contributor

kkmuffme commented May 3, 2024

That's probably a good first issue to give a PR a try. As a starting point src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/CoalesceAnalyzer.php

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

No branches or pull requests

3 participants