mirror of
https://github.com/danog/psalm.git
synced 2025-01-21 21:31:13 +01:00
Add support for string range
This commit is contained in:
parent
69d44070f8
commit
f8f5ddfb04
@ -649,20 +649,56 @@ class FunctionAnalyzer extends FunctionLikeAnalyzer
|
||||
|
||||
case 'range':
|
||||
$all_ints = true;
|
||||
$all_strings = true;
|
||||
$all_floats = true;
|
||||
$all_numbers = true;
|
||||
|
||||
foreach ($call_args as $call_arg) {
|
||||
$all_ints = $all_ints
|
||||
&& isset($call_arg->value->inferredType)
|
||||
&& $call_arg->value->inferredType->isInt();
|
||||
$is_int = false;
|
||||
$is_float = false;
|
||||
$is_string = false;
|
||||
|
||||
if (isset($call_arg->value->inferredType)) {
|
||||
if ($call_arg->value->inferredType->isInt()) {
|
||||
$is_int = true;
|
||||
} elseif ($call_arg->value->inferredType->isFloat()) {
|
||||
$is_float = true;
|
||||
} elseif ($call_arg->value->inferredType->isString()) {
|
||||
$is_string = true;
|
||||
}
|
||||
}
|
||||
|
||||
$all_ints = $all_ints && $is_int;
|
||||
|
||||
$all_floats = $all_floats && $is_float;
|
||||
|
||||
$all_strings = $all_strings && $is_string;
|
||||
|
||||
$all_numbers = $all_numbers && ($is_int || $is_float);
|
||||
}
|
||||
|
||||
if ($all_ints) {
|
||||
return new Type\Union([new Type\Atomic\TArray([Type::getInt(), Type::getInt()])]);
|
||||
}
|
||||
|
||||
if ($all_strings) {
|
||||
return new Type\Union([new Type\Atomic\TArray([Type::getInt(), Type::getString()])]);
|
||||
}
|
||||
|
||||
if ($all_floats) {
|
||||
return new Type\Union([new Type\Atomic\TArray([Type::getInt(), Type::getFloat()])]);
|
||||
}
|
||||
|
||||
if ($all_numbers) {
|
||||
return new Type\Union([new Type\Atomic\TArray([
|
||||
Type::getInt(),
|
||||
new Type\Union([new Type\Atomic\TInt, new Type\Atomic\TFloat])
|
||||
])]);
|
||||
}
|
||||
|
||||
return new Type\Union([new Type\Atomic\TArray([
|
||||
Type::getInt(),
|
||||
new Type\Union([new Type\Atomic\TInt, new Type\Atomic\TFloat])
|
||||
new Type\Union([new Type\Atomic\TInt, new Type\Atomic\TFloat, new Type\Atomic\TString])
|
||||
])]);
|
||||
|
||||
case 'get_parent_class':
|
||||
|
@ -1332,6 +1332,15 @@ class FunctionCallTest extends TestCase
|
||||
foo($x);
|
||||
}',
|
||||
],
|
||||
'rangeWithNoStepAndString' => [
|
||||
'<?php
|
||||
|
||||
function foo(string $bar) : void {}
|
||||
|
||||
foreach (range("a", "z") as $x) {
|
||||
foo($x);
|
||||
}',
|
||||
],
|
||||
'rangeWithFloatStep' => [
|
||||
'<?php
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user