Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Type/Constant/ConstantFloatType.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PHPStan\Type\Traits\ConstantNumericComparisonTypeTrait;
use PHPStan\Type\Traits\ConstantScalarTypeTrait;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use PHPStan\Type\VerbosityLevel;
use function abs;
use function ini_get;
Expand Down Expand Up @@ -69,6 +70,13 @@ public function describe(VerbosityLevel $level): string

public function toString(): Type
{
if ($this->value === 0.0) {
return new UnionType([
new ConstantStringType('0'),
new ConstantStringType('-0'),
]);
}

return new ConstantStringType((string) $this->value);
}

Expand Down
44 changes: 44 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-12225.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php declare(strict_types = 1);

namespace Bug12225;

use function PHPStan\Testing\assertType;

function floatAsserts(float $num): ?int
{
if ($num === 0.0) {
assertType('0.0', $num);
assertType("'-0'|'0'", (string) $num);
}

if ($num == 0) {
assertType('0.0', $num);
assertType("'-0'|'0'", (string) $num);
}

assertType("'-0'|'0'", (string) 0.0); // could be '0'
assertType("'-0'|'0'", (string) -0.0); // could be '-0'

assertType('-0.0', -1.0 * 0.0);
assertType('0.0', 1.0 * 0.0);
assertType('0.0', -1.0 * -0.0);
assertType('-0.0', 1.0 * -0.0);

assertType('true', 0.0 === -0.0);
assertType('true', 0.0 == -0.0);

return null;
}

/** @param mixed $num */
function withMixed($num): ?int
{
if ($num === 0.0) {
assertType('0.0', $num);
assertType("'-0'|'0'", (string) $num);
}

assertType('true', 0.0 === -0.0);

return null;
}
Loading