1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 12:24:49 +01:00

only transform a Keyed array into callable-array if there's two elements (#5086)

* only transform a Keyed array into callable-array if there's two elements in array

* add tests
This commit is contained in:
orklah 2021-01-22 22:14:29 +01:00 committed by Daniil Gentili
parent c837535c9d
commit c9129b5c4c
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
2 changed files with 4 additions and 1 deletions

View File

@ -40,6 +40,7 @@ use Psalm\Type\Atomic\TString;
use Psalm\Type\Atomic\TTemplateParam;
use function strpos;
use function substr;
use function count;
class SimpleAssertionReconciler extends \Psalm\Type\Reconciler
{
@ -1988,7 +1989,7 @@ class SimpleAssertionReconciler extends \Psalm\Type\Reconciler
$type = new TCallableList($type->type_param);
$callable_types[] = $type;
$did_remove_type = true;
} elseif ($type instanceof TKeyedArray) {
} elseif ($type instanceof TKeyedArray && count($type->properties) === 2) {
$type = clone $type;
$type = new TCallableKeyedArray($type->properties);
$callable_types[] = $type;

View File

@ -126,6 +126,8 @@ class ReconcilerTest extends \Psalm\Tests\TestCase
'iterableToArray' => ['array<int, int>', 'array', 'iterable<int, int>'],
'iterableToTraversable' => ['Traversable<int, int>', 'Traversable', 'iterable<int, int>'],
'callableToCallableArray' => ['callable-array{0: class-string|object, 1: string}', 'array', 'callable'],
'SmallKeyedArrayAndCallable' => ['array{test: string}', 'array{test: string}', 'callable'],
'BigKeyedArrayAndCallable' => ['array{foo: string, test: string, thing: string}', 'array{foo: string, test: string, thing: string}', 'callable'],
'callableOrArrayToCallableArray' => ['array<array-key, mixed>', 'array', 'callable|array'],
'traversableToIntersection' => ['Countable&Traversable', 'Traversable', 'Countable'],
'iterableWithoutParamsToTraversableWithoutParams' => ['Traversable', '!array', 'iterable'],