vscode-intelephense icon indicating copy to clipboard operation
vscode-intelephense copied to clipboard

Return types not checked

Open momala454 opened this issue 4 years ago • 8 comments

Describe the bug It doesn't verify that the return type of a function inside an if is correct

To Reproduce

class TestBug {
    public int $test;
}

function testBug(): TestBug {
    if (1) {
        return [];
    }
    return new TestBug();
}

No error is shown on the line return []; while it's incorrect image

If i put return [] on the last line, it detects the error however. So the detection doesn't work inside if image

Expected behavior It must detect that the return type is invalid inside the if (1)

Screenshots If applicable, add screenshots to help explain your problem.

Platform and version windows intelephense 1.7.1

momala454 avatar Oct 15 '21 12:10 momala454

Hey :wave:

Your screenshot is a bit misleading. As the error message is not the correct one that you actually want to show.

You cannot create an instance of an array, like new []. Instead it is either new TestBug() or just [].

PythooonUser avatar Oct 15 '21 13:10 PythooonUser

sorry, i updated my post to fix the screenshot (which was just as reference as the screenshot wasn't the error itself). I've also added a new screenshot that shows no error while it should detect the mismatch

momala454 avatar Oct 15 '21 13:10 momala454

That is not entirely true. When you are using different types for the return statement, you can see it does the type checking somehow in the background nonetheless.

However, it does not report it. Probably, because it is not smart enough to evaluate that if(1) always terminates the function in this case. Like, the final return statement is also not reported to be unreachable.

image

PythooonUser avatar Oct 15 '21 14:10 PythooonUser

is it possible to have a return that doesn't terminate the function ? I don't understand why it doesn't report an error if any of the "return" statement is incorrect

momala454 avatar Oct 15 '21 14:10 momala454

It seems to just report the one that is last.

PythooonUser avatar Oct 15 '21 14:10 PythooonUser

to prevent errors i think it's best to check all of them, because I almost missed an error in my code due to one return having a wrong return type

momala454 avatar Oct 15 '21 14:10 momala454

it doesn't show any error as soon as one return is correct, even if it's not the last one :

function testHelloWorld(): string
{
    return 10;
    return "bla";
    return 10;
}

momala454 avatar Mar 21 '22 15:03 momala454

Just discovered this as well. We thought match may be broken.

function test1(bool $var) : GdImage
{
    return match ($var)
    {
        true => 'a',
        false => new GdImage(),
        default => null,
    };
}

this does not report any issues but if you comment out false => new GdImage(), you now get

Expected type 'GdImage'. Found 'string|null'.`

I was then able to replicate this with if/elseif/else statements.

function test2(bool $var) : GdImage
{
    // Force this to be true
    $var = true;

    if ($var == true)
    {
        return 'a';
    }
    else if ($var == false)
    {
        return new GdImage();
    }
    else 
    {
        return [1];
    }

    return 8;
}

This is fine, but again if you comment out return new GdImage(); you now get

Expected type 'GdImage'. Found 'string|int[]|int'.

It's almost like if the method can return the correct type at some point it is fine with returning other things.

beeradmoore avatar May 19 '22 03:05 beeradmoore