Skip to content

False-positive division-by-zero warning when using floats #5309

Description

@dhirtzbruch

I am using phpstan version 0.12.92.

function sayHello(float $y): float {
	$x = 0.0;
	if($y > 0) {
		$x += 1;
	}
	if($x > 0) {
		return 5 / $x;
	}
	
	return 1.0;
}

Even though I check for > 0 before division, phpstan complains about division by zero:

Binary operation "/" between 5 and 0.0|1.0 results in an error.

Reproducer here

The same code changed to integers does not raise any issue.

I stumbled upon that issue while slowly increasing phpstan check level on a legacy codebase fixing all newly found code issues.

I really appreciate your great work on phpstan. Makes life much easier and code more reliable.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions