Skip to content

Commit

Permalink
Try
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Dec 25, 2022
1 parent a8ef02d commit 801809f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 16 deletions.
48 changes: 35 additions & 13 deletions src/Psalm/Internal/Type/TypeCombiner.php
Expand Up @@ -210,6 +210,7 @@ public static function combine(
$new_types = self::handleKeyedArrayEntries(
$combination,
$overwrite_empty_array,
$from_docblock,
);
}

Expand All @@ -225,6 +226,7 @@ public static function combine(
$allow_mixed_union,
$type,
$combination->array_type_params,
$from_docblock,
);
}

Expand All @@ -239,7 +241,7 @@ public static function combine(
foreach ($combination->builtin_type_params as $generic_type => $generic_type_params) {
if ($generic_type === 'iterable') {
assert(count($generic_type_params) <= 2);
$new_types[] = new TIterable($generic_type_params);
$new_types[] = new TIterable($generic_type_params, [], $from_docblock);
} else {
/** @psalm-suppress ArgumentTypeCoercion Caused by the PropertyTypeCoercion above */
$generic_object = new TGenericObject(
Expand All @@ -248,6 +250,7 @@ public static function combine(
false,
false,
$combination->extra_types,
$from_docblock,
);
$new_types[] = $generic_object;

Expand All @@ -267,6 +270,7 @@ public static function combine(
false,
$combination->object_static[$generic_type] ?? false,
$combination->extra_types,
$from_docblock,
);

$new_types[] = $generic_object;
Expand All @@ -293,9 +297,16 @@ public static function combine(

foreach ($object_type->getAtomicTypes() as $object_atomic_type) {
if ($object_atomic_type instanceof TNamedObject) {
$new_types[] = new TClassString($object_atomic_type->value, $object_atomic_type);
$new_types[] = new TClassString(
$object_atomic_type->value,
$object_atomic_type,
false,
false,
false,
$from_docblock,
);
} elseif ($object_atomic_type instanceof TObject) {
$new_types[] = new TClassString();
$new_types[] = new TClassString('object', null, false, false, false, $from_docblock);
}
}
}
Expand Down Expand Up @@ -351,6 +362,8 @@ public static function combine(
continue;
}

$type->from_docblock = $from_docblock;

$new_types[] = $type;
}

Expand Down Expand Up @@ -1321,7 +1334,8 @@ private static function getClassLikes(Codebase $codebase, string $fq_classlike_n
*/
private static function handleKeyedArrayEntries(
TypeCombination $combination,
bool $overwrite_empty_array
bool $overwrite_empty_array,
bool $from_docblock
): array {
$new_types = [];

Expand Down Expand Up @@ -1397,6 +1411,7 @@ private static function handleKeyedArrayEntries(
? null
: [$fallback_key_type, $fallback_value_type],
(bool)$combination->all_arrays_lists,
$from_docblock,
);
} else {
$objectlike = new TKeyedArray(
Expand All @@ -1406,6 +1421,7 @@ private static function handleKeyedArrayEntries(
? null
: [$fallback_key_type, $fallback_value_type],
(bool)$combination->all_arrays_lists,
$from_docblock,
);
}

Expand All @@ -1414,9 +1430,9 @@ private static function handleKeyedArrayEntries(
$key_type = $combination->objectlike_key_type ?? Type::getArrayKey();
$value_type = $combination->objectlike_value_type ?? Type::getMixed();
if ($combination->array_always_filled) {
$new_types[] = new TNonEmptyArray([$key_type, $value_type]);
$new_types[] = new TNonEmptyArray([$key_type, $value_type], $from_docblock);
} else {
$new_types[] = new TArray([$key_type, $value_type]);
$new_types[] = new TArray([$key_type, $value_type], $from_docblock);
}
}

Expand All @@ -1436,7 +1452,8 @@ private static function getArrayTypeFromGenericParams(
bool $overwrite_empty_array,
bool $allow_mixed_union,
Atomic $type,
array $generic_type_params
array $generic_type_params,
bool $from_docblock
): Atomic {
if ($combination->objectlike_entries) {
$objectlike_generic_type = null;
Expand All @@ -1455,11 +1472,11 @@ private static function getArrayTypeFromGenericParams(
);

if (is_int($property_name)) {
$objectlike_keys[$property_name] = new TLiteralInt($property_name);
$objectlike_keys[$property_name] = new TLiteralInt($property_name, $from_docblock);
} elseif ($type instanceof TKeyedArray && isset($type->class_strings[$property_name])) {
$objectlike_keys[$property_name] = new TLiteralClassString($property_name);
$objectlike_keys[$property_name] = new TLiteralClassString($property_name, $from_docblock);
} else {
$objectlike_keys[$property_name] = new TLiteralString($property_name);
$objectlike_keys[$property_name] = new TLiteralString($property_name, $from_docblock);
}
}

Expand Down Expand Up @@ -1504,7 +1521,7 @@ private static function getArrayTypeFromGenericParams(
}

if ($combination->all_arrays_callable) {
$array_type = new TCallableArray($generic_type_params);
$array_type = new TCallableArray($generic_type_params, $from_docblock);
} elseif ($combination->array_always_filled
|| ($combination->array_sometimes_filled && $overwrite_empty_array)
|| ($combination->objectlike_entries
Expand All @@ -1522,6 +1539,7 @@ private static function getArrayTypeFromGenericParams(
null,
[Type::getInt(), $combination->array_type_params[1]],
true,
$from_docblock,
);
} elseif ($combination->array_counts && count($combination->array_counts) === 1) {
$cnt = array_keys($combination->array_counts)[0];
Expand All @@ -1535,6 +1553,7 @@ private static function getArrayTypeFromGenericParams(
null,
null,
true,
$from_docblock,
);
} else {
$cnt = $combination->array_min_counts
Expand All @@ -1552,6 +1571,7 @@ private static function getArrayTypeFromGenericParams(
null,
[Type::getListKey(), $generic_type_params[1]],
true,
$from_docblock,
);
}
} else {
Expand All @@ -1561,6 +1581,7 @@ private static function getArrayTypeFromGenericParams(
$combination->array_min_counts
? min(array_keys($combination->array_min_counts))
: null,
$from_docblock,
);
}
} else {
Expand All @@ -1572,11 +1593,12 @@ private static function getArrayTypeFromGenericParams(
array_keys($combination->class_string_map_names)[0],
array_values($combination->class_string_map_as_types)[0],
$generic_type_params[1],
$from_docblock,
);
} elseif ($combination->all_arrays_lists) {
$array_type = Type::getListAtomic($generic_type_params[1]);
$array_type = Type::getListAtomic($generic_type_params[1], $from_docblock);
} else {
$array_type = new TArray($generic_type_params);
$array_type = new TArray($generic_type_params, $from_docblock);
}
}

Expand Down
8 changes: 7 additions & 1 deletion src/Psalm/Type/Atomic.php
Expand Up @@ -162,7 +162,13 @@ public static function create(
?string $text = null,
bool $from_docblock = false
): Atomic {
$result = self::createInner($value, $analysis_php_version_id, $template_type_map, $type_aliases, $from_docblock);
$result = self::createInner(
$value,
$analysis_php_version_id,
$template_type_map,
$type_aliases,
$from_docblock,
);
$result->offset_start = $offset_start;
$result->offset_end = $offset_end;
$result->text = $text;
Expand Down
4 changes: 2 additions & 2 deletions tests/TypeReconciliation/RedundantConditionTest.php
Expand Up @@ -1550,8 +1550,8 @@ function f(array $p) : void {
'code' => '<?php
/**
* @param array|null $value
* *
* * @return null
*
* @return null
*/
function reverseTransform($value)
{
Expand Down

0 comments on commit 801809f

Please sign in to comment.