From e7bc6f273abef924b177b6b326fbf2565c096da6 Mon Sep 17 00:00:00 2001 From: orklah Date: Wed, 5 Jan 2022 22:33:20 +0100 Subject: [PATCH 1/2] don't crash when pushing a template to in_array --- .../Statements/Expression/AssertionFinder.php | 2 +- tests/AssertAnnotationTest.php | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php b/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php index c5854130a2c..035effe4fc2 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php @@ -3556,7 +3556,7 @@ private static function getInarrayAssertions( // - The array may have one of the types but not the others. // // NOTE: the negation of the negation is the original assertion. - if ($value_type->getId() !== '' && !$value_type->isMixed()) { + if ($value_type->getId() !== '' && !$value_type->isMixed() && !$value_type->hasTemplate()) { $assertions[] = 'in-array-' . $value_type->getId(); } } else { diff --git a/tests/AssertAnnotationTest.php b/tests/AssertAnnotationTest.php index ade990073f5..5e311c37414 100644 --- a/tests/AssertAnnotationTest.php +++ b/tests/AssertAnnotationTest.php @@ -1898,6 +1898,29 @@ function assertBarNotNull(Foo $foo): bool function requiresString(string $_str): void {} ', ], + 'assertInArrayWithTemplateDontCrash' => [ + ' $objects + * @return array + */ + private function uniquateObjects(array $objects) : array + { + $uniqueObjects = []; + foreach ($objects as $object) { + if (in_array($object, $uniqueObjects, true)) { + continue; + } + $uniqueObjects[] = $object; + } + + return array_values($uniqueObjects); + } + } + ', + ], 'assertionOnMagicProperty' => [ ' Date: Wed, 5 Jan 2022 22:39:13 +0100 Subject: [PATCH 2/2] fix test --- tests/AssertAnnotationTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/AssertAnnotationTest.php b/tests/AssertAnnotationTest.php index 5e311c37414..92f47dfa3a2 100644 --- a/tests/AssertAnnotationTest.php +++ b/tests/AssertAnnotationTest.php @@ -1916,7 +1916,7 @@ private function uniquateObjects(array $objects) : array $uniqueObjects[] = $object; } - return array_values($uniqueObjects); + return $uniqueObjects; } } ',