Skip to content

Commit 309179a

Browse files
phpstan-botclaude
andcommitted
Merge foreach loops for gotGreater/gotSmaller and newMin/newMax computation
Compute $newMin/$newMax in the same loop that determines $gotGreater/$gotSmaller, eliminating the duplicate foreach over $constantIntegers['b'] and $integerRanges['b']. Also applies the symmetric fix for integer range generalization in the gotGreater && gotSmaller case (previously only fixed for constant integers). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 14a2154 commit 309179a

1 file changed

Lines changed: 27 additions & 21 deletions

File tree

src/Analyser/MutatingScope.php

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4100,30 +4100,24 @@ private function generalizeType(Type $a, Type $b, int $depth): Type
41004100

41014101
$gotGreater = false;
41024102
$gotSmaller = false;
4103+
$newMin = $min;
4104+
$newMax = $max;
41034105
foreach ($constantIntegers['b'] as $int) {
41044106
if ($int->getValue() > $max) {
41054107
$gotGreater = true;
41064108
}
4107-
if ($int->getValue() >= $min) {
4108-
continue;
4109+
if ($int->getValue() < $min) {
4110+
$gotSmaller = true;
4111+
}
4112+
if ($int->getValue() < $newMin) {
4113+
$newMin = $int->getValue();
4114+
}
4115+
if ($int->getValue() > $newMax) {
4116+
$newMax = $int->getValue();
41094117
}
4110-
4111-
$gotSmaller = true;
41124118
}
41134119

41144120
if ($gotGreater && $gotSmaller) {
4115-
$newMin = $min;
4116-
$newMax = $max;
4117-
foreach ($constantIntegers['b'] as $int) {
4118-
if ($int->getValue() < $newMin) {
4119-
$newMin = $int->getValue();
4120-
}
4121-
if ($int->getValue() <= $newMax) {
4122-
continue;
4123-
}
4124-
4125-
$newMax = $int->getValue();
4126-
}
41274121
$resultTypes[] = IntegerRangeType::fromInterval($newMin, $newMax);
41284122
} elseif ($gotGreater) {
41294123
$resultTypes[] = IntegerRangeType::fromInterval($min, null);
@@ -4174,6 +4168,8 @@ private function generalizeType(Type $a, Type $b, int $depth): Type
41744168

41754169
$gotGreater = false;
41764170
$gotSmaller = false;
4171+
$newMin = $min;
4172+
$newMax = $max;
41774173
foreach ($integerRanges['b'] as $range) {
41784174
if ($range->getMin() === null) {
41794175
$rangeMin = PHP_INT_MIN;
@@ -4189,11 +4185,15 @@ private function generalizeType(Type $a, Type $b, int $depth): Type
41894185
if ($rangeMax > $max) {
41904186
$gotGreater = true;
41914187
}
4192-
if ($rangeMin >= $min) {
4193-
continue;
4188+
if ($rangeMin < $min) {
4189+
$gotSmaller = true;
4190+
}
4191+
if ($rangeMin < $newMin) {
4192+
$newMin = $rangeMin;
4193+
}
4194+
if ($rangeMax > $newMax) {
4195+
$newMax = $rangeMax;
41944196
}
4195-
4196-
$gotSmaller = true;
41974197
}
41984198

41994199
if ($min === PHP_INT_MIN) {
@@ -4202,9 +4202,15 @@ private function generalizeType(Type $a, Type $b, int $depth): Type
42024202
if ($max === PHP_INT_MAX) {
42034203
$max = null;
42044204
}
4205+
if ($newMin === PHP_INT_MIN) {
4206+
$newMin = null;
4207+
}
4208+
if ($newMax === PHP_INT_MAX) {
4209+
$newMax = null;
4210+
}
42054211

42064212
if ($gotGreater && $gotSmaller) {
4207-
$resultTypes[] = new IntegerType();
4213+
$resultTypes[] = IntegerRangeType::fromInterval($newMin, $newMax);
42084214
} elseif ($gotGreater) {
42094215
$resultTypes[] = IntegerRangeType::fromInterval($min, null);
42104216
} elseif ($gotSmaller) {

0 commit comments

Comments
 (0)