Skip to content

Commit

Permalink
Merge pull request #7356 from orklah/keep-class-strings-through-array…
Browse files Browse the repository at this point in the history
…_merge

keep class-strings through array_merge
  • Loading branch information
orklah committed Jan 9, 2022
2 parents 413da78 + 52ae26f commit 549c02d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
Expand Up @@ -50,6 +50,7 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev
$codebase = $statements_source->getCodebase();

$generic_properties = [];
$class_strings = [];
$all_keyed_arrays = true;
$all_int_offsets = true;
$all_nonempty_lists = true;
Expand Down Expand Up @@ -107,6 +108,10 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev
continue;
}

if (isset($unpacked_type_part->class_strings[$key])) {
$class_strings[$key] = true;
}

if (!isset($generic_properties[$key]) || !$type->possibly_undefined) {
$generic_properties[$key] = $type;
} else {
Expand Down Expand Up @@ -218,6 +223,10 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev
) {
$objectlike = new TKeyedArray($generic_properties);

if ($class_strings !== []) {
$objectlike->class_strings = $class_strings;
}

if ($all_nonempty_lists || $all_int_offsets) {
$objectlike->is_list = true;
}
Expand Down
18 changes: 18 additions & 0 deletions tests/ArrayFunctionCallTest.php
Expand Up @@ -2121,6 +2121,24 @@ function foo(): array
return $a;
}',
],
'keepClassStringInOffsetThroughArrayMerge' => [
'<?php
class A {
/** @var array<class-string, string> */
private array $a;
public function __construct() {
$this->a = [];
}
public function handle(): void {
$b = [A::class => "d"];
$this->a = array_merge($this->a, $b);
}
}
',
],
];
}

Expand Down

0 comments on commit 549c02d

Please sign in to comment.