Skip to content

Commit d2a94c7

Browse files
committed
Fix exporting ConstantFloatType
1 parent e01ce68 commit d2a94c7

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/Type/Constant/ConstantFloatType.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
use PHPStan\Type\VerbosityLevel;
1717
use function abs;
1818
use function is_finite;
19+
use function rtrim;
20+
use function sprintf;
1921
use function strpos;
2022
use const PHP_FLOAT_EPSILON;
2123

@@ -108,7 +110,7 @@ public function generalize(GeneralizePrecision $precision): Type
108110
*/
109111
public function toPhpDocNode(): TypeNode
110112
{
111-
return new ConstTypeNode(new ConstExprFloatNode((string) $this->value));
113+
return new ConstTypeNode(new ConstExprFloatNode(rtrim(sprintf('%.20f', $this->value), '0.')));
112114
}
113115

114116
/**

tests/PHPStan/Type/TypeToPhpDocNodeTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,21 @@ public function dataToPhpDocNodeWithoutCheckingEquals(): iterable
277277
new ConstantStringType("foo\nbar\nbaz"),
278278
'(literal-string & non-falsy-string)',
279279
];
280+
281+
yield [
282+
new ConstantFloatType(9223372036854775807),
283+
'9223372036854775808',
284+
];
285+
286+
yield [
287+
new ConstantFloatType(-9223372036854775808),
288+
'-9223372036854775808',
289+
];
290+
291+
yield [
292+
new ConstantFloatType(2.35),
293+
'2.35000000000000008882',
294+
];
280295
}
281296

282297
/**
@@ -288,6 +303,9 @@ public function testToPhpDocNodeWithoutCheckingEquals(Type $type, string $expect
288303

289304
$typeString = (string) $phpDocNode;
290305
$this->assertSame($expected, $typeString);
306+
307+
$typeStringResolver = self::getContainer()->getByType(TypeStringResolver::class);
308+
$typeStringResolver->resolve($typeString);
291309
}
292310

293311
public function dataFromTypeStringToPhpDocNode(): iterable

0 commit comments

Comments
 (0)