Skip to content

Operations inside of loops widens type of unrelated variable used in conditional statement #12666

Closed
phpstan/phpstan-src
#6421

Description

@stefanfisk

Bug report

It seems like modifying a variable inside of a loop where the conditional compares said variable to another variable sometimes widens the type of the other variable.

As an example, here the type of $xn is widened int after the loop. If the increment is changed to 1, $xn stay an int<1,max> however.

**
 * @param int<1,max> $x0
 * @param int<1,max> $xn
 */
function addFrac(int $x0, int $xn): float
{
	$xi = $x0;

	\PHPStan\dumpType($xn);

	while ($xi < $xn) {
		$xi += 0.1;
	}

	\PHPStan\dumpType($xn);

	return $xi;
}

Code snippet that reproduces the problem

https://phpstan.org/r/0bf36f1f-55eb-4378-8fcb-a5a7d1648ef2

Expected output

I'd expect the type of $xn to either remain the same or narrow.

Did PHPStan help you today? Did it make you happy in any way?

PHPStan always sparks joy!

Activity

  1. added this to the Easy fixes milestone on Mar 1, 2025
  2. github-actions commented on Oct 13, 2026

    @github-actions

    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.

  3. locked as resolved and limited conversation to collaborators on Oct 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions