Skip to content

Commit

Permalink
Fix exporting ConstantFloatType
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Apr 18, 2023
1 parent e01ce68 commit d2a94c7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Type/Constant/ConstantFloatType.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use PHPStan\Type\VerbosityLevel;
use function abs;
use function is_finite;
use function rtrim;
use function sprintf;
use function strpos;
use const PHP_FLOAT_EPSILON;

Expand Down Expand Up @@ -108,7 +110,7 @@ public function generalize(GeneralizePrecision $precision): Type
*/
public function toPhpDocNode(): TypeNode
{
return new ConstTypeNode(new ConstExprFloatNode((string) $this->value));
return new ConstTypeNode(new ConstExprFloatNode(rtrim(sprintf('%.20f', $this->value), '0.')));

This comment has been minimized.

Copy link
@rvanvelzen

rvanvelzen Apr 20, 2023

Contributor

This is incorrect - see https://3v4l.org/QZQ6P

This comment has been minimized.

Copy link
@ondrejmirtes

ondrejmirtes Apr 20, 2023

Author Member

I'm probably dumb but I need a real-world test, ideally in TypeToPhpDocNodeTest, to see where this fails.

}

/**
Expand Down
18 changes: 18 additions & 0 deletions tests/PHPStan/Type/TypeToPhpDocNodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,21 @@ public function dataToPhpDocNodeWithoutCheckingEquals(): iterable
new ConstantStringType("foo\nbar\nbaz"),
'(literal-string & non-falsy-string)',
];

yield [
new ConstantFloatType(9223372036854775807),
'9223372036854775808',
];

yield [
new ConstantFloatType(-9223372036854775808),
'-9223372036854775808',
];

yield [
new ConstantFloatType(2.35),
'2.35000000000000008882',
];
}

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

$typeString = (string) $phpDocNode;
$this->assertSame($expected, $typeString);

$typeStringResolver = self::getContainer()->getByType(TypeStringResolver::class);
$typeStringResolver->resolve($typeString);
}

public function dataFromTypeStringToPhpDocNode(): iterable
Expand Down

0 comments on commit d2a94c7

Please sign in to comment.