From 550a7aabad538f442e8776a2f46e5f42a8de24e9 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Fri, 2 Dec 2022 18:03:59 +0100 Subject: [PATCH] Fixes --- src/Psalm/Internal/Diff/FileDiffer.php | 2 ++ .../PhpVisitor/PartialParserVisitor.php | 2 +- .../PhpVisitor/SimpleNameResolver.php | 2 +- .../Type/Comparator/ArrayTypeComparator.php | 19 ------------------- src/Psalm/Internal/Type/TypeCombiner.php | 1 - src/Psalm/Internal/Type/TypeParser.php | 3 +++ src/Psalm/Type/Atomic/GenericTrait.php | 5 ++--- src/Psalm/Type/Atomic/TKeyedArray.php | 1 + 8 files changed, 10 insertions(+), 25 deletions(-) diff --git a/src/Psalm/Internal/Diff/FileDiffer.php b/src/Psalm/Internal/Diff/FileDiffer.php index 675ce5c2c..4e1ee3fd8 100644 --- a/src/Psalm/Internal/Diff/FileDiffer.php +++ b/src/Psalm/Internal/Diff/FileDiffer.php @@ -81,6 +81,8 @@ class FileDiffer { $result = []; for ($d = count($trace) - 1; $d >= 0; --$d) { + // Todo: fix integer ranges in fors + /** @var int<0, max> $d */ $v = $trace[$d]; $k = $x - $y; diff --git a/src/Psalm/Internal/PhpVisitor/PartialParserVisitor.php b/src/Psalm/Internal/PhpVisitor/PartialParserVisitor.php index 4540c4dd2..4173f47f1 100644 --- a/src/Psalm/Internal/PhpVisitor/PartialParserVisitor.php +++ b/src/Psalm/Internal/PhpVisitor/PartialParserVisitor.php @@ -53,7 +53,7 @@ class PartialParserVisitor extends PhpParser\NodeVisitorAbstract /** @var PhpParser\ErrorHandler\Collecting */ private $error_handler; - /** @param array $offset_map */ + /** @param array $offset_map */ public function __construct( PhpParser\Parser $parser, PhpParser\ErrorHandler\Collecting $error_handler, diff --git a/src/Psalm/Internal/PhpVisitor/SimpleNameResolver.php b/src/Psalm/Internal/PhpVisitor/SimpleNameResolver.php index 0616249b1..1529704cc 100644 --- a/src/Psalm/Internal/PhpVisitor/SimpleNameResolver.php +++ b/src/Psalm/Internal/PhpVisitor/SimpleNameResolver.php @@ -30,7 +30,7 @@ class SimpleNameResolver extends NodeVisitorAbstract /** * @param ErrorHandler $errorHandler Error handler - * @param null|array $offset_map + * @param null|array $offset_map */ public function __construct(ErrorHandler $errorHandler, ?array $offset_map = null) { diff --git a/src/Psalm/Internal/Type/Comparator/ArrayTypeComparator.php b/src/Psalm/Internal/Type/Comparator/ArrayTypeComparator.php index 57ff583a8..b3c85187f 100644 --- a/src/Psalm/Internal/Type/Comparator/ArrayTypeComparator.php +++ b/src/Psalm/Internal/Type/Comparator/ArrayTypeComparator.php @@ -97,25 +97,6 @@ class ArrayTypeComparator return false; } - if ($container_type_part instanceof TKeyedArray - && $container_type_part->is_list - && $container_type_part->isNonEmpty() - && $input_type_part instanceof TNonEmptyArray - && $input_type_part->type_params[0]->isSingleIntLiteral() - && $input_type_part->type_params[0]->getSingleIntLiteral()->value === 0 - ) { - //this is a special case where the only offset value of an non empty array is 0, so it's a non empty list - return UnionTypeComparator::isContainedBy( - $codebase, - $input_type_part->type_params[1], - $container_type_part->getGenericValueType(), - $input_type_part->type_params[1]->ignore_nullable_issues, - $input_type_part->type_params[1]->ignore_falsable_issues, - $atomic_comparison_result, - $allow_interface_equality - ); - } - if ($container_type_part instanceof TKeyedArray) { $container_type_part = $container_type_part->getGenericArrayType(); } diff --git a/src/Psalm/Internal/Type/TypeCombiner.php b/src/Psalm/Internal/Type/TypeCombiner.php index b218a9359..5dcae773c 100644 --- a/src/Psalm/Internal/Type/TypeCombiner.php +++ b/src/Psalm/Internal/Type/TypeCombiner.php @@ -526,7 +526,6 @@ class TypeCombiner } foreach ($type->type_params as $i => $type_param) { - /** @psalm-suppress InvalidPropertyAssignmentValue */ $combination->array_type_params[$i] = Type::combineUnionTypes( $combination->array_type_params[$i] ?? null, $type_param, diff --git a/src/Psalm/Internal/Type/TypeParser.php b/src/Psalm/Internal/Type/TypeParser.php index cd635c83d..9752a6d31 100644 --- a/src/Psalm/Internal/Type/TypeParser.php +++ b/src/Psalm/Internal/Type/TypeParser.php @@ -681,6 +681,9 @@ class TypeParser } if ($generic_type_value === 'iterable') { + if (count($generic_params) > 2) { + throw new TypeParseTreeException('Too many template parameters for iterable'); + } return new TIterable($generic_params, [], $from_docblock); } diff --git a/src/Psalm/Type/Atomic/GenericTrait.php b/src/Psalm/Type/Atomic/GenericTrait.php index d4568475f..0bc56987c 100644 --- a/src/Psalm/Type/Atomic/GenericTrait.php +++ b/src/Psalm/Type/Atomic/GenericTrait.php @@ -240,7 +240,7 @@ trait GenericTrait ?Codebase $codebase ): ?array { $type_params = $this->type_params; - foreach ($type_params as $offset => $type_param) { + foreach ($type_params as $offset => &$type_param) { $type_param = TemplateInferredTypeReplacer::replace( $type_param, $template_result, @@ -250,9 +250,8 @@ trait GenericTrait if ($this instanceof TArray && $offset === 0 && $type_param->isMixed()) { $type_param = Type::getArrayKey(); } - - $type_params[$offset] = $type_param; } + unset($type_param); return $type_params === $this->type_params ? null : $type_params; } diff --git a/src/Psalm/Type/Atomic/TKeyedArray.php b/src/Psalm/Type/Atomic/TKeyedArray.php index 27815a24f..6afa0e801 100644 --- a/src/Psalm/Type/Atomic/TKeyedArray.php +++ b/src/Psalm/Type/Atomic/TKeyedArray.php @@ -398,6 +398,7 @@ class TKeyedArray extends Atomic if ($this->is_list) { foreach ($this->properties as $k => $property) { if ($property->possibly_undefined) { + /** @var int<0, max> */ return $k; } }