1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Improve handling of coerced-to-array strings in array_splice

This commit is contained in:
Matt Brown 2018-08-14 11:51:17 -04:00
parent cb63f4f70f
commit 366f625c83
2 changed files with 28 additions and 0 deletions

View File

@ -391,6 +391,16 @@ class CallChecker
return false;
}
if (isset($replacement_arg->inferredType)
&& !$replacement_arg->inferredType->hasArray()
&& $replacement_arg->inferredType->hasString()
&& $replacement_arg->inferredType->isSingle()
) {
$replacement_arg->inferredType = new Type\Union([
new Type\Atomic\TArray([Type::getInt(), $replacement_arg->inferredType])
]);
}
if (isset($array_arg->inferredType)
&& $array_arg->inferredType->hasArray()
&& isset($replacement_arg->inferredType)
@ -419,8 +429,18 @@ class CallChecker
$context,
false
);
return;
}
ExpressionChecker::assignByRefParam(
$statements_checker,
$array_arg,
Type::getArray(),
$context,
false
);
return;
}

View File

@ -920,6 +920,14 @@ class FunctionCallTest extends TestCase
'$c' => 'array{0:int, 1:int, 2:int}',
],
],
'arraySpliceOtherType' => [
'<?php
$d = [["red"], ["green"], ["blue"]];
array_splice($d, -1, 1, "foo");',
'assertions' => [
'$d' => 'array<int, array{0:string}|string>',
],
],
];
}