Skip to content

Commit

Permalink
Merge pull request #7400 from orklah/combinePositiveAndRange
Browse files Browse the repository at this point in the history
Combine positive int and range
  • Loading branch information
orklah committed Jan 15, 2022
2 parents 4e27889 + 0beb8e7 commit 32c8e63
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Psalm/Internal/Type/TypeCombiner.php
Expand Up @@ -1181,6 +1181,13 @@ function ($int): bool {
}
} elseif (!isset($combination->value_types['int'])) {
$combination->value_types['int'] = $type;
} elseif ($combination->value_types['int'] instanceof TIntRange) {
//if we already had a range, we ensure the min is no higher than 1
$combination->value_types['int']->min_bound = TIntRange::getNewLowestBound(
$combination->value_types['int']->min_bound,
1
);
$combination->value_types['int']->max_bound = null;
} elseif (get_class($combination->value_types['int']) !== get_class($type)) {
$combination->value_types['int'] = new TInt();
}
Expand Down
13 changes: 13 additions & 0 deletions tests/IntRangeTest.php
Expand Up @@ -684,6 +684,19 @@ function doAnalysis(): void
'$length===' => '1',
],
],
'PositiveIntCombinedWithIntRange' => [
'<?php
/** @var positive-int */
$int = 1;
/** @var array<int<0, max>, int<0, max>> */
$_arr = [];
$_arr[1] = $int;
$_arr[$int] = 2;',
'assertions' => [
'$_arr===' => 'non-empty-array<int<0, max>, int<0, max>>',
],
],
];
}

Expand Down

0 comments on commit 32c8e63

Please sign in to comment.