From 8954c12eaa83501f25011064f309325066fe04c4 Mon Sep 17 00:00:00 2001 From: orklah Date: Thu, 14 Jan 2021 22:35:06 +0100 Subject: [PATCH] don't throw InvalidArrayOffset when dealing with templates (#5019) --- .../Analyzer/Statements/Expression/ArrayAnalyzer.php | 1 + tests/ArrayAssignmentTest.php | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/ArrayAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/ArrayAnalyzer.php index 69f5d2729..2323e144d 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/ArrayAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/ArrayAnalyzer.php @@ -154,6 +154,7 @@ class ArrayAnalyzer && !$atomic_key_type instanceof Type\Atomic\TInt && !$atomic_key_type instanceof Type\Atomic\TArrayKey && !$atomic_key_type instanceof Type\Atomic\TMixed + && !$atomic_key_type instanceof Type\Atomic\TTemplateParam && !( $atomic_key_type instanceof Type\Atomic\TObjectWithProperties && isset($atomic_key_type->methods['__toString']) diff --git a/tests/ArrayAssignmentTest.php b/tests/ArrayAssignmentTest.php index 83c553a2b..bc367d9dc 100644 --- a/tests/ArrayAssignmentTest.php +++ b/tests/ArrayAssignmentTest.php @@ -1506,6 +1506,16 @@ class ArrayAssignmentTest extends TestCase return $list; }' ], + 'ArrayCreateTemplateArrayKey' => [ + '/** + * @template K of array-key + * @param K $key + */ + function with($key): void + { + [$key => 123]; + }', + ], ]; }