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

Improve type inference inside loops #3034

Open
momala454 opened this issue Aug 27, 2024 · 1 comment
Open

Improve type inference inside loops #3034

momala454 opened this issue Aug 27, 2024 · 1 comment

Comments

@momala454
Copy link

Describe the bug
A clear and concise description of what the bug is.

To Reproduce

function world(string $a): void
{
}

/** @param array<string> $a */
function hello(array $a): void
{
    $bestValue = null;
    foreach ($a as $test) {
        if ($bestValue === null) {
            $bestValue = $test;
        } else {
            var_dump($bestValue);
            world($bestValue);
        }
    }
}
hello(['hello', 'world']);

Expected behavior
Type is string, not never

Screenshots
image

Platform and version
1.12.4

@bmewburn
Copy link
Owner

There's currently a limitation in the control flow analysis implementation. A workaround is to widen the type with an annotation.

/** @var string|null $bestValue */
$bestValue = null;

@bmewburn bmewburn changed the title Incorrect *never* type Improve type inference inside loops Aug 29, 2024
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

2 participants