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

InvalidReturnType incorrect error #6386

Closed
KorDum opened this issue Sep 2, 2021 · 7 comments · Fixed by #6392
Closed

InvalidReturnType incorrect error #6386

KorDum opened this issue Sep 2, 2021 · 7 comments · Fixed by #6392

Comments

@KorDum
Copy link

KorDum commented Sep 2, 2021

For example
https://psalm.dev/r/d75a4c2020
Code above throws exception or returns string.

@psalm-github-bot
Copy link

I found these snippets:

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

class Test {
	private int $retryAttempts = 10;

    private function getResult(): string
    {
        // return tring or throw exception whatever
        throw new Exception();
    }

    private function getResultWithRetry(): string
    {
        $attempt = 1;

        while (true) {
            try {
                return $this->getResult();
            } catch (Throwable $exception) {
                if ($attempt >= $this->retryAttempts) {
                    throw $exception;
                }

                $attempt++;

                continue;
            }
        }
    }
}
Psalm output (using commit eb973ab):

ERROR: InvalidReturnType - 12:44 - Not all code paths of Test::getResultWithRetry end in a return statement, return type string expected

@weirdan
Copy link
Collaborator

weirdan commented Sep 2, 2021

As a workaround, you can throw exception after the while block: https://psalm.dev/r/1e77336875

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/1e77336875
<?php

class Test {
	private int $retryAttempts = 10;

    private function getResult(): string
    {
        // return tring or throw exception whatever
        throw new Exception();
    }

    private function getResultWithRetry(): string
    {
        $attempt = 1;

        while (true) {
            try {
                return $this->getResult();
            } catch (Throwable $exception) {
                if ($attempt >= $this->retryAttempts) {
                    throw $exception;
                }

                $attempt++;

                continue;
            }
        }
        throw new RuntimeException('should never happen');
    }
}
Psalm output (using commit eb973ab):

No issues!

@KorDum
Copy link
Author

KorDum commented Sep 2, 2021

As a workaround, you can throw exception after the while block: https://psalm.dev/r/1e77336875

Sorry, but it is incorrect dead code. IDE and phpstan will sound the alarm.

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/1e77336875
<?php

class Test {
	private int $retryAttempts = 10;

    private function getResult(): string
    {
        // return tring or throw exception whatever
        throw new Exception();
    }

    private function getResultWithRetry(): string
    {
        $attempt = 1;

        while (true) {
            try {
                return $this->getResult();
            } catch (Throwable $exception) {
                if ($attempt >= $this->retryAttempts) {
                    throw $exception;
                }

                $attempt++;

                continue;
            }
        }
        throw new RuntimeException('should never happen');
    }
}
Psalm output (using commit eb973ab):

No issues!

@orklah
Copy link
Collaborator

orklah commented Sep 2, 2021

simplified:
https://psalm.dev/r/bd185e9e6a

@psalm-github-bot
Copy link

I found these snippets:

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

function getResultWithRetry(): string
{
    while (true) {
        return '';
    }
}
Psalm output (using commit eb973ab):

ERROR: InvalidReturnType - 3:32 - Not all code paths of getResultWithRetry end in a return statement, return type string expected

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

Successfully merging a pull request may close this issue.

3 participants