Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 6 additions & 1 deletion src/Type/Accessory/AccessoryDecimalIntegerStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,14 @@ public function toInteger(): Type
return new IntegerType();
}

/** A decimal integer string holds an int, which converts to a finite float. */
public function toFloat(): Type
{
return new FloatType();
if ($this->inverse) {
return new FloatType();
}

return (new IntegerType())->toFloat();
}

public function toString(): Type
Expand Down
5 changes: 4 additions & 1 deletion src/Type/IntegerType.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
use PHPStan\Type\Traits\OperatorTypeTrait;
use PHPStan\Type\Traits\UndecidedBooleanTypeTrait;
use PHPStan\Type\Traits\UndecidedComparisonTypeTrait;
use const PHP_INT_MAX;
use const PHP_INT_MIN;

/** @api */
#[ShadowedByTurboExtension(implementation: __DIR__ . '/../../turbo-ext/src/IntegerType.cpp')]
Expand Down Expand Up @@ -76,9 +78,10 @@ public function toAbsoluteNumber(): Type
return IntegerRangeType::createAllGreaterThanOrEqualTo(0);
}

/** An int converts to the float closest to it, which is never INF, -INF or NAN. */
public function toFloat(): Type
{
return new FloatType();
return FloatRangeType::fromInterval((float) PHP_INT_MIN, (float) PHP_INT_MAX);
}

public function toInteger(): Type
Expand Down
6 changes: 3 additions & 3 deletions src/Type/NumberSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -449,8 +449,8 @@ private function toHull(): self
}

/**
* The floats an integer interval stands for. An unbounded side reaches the
* infinity on that side, which the integer never is.
* The floats an integer interval stands for. An unbounded side ends at the
* float closest to PHP_INT_MIN or PHP_INT_MAX: an int is never infinite.
*
* @param array{int|null, int|null} $interval
* @return array{float, float, bool} [min, max, whether an infinite bound is a value]
Expand All @@ -459,7 +459,7 @@ private static function integerToFloatInterval(array $interval): array
{
[$min, $max] = $interval;

return [$min === null ? -INF : (float) $min, $max === null ? INF : (float) $max, false];
return [(float) ($min ?? PHP_INT_MIN), (float) ($max ?? PHP_INT_MAX), false];
}

/**
Expand Down
12 changes: 6 additions & 6 deletions tests/PHPStan/Analyser/nsrt/binary.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ public function doFoo(array $generalArray)
assertType('1', 1 * 1);
assertType('1', 1 ** 1);
assertType('0', 1 % 1);
assertType('(float|int)', $integer1 /= 2);
assertType('(float<-4.611686018427388E+18, 4.611686018427388E+18>|int)', $integer1 /= 2);
assertType('int', $integer2 *= 1);
assertType('2.5999999999999996', 1.2 + 1.4);
assertType('-0.19999999999999996', 1.2 - 1.4);
Expand All @@ -301,10 +301,10 @@ public function doFoo(array $generalArray)
assertType('1.4', 1 * 1.4);
assertType('1.0', 1 ** 1.4);
assertType('1', 3 % 2.4);
assertType('float', $integer3 /= 2.4);
assertType('float', $integer4 *= 2.4);
assertType('float<-3.8430716820228234E+18, 3.8430716820228234E+18>', $integer3 /= 2.4);
assertType('float<-2.213609288845146E+19, 2.213609288845146E+19>', $integer4 *= 2.4);
assertType('int', $otherInteger + 1);
assertType('float', $otherInteger + 1.0);
assertType('float<-9.223372036854776E+18, 9.223372036854776E+18>', $otherInteger + 1.0);
assertType('2.2', 1.2 + 1);
assertType('0.19999999999999996', 1.2 - 1);
assertType('0.6', 1.2 / 2);
Expand Down Expand Up @@ -459,8 +459,8 @@ public function doFoo(array $generalArray)
assertType('int<0, max>', count($appendingToArrayInBranches));
assertType('3|5', count($conditionalArray));
assertType('2', $array[1]);
assertType('(float|int)', $integer / $integer);
assertType('(float|int)', $otherInteger / $integer);
assertType('(float<-9.223372036854776E+18, 9.223372036854776E+18>|int)', $integer / $integer);
assertType('(float<-9.223372036854776E+18, 9.223372036854776E+18>|int)', $otherInteger / $integer);
assertType('(array|float|int)', $mixed + $mixed);
assertType('(float|int)', $mixed - $mixed);
assertType('array', $mixed + []);
Expand Down
4 changes: 2 additions & 2 deletions tests/PHPStan/Analyser/nsrt/bug-12393.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function doFoo(array $plugin){

public function doBar(int $i){
$this->float = $i;
assertType('float', $this->float);
assertType('float<-9.223372036854776E+18, 9.223372036854776E+18>', $this->float);
}

public function doBaz(int $i){
Expand Down Expand Up @@ -89,7 +89,7 @@ public function doFoo(array $plugin){

public function doBar(int $i){
self::$float = $i;
assertType('float', self::$float);
assertType('float<-9.223372036854776E+18, 9.223372036854776E+18>', self::$float);
}

public function doBaz(int $i){
Expand Down
4 changes: 2 additions & 2 deletions tests/PHPStan/Analyser/nsrt/bug-12393b.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function doFoo(array $plugin){

public function doBar(int $i){
$this->float = $i;
assertType('float', $this->float);
assertType('float<-9.223372036854776E+18, 9.223372036854776E+18>', $this->float);
}

public function doBaz(int $i){
Expand Down Expand Up @@ -91,7 +91,7 @@ public function doFoo(array $plugin){

public function doBar(int $i){
self::$float = $i;
assertType('float', self::$float);
assertType('float<-9.223372036854776E+18, 9.223372036854776E+18>', self::$float);
}

public function doBaz(int $i){
Expand Down
57 changes: 57 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-15767.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php declare(strict_types = 1);

namespace Bug15767;

use function PHPStan\Testing\assertType;

/** @return finite-float */
function int_to_float(int $x): float {
assertType('float<-9.223372036854776E+18, 9.223372036854776E+18>', (float) $x);
return (float) $x;
}

