Skip to content
Permalink
Browse files

Improve accuracy around array addition

  • Loading branch information...
muglug committed Sep 8, 2019
1 parent b61d0c6 commit d27935d1099c3c2a9bafe5ada67cecefc2689aba
@@ -47,6 +47,7 @@
use function preg_quote;
use function strtolower;
use function strlen;
use DeepCopy\TypeMatcher\TypeMatcher;
/**
* @internal
@@ -1095,9 +1096,22 @@ private static function analyzeNonDivOperands(
$has_valid_right_operand = true;
$has_valid_left_operand = true;
if ($left_type_part instanceof ObjectLike && $right_type_part instanceof ObjectLike) {
if ($left_type_part instanceof ObjectLike
&& $right_type_part instanceof ObjectLike
) {
$definitely_existing_mixed_right_properties = array_diff_key(
$right_type_part->properties,
$left_type_part->properties
);
$properties = $left_type_part->properties + $right_type_part->properties;
if (!$left_type_part->sealed) {
foreach ($definitely_existing_mixed_right_properties as $key => $type) {
$properties[$key] = Type::combineUnionTypes(Type::getMixed(), $type);
}
}
$result_type_member = new Type\Union([new ObjectLike($properties)]);
} else {
$result_type_member = TypeCombination::combineTypes(
@@ -450,10 +450,14 @@ public static function getArrayAccessTypeGivenOffset(
) {
$from_string_key = $type->type_params[0]->isString();
$from_mixed_array = $type->type_params[1]->isMixed();
$from_empty_array = $type->type_params[0]->isEmpty() && $type->type_params[1]->isEmpty();
// ok, type becomes an ObjectLike
$array_type->removeType($type_string);
$type = new ObjectLike([$key_value => $from_mixed_array ? Type::getMixed() : Type::getEmpty()]);
$type->sealed = $from_empty_array;
if ($from_mixed_array) {
$type->had_mixed_value = true;
@@ -202,7 +202,7 @@ class B {}
'assertions' => [
'$foo' => 'array{0: string, 1: string, 2: string}',
'$bar' => 'array{0: int, 1: int, 2: int}',
'$bat' => 'array<string, int>',
'$bat' => 'non-empty-array<string, int>',
],
],
'implicitStringArrayCreation' => [
@@ -369,7 +369,7 @@ function fooFoo($a) {
$c[$b][$b][] = "bam";',
'assertions' => [
'$a' => 'array{boop: array<int, string>}',
'$c' => 'array{boop: array<string, array<int, string>>}',
'$c' => 'array{boop: non-empty-array<string, array<int, string>>}',
],
],
'assignExplicitValueToGeneric' => [
@@ -438,7 +438,7 @@ function fooFoo($a) {
'$e' => 'int',
],
],
'objectLikeArrayAddition' => [
'objectLikeArrayAdditionNotNested' => [
'<?php
$foo = [];
$foo["a"] = 1;
@@ -553,10 +553,10 @@ function fooFoo($a) {
$e[0][$int] = 3;
$e[0][$string] = 5;',
'assertions' => [
'$b' => 'array{0: array<string|int, int>}',
'$c' => 'array{0: array<int|string, int>}',
'$d' => 'array{0: array<int|string, int>}',
'$e' => 'array{0: array<string|int, int>}',
'$b' => 'array{0: non-empty-array<string|int, int>}',
'$c' => 'array{0: non-empty-array<int|string, int>}',
'$d' => 'array{0: non-empty-array<int|string, int>}',
'$e' => 'array{0: non-empty-array<string|int, int>}',
],
],
'updateStringIntKeyWithObjectLikeRootAndNumberOffset' => [
@@ -597,10 +597,10 @@ function fooFoo($a) {
$e["root"][$int] = 3;
$e["root"][$string] = 5;',
'assertions' => [
'$b' => 'array{root: array<string|int, int>}',
'$c' => 'array{root: array<int|string, int>}',
'$d' => 'array{root: array<int|string, int>}',
'$e' => 'array{root: array<string|int, int>}',
'$b' => 'array{root: non-empty-array<string|int, int>}',
'$c' => 'array{root: non-empty-array<int|string, int>}',
'$d' => 'array{root: non-empty-array<int|string, int>}',
'$e' => 'array{root: non-empty-array<string|int, int>}',
],
],
'mixedArrayAssignmentWithStringKeys' => [
@@ -1100,6 +1100,14 @@ public function foo() : void {
}
}'
],
'arrayAssignmentAddsTypePossibilities' => [
'<?php
function bar(array $value): void {
$value["b"] = "hello";
$value = $value + ["a" => 0];
if (is_int($value["a"])) {}
}'
],
];
}

0 comments on commit d27935d

Please sign in to comment.
You can’t perform that action at this time.