Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Dec 6, 2023
1 parent 0a513e8 commit 9c99d3a
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ private static function updateTypeWithKeyValues(
$properties = [];
$classStrings = [];
$current_type = $current_type->setPossiblyUndefined(
$current_type->possibly_undefined || count($key_values) > 1
$current_type->possibly_undefined || count($key_values) > 1,
);
foreach ($key_values as $key_value) {
$properties[$key_value->value] = $current_type;
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 @@ -57,6 +57,9 @@ final class TypeCombination
/** @var array<string|int, Union> */
public array $objectlike_entries = [];

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

public bool $objectlike_sealed = true;

public ?Union $objectlike_key_type = null;
Expand Down
12 changes: 11 additions & 1 deletion src/Psalm/Internal/Type/TypeCombiner.php
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,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 @@ -700,6 +701,15 @@ private static function scrapeTypeProperties(
);
}

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);
}

unset($missing_entries[$candidate_property_name]);
}

Expand Down Expand Up @@ -1415,7 +1425,7 @@ private static function handleKeyedArrayEntries(
} else {
$objectlike = new TKeyedArray(
$combination->objectlike_entries,
null,
$combination->objectlike_class_string_keys,
$sealed || $fallback_key_type === null || $fallback_value_type === null
? null
: [$fallback_key_type, $fallback_value_type],
Expand Down
3 changes: 1 addition & 2 deletions src/Psalm/Type/Reconciler.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
use Psalm\Type\Atomic\TFalse;
use Psalm\Type\Atomic\TInt;
use Psalm\Type\Atomic\TKeyedArray;
use Psalm\Type\Atomic\TList;
use Psalm\Type\Atomic\TLiteralInt;
use Psalm\Type\Atomic\TLiteralString;
use Psalm\Type\Atomic\TMixed;
Expand Down Expand Up @@ -1130,7 +1129,7 @@ private static function adjustTKeyedArrayType(
$base_key = implode($key_parts);

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

foreach ($array_key_offsets as $array_key_offset) {
Expand Down
18 changes: 18 additions & 0 deletions tests/ArrayAssignmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,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

0 comments on commit 9c99d3a

Please sign in to comment.