/**
* @param decimal-int-string $x
* @return finite-float
*/
function decimal_int_string_to_float(string $x): float {
assertType('float<-9.223372036854776E+18, 9.223372036854776E+18>', (float) $x);
return (float) $x;
}

/**
* @param decimal-int-string $d
* @param non-decimal-int-string $nd
* @param int<0, max> $nonNegative
*/
function alternatives(int $x, string $d, string $nd, int $nonNegative): void
{
assertType('float<-9.223372036854776E+18, 9.223372036854776E+18>', floatval($x));
assertType('float<-9.223372036854776E+18, 9.223372036854776E+18>', doubleval($d));
assertType('float', (float) $nd);
assertType('float<-9.223372036854776E+18, 9.223372036854776E+18>', $x + 0.0);
assertType('float<-9.223372036854776E+18, 9.223372036854776E+18>', 0.0 + $x);
assertType('float<-9.223372036854776E+18, 9.223372036854776E+18>', $x * 1.0);
assertType('float<-9.223372036854776E+18, 9.223372036854776E+18>', $x - 0.0);
assertType('float<-9.223372036854776E+18, 9.223372036854776E+18>', $x / 1.0);
assertType('float<-9.223372036854776E+18, 9.223372036854776E+18>', $d + 0.0);
assertType('float<0.0, 9.223372036854776E+18>', $nonNegative + 0.0);
assertType('float<-1.8446744073709552E+19, 1.8446744073709552E+19>', $x * 2.0);
assertType('float', $x * 1e300);
assertType('(float<-4.611686018427388E+18, 4.611686018427388E+18>|int)', $x / 2);

$y = $x;
$y += 0.0;
assertType('float<-9.223372036854776E+18, 9.223372036854776E+18>', $y);

$y = $x;
settype($y, 'float');
assertType('float<-9.223372036854776E+18, 9.223372036854776E+18>', $y);

$y = $d;
settype($y, 'double');
assertType('float<-9.223372036854776E+18, 9.223372036854776E+18>', $y);

assertType('true', is_finite((float) $x));
assertType('false', is_nan($x + 0.0));
}
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/bug-9724.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function thisDoesntWork(?int $limit, int $offset = 0): void
public function withRange(int $limit, int $offset, int $offsetRange): void
{
if ($limit) {
assertType('(float|int)', $offset / $limit);
assertType('(float<-9.223372036854776E+18, 9.223372036854776E+18>|int)', $offset / $limit);
assertType('float<-2.0, 2.0>|int<-2, 2>', $offsetRange / $limit);
}
}
Expand Down
16 changes: 8 additions & 8 deletions tests/PHPStan/Analyser/nsrt/division-by-possible-zero.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
*/
function doFoo(int $range1, int $range2, int $int, int $r1, int $i, int $rMin, int $a, int $b, int $p, int $u): void
{
assertType('float<0.0, 5.0>|int<1, 5>', 5 / $range1);
assertType('float<-5.0, 0.0>|int<-5, -1>', 5 / $range2);
assertType('float<min, 0.0>|int<min, 0>', $range1 / $range2);
assertType('(float<-5.0, 5.0>|int<-5, -1>|int<1, 5>)', 5 / $int);
assertType('(float<-10.0, 10.0>|int<-10, -1>|int<1, 10>)', $r1 / $i);
assertType('float<-10.0, 0.0>|float<0.2, 10.0>|int<-10, -1>|int<1, 10>', $r1 / $rMin);
assertType('float<0.0, max>|int<0, max>', $a / $b);
assertType('-1|1|float<-1.0, -0.2>|float<0.0, 1.0>', -1 / $rMin);
assertType('float<5.421010862427522E-19, 5.0>|int<1, 5>', 5 / $range1);
assertType('float<-5.0, -5.421010862427522E-19>|int<-5, -1>', 5 / $range2);
assertType('float<-9.223372036854776E+18, 0.0>|int<min, 0>', $range1 / $range2);
assertType('(float<-5.0, -5.421010862427522E-19>|float<5.421010862427522E-19, 5.0>|int<-5, -1>|int<1, 5>)', 5 / $int);
assertType('(float<-10.0, -1.0842021724855044E-19>|float<1.0842021724855044E-19, 10.0>|int<-10, -1>|int<1, 10>)', $r1 / $i);
assertType('float<-10.0, -1.0842021724855044E-19>|float<0.2, 10.0>|int<-10, -1>|int<1, 10>', $r1 / $rMin);
assertType('float<0.0, 9.223372036854776E+18>|int<0, max>', $a / $b);
assertType('-1|1|float<-1.0, -0.2>|float<1.0842021724855044E-19, 1.0>', -1 / $rMin);
assertType('float<-2.0, 2.0>|int<-2, 2>', $p / $u);
}
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/filter-var.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function scalars(bool $bool, float $float, int $int, string $string, int
assertType('17.0', filter_var(17.0, FILTER_VALIDATE_FLOAT));
assertType('17.1', filter_var(17.1, FILTER_VALIDATE_FLOAT));
assertType('1.0E-50', filter_var(1e-50, FILTER_VALIDATE_FLOAT));
assertType('float', filter_var($int, FILTER_VALIDATE_FLOAT));
assertType('float<-9.223372036854776E+18, 9.223372036854776E+18>', filter_var($int, FILTER_VALIDATE_FLOAT));
assertType('float<0.0, 9.0>', filter_var($intRange, FILTER_VALIDATE_FLOAT));
assertType('17.0', filter_var(17, FILTER_VALIDATE_FLOAT));
assertType('float|false', filter_var($string, FILTER_VALIDATE_FLOAT));
Expand Down
4 changes: 2 additions & 2 deletions tests/PHPStan/Analyser/nsrt/float-range-arithmetic.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public function doFoo(float $a, float $b, float $p, float $nn, float $fin, int $
assertType('float<-6.0, 8.0>', $a * $b);
assertType('float<-3.0, 4.0>', $b * 1);

// a non-int bounded operand stays unbounded on that side
assertType('float<min, max>', $a + $int);
// an int operand ends at the floats closest to PHP_INT_MIN and PHP_INT_MAX
assertType('float<-9.223372036854776E+18, 9.223372036854776E+18>', $a + $int);
assertType('float', $a + $f);

// overflow reaches the infinities
Expand Down
4 changes: 2 additions & 2 deletions tests/PHPStan/Analyser/nsrt/float-range-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ public function producers(array $list): void
assertType('float<0.0, max>', microtime(true));

// https://github.com/phpstan/phpstan/issues/6963
assertType('float<0.0, max>|int<0, max>', count($list) / 4);
assertType('float<0.0, max>', ceil(count($list) / 4));
assertType('float<0.0, 2.305843009213694E+18>|int<0, max>', count($list) / 4);
assertType('float<0.0, 9.223372036854776E+18>', ceil(count($list) / 4));
}

}
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/float-range-narrowing.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ function casts(int $oneToFive, int $nonNegative, int $upToMinusThree, int $int):
assertType('float<1.0, 5.0>', (float) $oneToFive);
assertType('float<0.0, 9.223372036854776E+18>', (float) $nonNegative);
assertType('float<-9.223372036854776E+18, -3.0>', floatval($upToMinusThree));
assertType('float', (float) $int);
assertType('float<-9.223372036854776E+18, 9.223372036854776E+18>', (float) $int);
}

