From 801809fbc0d68f691bc2a8a8014ad04ce0293145 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Sun, 25 Dec 2022 18:25:40 +0100 Subject: [PATCH] Try --- src/Psalm/Internal/Type/TypeCombiner.php | 48 ++++++++++++++----- src/Psalm/Type/Atomic.php | 8 +++- .../RedundantConditionTest.php | 4 +- 3 files changed, 44 insertions(+), 16 deletions(-) diff --git a/src/Psalm/Internal/Type/TypeCombiner.php b/src/Psalm/Internal/Type/TypeCombiner.php index 89abc13b52c..2ca2b41b14c 100644 --- a/src/Psalm/Internal/Type/TypeCombiner.php +++ b/src/Psalm/Internal/Type/TypeCombiner.php @@ -210,6 +210,7 @@ public static function combine( $new_types = self::handleKeyedArrayEntries( $combination, $overwrite_empty_array, + $from_docblock, ); } @@ -225,6 +226,7 @@ public static function combine( $allow_mixed_union, $type, $combination->array_type_params, + $from_docblock, ); } @@ -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( @@ -248,6 +250,7 @@ public static function combine( false, false, $combination->extra_types, + $from_docblock, ); $new_types[] = $generic_object; @@ -267,6 +270,7 @@ public static function combine( false, $combination->object_static[$generic_type] ?? false, $combination->extra_types, + $from_docblock, ); $new_types[] = $generic_object; @@ -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); } } } @@ -351,6 +362,8 @@ public static function combine( continue; } + $type->from_docblock = $from_docblock; + $new_types[] = $type; } @@ -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 = []; @@ -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( @@ -1406,6 +1421,7 @@ private static function handleKeyedArrayEntries( ? null : [$fallback_key_type, $fallback_value_type], (bool)$combination->all_arrays_lists, + $from_docblock, ); } @@ -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); } } @@ -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; @@ -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); } } @@ -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 @@ -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]; @@ -1535,6 +1553,7 @@ private static function getArrayTypeFromGenericParams( null, null, true, + $from_docblock, ); } else { $cnt = $combination->array_min_counts @@ -1552,6 +1571,7 @@ private static function getArrayTypeFromGenericParams( null, [Type::getListKey(), $generic_type_params[1]], true, + $from_docblock, ); } } else { @@ -1561,6 +1581,7 @@ private static function getArrayTypeFromGenericParams( $combination->array_min_counts ? min(array_keys($combination->array_min_counts)) : null, + $from_docblock, ); } } else { @@ -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); } } diff --git a/src/Psalm/Type/Atomic.php b/src/Psalm/Type/Atomic.php index ac47951ff8b..520ff00f6c8 100644 --- a/src/Psalm/Type/Atomic.php +++ b/src/Psalm/Type/Atomic.php @@ -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; diff --git a/tests/TypeReconciliation/RedundantConditionTest.php b/tests/TypeReconciliation/RedundantConditionTest.php index 12424087477..e020de6f6fd 100644 --- a/tests/TypeReconciliation/RedundantConditionTest.php +++ b/tests/TypeReconciliation/RedundantConditionTest.php @@ -1550,8 +1550,8 @@ function f(array $p) : void { 'code' => '