Skip to content

Commit

Permalink
Add more accomodations for TPositiveInt
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Sep 1, 2020
1 parent b5279cd commit c2f492a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Psalm/Internal/Type/Comparator/ScalarTypeComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,11 @@ public static function isContainedBy(
return true;
}

if (get_class($input_type_part) === TInt::class && $container_type_part instanceof TLiteralInt) {
if ((get_class($input_type_part) === TInt::class && $container_type_part instanceof TLiteralInt)
|| (get_class($input_type_part) === TPositiveInt::class
&& $container_type_part instanceof TLiteralInt
&& $container_type_part->value > 0)
) {
if ($atomic_comparison_result) {
$atomic_comparison_result->type_coerced = true;
$atomic_comparison_result->type_coerced_from_scalar = true;
Expand Down
12 changes: 12 additions & 0 deletions tests/ArrayAccessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,18 @@ function bar(int $i) {
if ($foo !== null) {}'
],
'accessKnownArrayWithPositiveInt' => [
'<?php
/** @param list<int> $arr */
function foo(array $arr) : void {
$o = [4, 15, 18, 21, 51];
$i = 0;
foreach ($arr as $a) {
if ($o[$i] === $a) {}
$i++;
}
}'
],
];
}

Expand Down

0 comments on commit c2f492a

Please sign in to comment.