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

while loop condition is not enforced after the loop #5322

Closed
homersimpsons opened this issue Jul 16, 2021 · 2 comments
Closed

while loop condition is not enforced after the loop #5322

homersimpsons opened this issue Jul 16, 2021 · 2 comments

Comments

@homersimpsons
Copy link

The following snippet analysis should not trigger any error:
https://phpstan.org/r/7ce1ea05-26eb-4ffc-aa6d-80a368bb0044

<?php declare(strict_types = 1);

function produceIntOrNull(): ?int {
  return rand(0, 2) === 0 ? null : 0;	
}

function expectInt(int $a): void {}

$int = null;
while (!is_int($int)) {
	$int = produceIntOrNull();
}

expectInt($int);

The while (!is_int($int)) { } ensures that $int is an int after the loop.

We can note that if constructs handle this well: https://phpstan.org/r/7fda8056-a088-44ba-9ee0-08623ce532be

There could be some tricky corner cases, notably with break

@ondrejmirtes
Copy link
Member

Hi, this is a no-brainer, thank you for the suggestion :) Implemented: phpstan/phpstan-src@3166ba3

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 19, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants