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
4 changes: 4 additions & 0 deletions src/Type/Constant/ConstantFloatType.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ public function toString(): Type
]);
}

if (is_nan($this->value)) {
return new ConstantStringType('NAN');
}

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

Expand Down
14 changes: 14 additions & 0 deletions tests/PHPStan/Analyser/nsrt/nan-to-string.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php // lint >= 8.0

namespace NanToString;

use function PHPStan\Testing\assertType;

function doFoo(): void
{
$nan = NAN;
assertType("'NAN'", (string) $nan);
assertType("'NAN'", $nan . '');
assertType("'INF'", (string) INF);
assertType("'-INF'", (string) -INF);
}
7 changes: 5 additions & 2 deletions turbo-ext/src/ConstantFloatType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,10 @@ class ConstantFloatType
}

/* new UnionType([new ConstantStringType('0'), new ConstantStringType('-0')])
* for 0.0 (and -0.0, which === 0.0), new ConstantStringType((string) $this->value)
* otherwise — the cast under the current `precision` ini */
* for 0.0 (and -0.0, which === 0.0), new ConstantStringType('NAN') for a
* NAN, which PHP 8.5's zend_double_to_str() would convert with a warning,
* new ConstantStringType((string) $this->value) otherwise — the cast under
* the current `precision` ini */
zv::Val toString() const
{
double v = 0.0;
Expand All @@ -193,6 +195,7 @@ class ConstantFloatType
types.push(std::move(negativeZero));
return pt_type_new_union(std::move(types));
}
if (std::isnan(v)) return pt_type_new_constant_string("NAN", 3);
zend_string *str = zend_double_to_str(v);
zv::Val result = pt_type_new_constant_string(ZSTR_VAL(str), ZSTR_LEN(str));
zend_string_release(str);
Expand Down
Loading