Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix shaped array class string key combination #10450

Merged
merged 5 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,9 @@ private static function updateTypeWithKeyValues(
if (!$has_matching_objectlike_property && !$has_matching_string) {
$properties = [];
$classStrings = [];
$current_type = $current_type->setPossiblyUndefined(count($key_values) > 1);
$current_type = $current_type->setPossiblyUndefined(
$current_type->possibly_undefined || count($key_values) > 1,
);
foreach ($key_values as $key_value) {
$properties[$key_value->value] = $current_type;
if ($key_value instanceof TLiteralClassString) {
Expand Down
3 changes: 3 additions & 0 deletions src/Psalm/Internal/Type/TypeCombination.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ final class TypeCombination
/** @var array<string|int, Union> */
public array $objectlike_entries = [];

/** @var array<string, bool> */
public array $objectlike_class_string_keys = [];

public bool $objectlike_sealed = true;

public ?Union $objectlike_key_type = null;
Expand Down
16 changes: 15 additions & 1 deletion src/Psalm/Internal/Type/TypeCombiner.php
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,7 @@ private static function scrapeTypeProperties(

$has_defined_keys = false;

$class_strings = $type->class_strings ?? [];
foreach ($type->properties as $candidate_property_name => $candidate_property_type) {
$value_type = $combination->objectlike_entries[$candidate_property_name] ?? null;

Expand Down Expand Up @@ -706,6 +707,19 @@ private static function scrapeTypeProperties(
}

unset($missing_entries[$candidate_property_name]);

if (is_int($candidate_property_name)) {
continue;
}

if (isset($combination->objectlike_class_string_keys[$candidate_property_name])) {
$combination->objectlike_class_string_keys[$candidate_property_name] =
$combination->objectlike_class_string_keys[$candidate_property_name]
&& ($class_strings[$candidate_property_name] ?? false);
} else {
$combination->objectlike_class_string_keys[$candidate_property_name] =
($class_strings[$candidate_property_name] ?? false);
}
}

if ($type->fallback_params) {
Expand Down Expand Up @@ -1421,7 +1435,7 @@ private static function handleKeyedArrayEntries(
} else {
$objectlike = new TKeyedArray(
$combination->objectlike_entries,
null,
array_filter($combination->objectlike_class_string_keys),
$sealed || $fallback_key_type === null || $fallback_value_type === null
? null
: [$fallback_key_type, $fallback_value_type],
Expand Down
4 changes: 3 additions & 1 deletion src/Psalm/Type/Reconciler.php
Original file line number Diff line number Diff line change
Expand Up @@ -1132,7 +1132,9 @@ private static function adjustTKeyedArrayType(

$base_key = implode($key_parts);

$result_type = $result_type->setPossiblyUndefined(count($array_key_offsets) > 1);
$result_type = $result_type->setPossiblyUndefined(
$result_type->possibly_undefined || count($array_key_offsets) > 1,
);

foreach ($array_key_offsets as $array_key_offset) {
if (isset($existing_types[$base_key]) && $array_key_offset !== false) {
Expand Down
18 changes: 18 additions & 0 deletions tests/ArrayAssignmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,24 @@ public function providerValidCodeParse(): iterable
'$resultOpt===' => 'array{a?: true, b?: true}',
],
],
'assignUnionOfLiteralsClassKeys' => [
'code' => '<?php
class a {}
class b {}

$result = [];

foreach ([a::class, b::class] as $k) {
$result[$k] = true;
}

foreach ($result as $k => $v) {
$vv = new $k;
}',
'assertions' => [
'$result===' => 'array{a::class: true, b::class: true}',
],
],
'genericArrayCreationWithSingleIntValue' => [
'code' => '<?php
$out = [];
Expand Down