/**
Expand Down
20 changes: 10 additions & 10 deletions tests/PHPStan/Analyser/nsrt/integer-range-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,11 @@ public function math($i, $j, $z, $pi, $r1, $r2, $r3, $rMin, $rMax, $x, $y) {
assertType('int<2, max>', $pi + 1);
assertType('int<-1, max>', $pi - 2);
assertType('int<2, max>', $pi * 2);
assertType('float<0.5, max>|int<1, max>', $pi / 2);
assertType('float<0.5, 4.611686018427388E+18>|int<1, max>', $pi / 2);
assertType('int<2, max>', 1 + $pi);
assertType('int<min, 1>', 2 - $pi);
assertType('int<2, max>', 2 * $pi);
assertType('float<0.0, 2.0>|int<1, 2>', 2 / $pi);
assertType('float<2.168404344971009E-19, 2.0>|int<1, 2>', 2 / $pi);

assertType('int<5, 14>', $r1 + 4);
assertType('int<-3, 6>', $r1 - 4);
Expand All @@ -251,7 +251,7 @@ public function math($i, $j, $z, $pi, $r1, $r2, $r3, $rMin, $rMax, $x, $y) {
assertType('int<9, max>', $rMax + 4);
assertType('int<1, max>', $rMax - 4);
assertType('int<20, max>', $rMax * 4);
assertType('float<1.25, max>|int<2, max>', $rMax / 4);
assertType('float<1.25, 2.305843009213694E+18>|int<2, max>', $rMax / 4);

assertType('int<6, 20>', $r1 + $r2);
assertType('int<-9, 5>', $r1 - $r2);
Expand All @@ -266,21 +266,21 @@ public function math($i, $j, $z, $pi, $r1, $r2, $r3, $rMin, $rMax, $x, $y) {
assertType('int<min, 15>', $rMin + $r1);
assertType('int<min, 4>', $rMin - $r1);
assertType('int<min, 50>', $rMin * $r1);
assertType('float<min, 5.0>|int<min, 5>', $rMin / $r1);
assertType('float<-9.223372036854776E+18, 5.0>|int<min, 5>', $rMin / $r1);

assertType('int<6, max>', $r1 + $rMax);
assertType('int<min, 5>', $r1 - $rMax);
assertType('int<5, max>', $r1 * $rMax);
assertType('float<0.0, 2.0>|int<1, 2>', $r1 / $rMax);
assertType('float<1.0842021724855044E-19, 2.0>|int<1, 2>', $r1 / $rMax);
assertType('int<6, max>', $rMax + $r1);
assertType('int<-5, max>', $rMax - $r1);
assertType('int<5, max>', $rMax * $r1);
assertType('float<0.5, max>|int<1, max>', $rMax / $r1);
assertType('float<0.5, 9.223372036854776E+18>|int<1, max>', $rMax / $r1);

assertType('5|10|15|20|30', $x / $y);

assertType('float<0.0, max>|int<1, max>', $rMax / $rMax);
assertType('(float|int)', $rMin / $rMin);
assertType('float<5.421010862427522E-19, 1.8446744073709553E+18>|int<1, max>', $rMax / $rMax);
assertType('(float<-9.223372036854776E+18, 9.223372036854776E+18>|int)', $rMin / $rMin);
}

/**
Expand Down Expand Up @@ -308,10 +308,10 @@ public function maximaInversion($rMin, $rMax) {
assertType('int<-5, max>', $rMin * -1);
assertType('int<min, -10>', $rMax * -2);

assertType('float<-0.4, 0.0>', -2 / $rMax);
assertType('float<-0.4, -2.168404344971009E-19>', -2 / $rMax);

assertType('int<-5, max>', $rMin / -1);
assertType('float<min, -2.5>|int<min, -3>', $rMax / -2);
assertType('float<-4.611686018427388E+18, -2.5>|int<min, -3>', $rMax / -2);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions tests/PHPStan/Analyser/nsrt/math.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,18 @@ public function doIpsum(int $newLevel): void
$min = min(30, $newLevel);
assertType('int<min, 30>', $min);
$minDivFive = $min / 5;
assertType('float<min, 6.0>|int<min, 6>', $minDivFive);
assertType('float<-1.8446744073709553E+18, 6.0>|int<min, 6>', $minDivFive);
$volume = 0x10000000 * $minDivFive;
assertType('float<min, 1610612736.0>|int<min, 1610612736>', $volume);
assertType('float<-4.9517601571415214E+26, 1610612736.0>|int<min, 1610612736>', $volume);
}

public function doDolor(int $i): void
{
$chunks = min(200, $i);
assertType('int<min, 200>', $chunks);
$divThirty = $chunks / 30;
assertType('float<min, 6.666666666666667>|int<min, 6>', $divThirty);
assertType('float<min, 9.666666666666668>|int<min, 9>', $divThirty + 3);
assertType('float<-3.0744573456182586E+17, 6.666666666666667>|int<min, 6>', $divThirty);
assertType('float<-3.0744573456182586E+17, 9.666666666666668>|int<min, 9>', $divThirty + 3);
}

public function doSit(int $i, int $j): void
Expand Down
6 changes: 3 additions & 3 deletions tests/PHPStan/Analyser/nsrt/node-callback-scope.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ public function doPlus($a, $b, int $c, int $d): void
*/
public function doDiv($a, $b, int $c, int $d): void
{
assertType('(float|int)', $a / $b);
assertType('(float<-9.223372036854776E+18, 9.223372036854776E+18>|int)', $a / $b);
assertNativeType('(float|int)', $a / $b);
assertType('1', 1 / 1);
assertNativeType('1', 1 / 1);
assertType('(float|int)', $c / $d);
assertNativeType('(float|int)', $c / $d);
assertType('(float<-9.223372036854776E+18, 9.223372036854776E+18>|int)', $c / $d);
assertNativeType('(float<-9.223372036854776E+18, 9.223372036854776E+18>|int)', $c / $d);

assertType('*ERROR*', $c / 0); // DivisionByZeroError
}
Expand Down
4 changes: 2 additions & 2 deletions tests/PHPStan/Analyser/nsrt/set-type-type-specifying.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function doFloat(string $s, int $i, float $f, array $a, object $o)
assertType('float', $s);

settype($i, 'float');
assertType('float', $i);
assertType('float<-9.223372036854776E+18, 9.223372036854776E+18>', $i);

settype($f, 'float');
assertType('float', $f);
Expand All @@ -83,7 +83,7 @@ function doDouble(string $s, int $i, float $f, array $a, object $o)
assertType('float', $s);

settype($i, 'double');
assertType('float', $i);
assertType('float<-9.223372036854776E+18, 9.223372036854776E+18>', $i);

settype($f, 'double');
assertType('float', $f);
Expand Down
Loading
Loading