1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Fix #266 - do better analysis of array_rand

And also have better fallback when accessing string key on object-like array
This commit is contained in:
Matt Brown 2017-11-07 17:38:54 -05:00
parent a50783404f
commit 750d18d20a
3 changed files with 22 additions and 0 deletions

View File

@ -1130,6 +1130,8 @@ class FetchChecker
)) {
return false;
}
} elseif ($key_type->hasString()) {
$stmt->inferredType = $type->getGenericTypeParam();
}
}
continue;

View File

@ -107,3 +107,11 @@ function array_pop(array $arr) {}
* @return array<TKey, TValue>
*/
function array_reverse(array $arr) {}
/**
* @template TKey
*
* @param array<TKey, mixed> $arr
* @return TKey
*/
function array_rand(array $arr) {}

View File

@ -281,6 +281,18 @@ class FunctionCallTest extends TestCase
fooFoo($a->foo);
fooFoo($b);',
],
'arrayRand' => [
'<?php
$vars = ["x" => "a", "y" => "b"];
$c = array_rand($vars);
$d = $vars[$c];',
'assertions' => [
'$vars' => 'array{x:string, y:string}',
'$c' => 'string',
'$d' => 'string',
],
],
];
}