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
15 changes: 8 additions & 7 deletions src/Type/ValueSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,7 @@ private function calculateShift(string $operator, self $right, PhpVersion $phpVe

/**
* Operands with more pairs of constants than are computed one by one become the
* type all their constants are: int, float, string or null; anything else can
* type all their constants are: int, float, int|float, string or null; anything else can
* only be an error.
*
* @return array{self, self}
Expand Down Expand Up @@ -976,12 +976,13 @@ private function generalize(): self
$hasStrings = $this->strings !== null;
$hasNumbers = $this->numbers !== null && !$this->numbers->isEmpty();
if (!$hasBooleans && !$hasStrings && !$this->null && $hasNumbers) {
if ($this->numbers->getFloatIntervals() === [] && !$this->numbers->hasNan()) {
return self::createNumbers(new NumberSet([[null, null]], [], false));
}
if ($this->numbers->getIntegerIntervals() === []) {
return self::createNumbers(new NumberSet([], [[-INF, INF]], true));
}
$hasFloats = $this->numbers->getFloatIntervals() !== [] || $this->numbers->hasNan();

return self::createNumbers(new NumberSet(
$this->numbers->getIntegerIntervals() !== [] ? [[null, null]] : [],
$hasFloats ? [[-INF, INF]] : [],
$hasFloats,
));
}
if (!$hasBooleans && !$hasNumbers && !$this->null && $hasStrings) {
return self::createStrings(new StringType());
Expand Down
38 changes: 38 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-15670.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Bug15670;

use function PHPStan\Testing\assertType;

/**
* @param 0|0.01|0.02|0.03|0.04|0.05|0.06|0.07|0.08|0.09|0.10|0.11 $a
* @param 0.1|0.2|0.3|0.4|0.5|0.6|0.7|0.8|0.9|1.0|1.1 $b
* @param 0|1|2|3|4|5|6|7|8|9|10|0.5 $c
* @param 1|2|3|4|5|6|7|8|9|10|11 $d
*/
function mixedIntFloat($a, $b, $c, $d): void
{
assertType('float', $a + $b);
assertType('float|int', $c + $d);
assertType('float|int', $c * $d);
}

final class Foo
{

private const WEIGHTS = [1 => 0.0761, 2 => 0.0720, 3 => 0.0811, 4 => 0.0812, 5 => 0.0813, 6 => 0.0814, 7 => 0.0815, 8 => 0.0816, 9 => 0.0817, 10 => 0.0818, 11 => 0.0819, 12 => 0.1290];

/** @param list<int<0, 12>> $months */
public function doFoo(array $months): void
{
$weight = 0;
foreach ($months as $month) {
if ($month === 0) {
break;
}
$weight += self::WEIGHTS[$month] ?? 0.0;
}
assertType('0|float', $weight);
}

}
12 changes: 6 additions & 6 deletions turbo-ext/src/ValueSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1172,13 +1172,13 @@ class ValueSetOps
bool hasNumbers = !numbersEmpty(s.numbers());
if (!hasBooleans && !hasStrings && !s.null_() && hasNumbers) {
NumbersView n(s.numbers());
if (n.floatCount() == 0 && !n.nan()) return createNumbersOf(anyInteger());
if (n.integerCount() == 0) {
NumberSetData anyFloat;
anyFloat.floatIntervals.push_back({-HUGE_VAL, HUGE_VAL});
anyFloat.nan = true;
return createNumbersOf(anyFloat);
bool hasFloats = n.floatCount() != 0 || n.nan();
NumberSetData generalized = n.integerCount() != 0 ? anyInteger() : NumberSetData();
if (hasFloats) {
generalized.floatIntervals.push_back({-HUGE_VAL, HUGE_VAL});
generalized.nan = true;
}
return createNumbersOf(generalized);
}
if (!hasBooleans && !hasNumbers && !s.null_() && hasStrings) {
IETR_VAL(stringType, newStringType());
Expand Down
Loading