Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Casting int-range should keep literals #10941

Merged
merged 1 commit into from
May 5, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Psalm\Type\Atomic\TFalse;
use Psalm\Type\Atomic\TFloat;
use Psalm\Type\Atomic\TInt;
use Psalm\Type\Atomic\TIntRange;
use Psalm\Type\Atomic\TKeyedArray;
use Psalm\Type\Atomic\TList;
use Psalm\Type\Atomic\TLiteralFloat;
Expand All @@ -53,6 +54,7 @@
use function array_pop;
use function array_values;
use function get_class;
use function range;
use function strtolower;

/**
Expand Down Expand Up @@ -537,6 +539,18 @@ public static function castFloatAttempt(
continue;
}

if ($atomic_type instanceof TIntRange
&& $atomic_type->min_bound !== null
&& $atomic_type->max_bound !== null
&& ($atomic_type->max_bound - $atomic_type->min_bound) < 500
) {
foreach (range($atomic_type->min_bound, $atomic_type->max_bound) as $literal_int_value) {
$valid_floats[] = new TLiteralFloat((float) $literal_int_value);
}

continue;
}

if ($atomic_type instanceof TInt) {
if ($atomic_type instanceof TLiteralInt) {
$valid_floats[] = new TLiteralFloat((float) $atomic_type->value);
Expand Down Expand Up @@ -721,9 +735,17 @@ public static function castStringAttempt(
|| $atomic_type instanceof TNumeric
) {
if ($atomic_type instanceof TLiteralInt || $atomic_type instanceof TLiteralFloat) {
$castable_types[] = Type::getAtomicStringFromLiteral((string) $atomic_type->value);
$valid_strings[] = Type::getAtomicStringFromLiteral((string) $atomic_type->value);
} elseif ($atomic_type instanceof TNonspecificLiteralInt) {
$castable_types[] = new TNonspecificLiteralString();
} elseif ($atomic_type instanceof TIntRange
&& $atomic_type->min_bound !== null
&& $atomic_type->max_bound !== null
&& ($atomic_type->max_bound - $atomic_type->min_bound) < 500
) {
foreach (range($atomic_type->min_bound, $atomic_type->max_bound) as $literal_int_value) {
$valid_strings[] = Type::getAtomicStringFromLiteral((string) $literal_int_value);
}
} else {
$castable_types[] = new TNumericString();
}
Expand Down
10 changes: 10 additions & 0 deletions tests/CastTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,15 @@ public function providerValidCodeParse(): iterable
'$a===' => 'array{a: int, b: string, ...<array-key, mixed>}',
],
];
yield 'castIntRangeToString' => [
'code' => '<?php
/** @var int<-5, 3> */
$int_range = 2;
$string = (string) $int_range;
',
'assertions' => [
'$string===' => "'-1'|'-2'|'-3'|'-4'|'-5'|'0'|'1'|'2'|'3'",
],
];
}
}
Loading