From 2a92db8fada8d4677188f3c28550a2006d256d53 Mon Sep 17 00:00:00 2001 From: Matthew Brown Date: Wed, 22 Dec 2021 19:47:20 +0000 Subject: [PATCH] Remove unused in-array assertions These assertions cannot be parsed, since the `in-array` assertion handler always tries to convert assertions to a type --- .../Type/NegatedAssertionReconciler.php | 47 ++++++++----------- .../Type/SimpleAssertionReconciler.php | 10 +--- src/Psalm/Type/Reconciler.php | 6 --- 3 files changed, 21 insertions(+), 42 deletions(-) diff --git a/src/Psalm/Internal/Type/NegatedAssertionReconciler.php b/src/Psalm/Internal/Type/NegatedAssertionReconciler.php index 6ecc9177e..4c910db97 100644 --- a/src/Psalm/Internal/Type/NegatedAssertionReconciler.php +++ b/src/Psalm/Internal/Type/NegatedAssertionReconciler.php @@ -3,7 +3,6 @@ namespace Psalm\Internal\Type; use Psalm\CodeLocation; -use Psalm\Exception\TypeParseTreeException; use Psalm\Internal\Analyzer\StatementsAnalyzer; use Psalm\Internal\Analyzer\TraitAnalyzer; use Psalm\Internal\Type\Comparator\AtomicTypeComparator; @@ -158,35 +157,29 @@ class NegatedAssertionReconciler extends Reconciler if (strpos($assertion, 'in-array-') === 0) { $assertion = substr($assertion, 9); - $new_var_type = null; - try { - $new_var_type = Type::parseString($assertion); - } catch (TypeParseTreeException $e) { - } + $new_var_type = Type::parseString($assertion); - if ($new_var_type) { - $intersection = Type::intersectUnionTypes( - $new_var_type, - $existing_var_type, - $statements_analyzer->getCodebase() - ); + $intersection = Type::intersectUnionTypes( + $new_var_type, + $existing_var_type, + $statements_analyzer->getCodebase() + ); - if ($intersection === null) { - if ($key && $code_location) { - self::triggerIssueForImpossible( - $existing_var_type, - $existing_var_type->getId(), - $key, - '!' . $assertion, - true, - $negated, - $code_location, - $suppressed_issues - ); - } - - $failed_reconciliation = Reconciler::RECONCILIATION_EMPTY; + if ($intersection === null) { + if ($key && $code_location) { + self::triggerIssueForImpossible( + $existing_var_type, + $existing_var_type->getId(), + $key, + '!' . $assertion, + true, + $negated, + $code_location, + $suppressed_issues + ); } + + $failed_reconciliation = Reconciler::RECONCILIATION_EMPTY; } return $existing_var_type; diff --git a/src/Psalm/Internal/Type/SimpleAssertionReconciler.php b/src/Psalm/Internal/Type/SimpleAssertionReconciler.php index 9050e4e25..382cd9072 100644 --- a/src/Psalm/Internal/Type/SimpleAssertionReconciler.php +++ b/src/Psalm/Internal/Type/SimpleAssertionReconciler.php @@ -4,7 +4,6 @@ namespace Psalm\Internal\Type; use Psalm\CodeLocation; use Psalm\Codebase; -use Psalm\Exception\TypeParseTreeException; use Psalm\Internal\Codebase\ClassConstantByWildcardResolver; use Psalm\Internal\Codebase\InternalCallMapHandler; use Psalm\Internal\Type\TypeCombiner; @@ -1563,14 +1562,7 @@ class SimpleAssertionReconciler extends Reconciler array $suppressed_issues, int &$failed_reconciliation ): Union { - try { - $new_var_type = Type::parseString($assertion); - } catch (TypeParseTreeException $e) { - // Not all assertions can be parsed as type, it's fine. - // One particular case is variable array key (e. g. $arr[$key]), which end up as in-array-$arr assertion - - return $existing_var_type; - } + $new_var_type = Type::parseString($assertion); $intersection = Type::intersectUnionTypes($new_var_type, $existing_var_type, $codebase); diff --git a/src/Psalm/Type/Reconciler.php b/src/Psalm/Type/Reconciler.php index 7fb517622..48e2e0b48 100644 --- a/src/Psalm/Type/Reconciler.php +++ b/src/Psalm/Type/Reconciler.php @@ -415,12 +415,6 @@ class Reconciler && $key_parts[2][0] !== '\'' && !is_numeric($key_parts[2]) ) { - if (isset($new_types[$key_parts[2]])) { - $new_types[$key_parts[2]][] = ['=in-array-' . $key_parts[0]]; - } else { - $new_types[$key_parts[2]] = [['=in-array-' . $key_parts[0]]]; - } - if ($key_parts[0][0] === '$') { if (isset($new_types[$key_parts[0]])) { $new_types[$key_parts[0]][] = ['=has-array-key-' . $key_parts[2]];