1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

Improve ergonomics of array assignment

This commit is contained in:
Brown 2020-04-11 17:50:24 -04:00
parent 2fe8717639
commit 7dfa37beb2
4 changed files with 31 additions and 7 deletions

View File

@ -428,7 +428,7 @@ class TypeCombination
$allow_mixed_union
);
if ($allow_mixed_union || !$generic_type_params[1]->isMixed()) {
if (!$generic_type_params[1]->isMixed()) {
$generic_type_params[1] = Type::combineUnionTypes(
$generic_type_params[1],
$objectlike_generic_type,

View File

@ -1402,9 +1402,26 @@ class ArrayAssignmentTest extends TestCase
$arr["hello"]["goodbye"] = 5;
}',
[
'$arr' => 'array<array-key, array{goodbye: int}|mixed>',
'$arr' => 'array<array-key, mixed>',
]
],
'dontUpdateMixedArrayWithStringKey' => [
'<?php
class A {}
/**
* @psalm-suppress MixedArgument
*/
function run1(array $arguments): void {
if (rand(0, 1)) {
$arguments["c"] = new A();
}
if ($arguments["b"]) {
echo $arguments["b"];
}
}',
],
];
}

View File

@ -383,7 +383,7 @@ class TypeCombinationTest extends TestCase
],
],
'combineMixedArrayWithObjectLike' => [
'array<array-key, int|mixed>',
'array<array-key, mixed>',
[
'array{a: int}',
'array',
@ -523,21 +523,28 @@ class TypeCombinationTest extends TestCase
],
],
'combineCallableArrayAndArray' => [
'array<array-key, mixed|string>',
'array<array-key, mixed>',
[
'callable-array{class-string, string}',
'array',
],
],
'combineObjectLikeArrayAndArray' => [
'combineGenericArrayAndMixedArray' => [
'array<array-key, int|mixed>',
[
'array<string, int>',
'array<array-key, mixed>',
],
],
'combineObjectLikeArrayAndArray' => [
'array<array-key, mixed>',
[
'array{hello: int}',
'array<array-key, mixed>',
],
],
'combineObjectLikeArrayAndNestedArray' => [
'array<array-key, array{goodbye: int}|mixed>',
'array<array-key, mixed>',
[
'array{hello: array{goodbye: int}}',
'array<array-key, mixed>',

View File

@ -139,7 +139,7 @@ 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'],
'callableOrArrayToCallableArray' => ['array<array-key, mixed|object|string>', 'array', 'callable|array'],
'callableOrArrayToCallableArray' => ['array<array-key, mixed>', 'array', 'callable|array'],
'traversableToIntersection' => ['Countable&Traversable', 'Traversable', 'Countable'],
'iterableWithoutParamsToTraversableWithoutParams' => ['Traversable', '!array', 'iterable'],
'iterableWithParamsToTraversableWithParams' => ['Traversable<int, string>', '!array', 'iterable<int, string>'],