From 58e7ae1809a5b2b63a27cd7bc67173485368f37b Mon Sep 17 00:00:00 2001 From: Tyson Andre Date: Sat, 4 Sep 2021 12:28:34 -0400 Subject: [PATCH] Also support non-literal types in strict in_array() check Modified approach to #6396 by orklah Fixes #6333 Co-Authored-By: orklah --- .../Statements/Expression/AssertionFinder.php | 7 ++-- tests/TypeReconciliation/InArrayTest.php | 26 +++++++++++++++ tests/TypeReconciliation/ValueTest.php | 33 +++++++++++++++++++ 3 files changed, 61 insertions(+), 5 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php b/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php index adf27c957d6..cee52afa875 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php @@ -3442,7 +3442,7 @@ private static function getInarrayAssertions( $assertions = []; if (!$is_sealed) { - if ($value_type->getId() !== '') { + if ($value_type->getId() !== '' && !$value_type->isMixed()) { $assertions[] = 'in-array-' . $value_type->getId(); } } else { @@ -3453,10 +3453,7 @@ private static function getInarrayAssertions( || $atomic_value_type instanceof Type\Atomic\TEnumCase ) { $assertions[] = '=' . $atomic_value_type->getAssertionString(); - } elseif ($atomic_value_type instanceof Type\Atomic\TFalse - || $atomic_value_type instanceof Type\Atomic\TTrue - || $atomic_value_type instanceof Type\Atomic\TNull - ) { + } else { $assertions[] = $atomic_value_type->getAssertionString(); } } diff --git a/tests/TypeReconciliation/InArrayTest.php b/tests/TypeReconciliation/InArrayTest.php index 6b3e21b732c..41655033900 100644 --- a/tests/TypeReconciliation/InArrayTest.php +++ b/tests/TypeReconciliation/InArrayTest.php @@ -169,6 +169,32 @@ function assertInArray(string|bool $x, $y) { [], '8.0' ], + 'in_arrayNullOrString' => [ + ' [ + ' 'RedundantCondition', ], + 'inArrayRemoveNull' => [ + ' 'RedundantCondition', + ], + 'inArrayDetectType' => [ + ' 'RedundantCondition', + ], + 'inArrayRemoveInvalid' => [ + ' 'RedundantCondition', + ], 'neverNotIdenticalFloatType' => [ '