From f4f989d1e5117e98a0fce15e87014a4988e806dc Mon Sep 17 00:00:00 2001 From: Tyson Andre Date: Mon, 6 Sep 2021 21:42:56 -0400 Subject: [PATCH] Use different `in-array-*` check for non-literals Negating this would not contradict the original condition if the variables were different. A string can be in one array of strings but not be in a different array of strings. --- .../Statements/Expression/AssertionFinder.php | 15 ++++++-- tests/TypeReconciliation/InArrayTest.php | 34 +++++++++++++++++++ tests/TypeReconciliation/ValueTest.php | 22 ------------ 3 files changed, 46 insertions(+), 25 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php b/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php index c9a30914a..b1eded5b5 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php @@ -3456,13 +3456,22 @@ class AssertionFinder } } else { foreach ($value_type->getAtomicTypes() as $atomic_value_type) { - if ($atomic_value_type instanceof Type\Atomic\TFalse + if ($atomic_value_type instanceof Type\Atomic\TLiteralInt + || $atomic_value_type instanceof Type\Atomic\TLiteralString + || $atomic_value_type instanceof Type\Atomic\TLiteralFloat + || $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 ) { $assertions[] = $atomic_value_type->getAssertionString(); - } else { - $assertions[] = '=' . $atomic_value_type->getAssertionString(); + } elseif (!$atomic_value_type instanceof Type\Atomic\TMixed) { + // mixed doesn't tell us anything and can be omitted. + // + // For the meaning of in-array, see the above comment. + $assertions[] = 'in-array-' . $value_type->getId(); } } } diff --git a/tests/TypeReconciliation/InArrayTest.php b/tests/TypeReconciliation/InArrayTest.php index ec99cd9b4..6246324ea 100644 --- a/tests/TypeReconciliation/InArrayTest.php +++ b/tests/TypeReconciliation/InArrayTest.php @@ -211,6 +211,18 @@ class InArrayTest extends \Psalm\Tests\TestCase [], '8.0' ], + 'in_array-keyed-array-string-twice' => [ + ' 'MixedReturnStatement - src' . DIRECTORY_SEPARATOR . 'somefile.php:12:32 - Could not infer a return type', 'error_level' => ['RedundantConditionGivenDocblockType'], ], + 'inArrayDetectType' => [ + ' 'RedundantCondition', + ], + 'inArrayRemoveInvalid' => [ + ' 'RedundantCondition', + ], ]; } } diff --git a/tests/TypeReconciliation/ValueTest.php b/tests/TypeReconciliation/ValueTest.php index 495d4d97d..63cc2be66 100644 --- a/tests/TypeReconciliation/ValueTest.php +++ b/tests/TypeReconciliation/ValueTest.php @@ -972,28 +972,6 @@ class ValueTest extends \Psalm\Tests\TestCase }', 'error_message' => 'RedundantCondition', ], - 'inArrayDetectType' => [ - ' 'RedundantCondition', - ], - 'inArrayRemoveInvalid' => [ - ' 'RedundantCondition', - ], 'neverNotIdenticalFloatType' => [ '