Skip to content

Commit efef7fc

Browse files
VincentLangletondrejmirtes
authored andcommitted
Fix ConstantFloatType::toString
1 parent 2c53ed6 commit efef7fc

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/Type/Constant/ConstantFloatType.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PHPStan\Type\Traits\ConstantNumericComparisonTypeTrait;
1212
use PHPStan\Type\Traits\ConstantScalarTypeTrait;
1313
use PHPStan\Type\Type;
14+
use PHPStan\Type\UnionType;
1415
use PHPStan\Type\VerbosityLevel;
1516
use function abs;
1617
use function ini_get;
@@ -69,6 +70,13 @@ public function describe(VerbosityLevel $level): string
6970

7071
public function toString(): Type
7172
{
73+
if ($this->value === 0.0) {
74+
return new UnionType([
75+
new ConstantStringType('0'),
76+
new ConstantStringType('-0'),
77+
]);
78+
}
79+
7280
return new ConstantStringType((string) $this->value);
7381
}
7482

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug12225;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
function floatAsserts(float $num): ?int
8+
{
9+
if ($num === 0.0) {
10+
assertType('0.0', $num);
11+
assertType("'-0'|'0'", (string) $num);
12+
}
13+
14+
if ($num == 0) {
15+
assertType('0.0', $num);
16+
assertType("'-0'|'0'", (string) $num);
17+
}
18+
19+
assertType("'-0'|'0'", (string) 0.0); // could be '0'
20+
assertType("'-0'|'0'", (string) -0.0); // could be '-0'
21+
22+
assertType('-0.0', -1.0 * 0.0);
23+
assertType('0.0', 1.0 * 0.0);
24+
assertType('0.0', -1.0 * -0.0);
25+
assertType('-0.0', 1.0 * -0.0);
26+
27+
assertType('true', 0.0 === -0.0);
28+
assertType('true', 0.0 == -0.0);
29+
30+
return null;
31+
}
32+
33+
/** @param mixed $num */
34+
function withMixed($num): ?int
35+
{
36+
if ($num === 0.0) {
37+
assertType('0.0', $num);
38+
assertType("'-0'|'0'", (string) $num);
39+
}
40+
41+
assertType('true', 0.0 === -0.0);
42+
43+
return null;
44+
}

0 commit comments

Comments
 (0)