From ddc8c50d2aad5965749841c395e09e82786a6623 Mon Sep 17 00:00:00 2001 From: Brown Date: Tue, 31 Mar 2020 11:28:36 -0400 Subject: [PATCH] Prevent some inheritance issues --- .../Internal/Type/AssertionReconciler.php | 25 ++++++++++++++ .../RedundantConditionTest.php | 33 +++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/src/Psalm/Internal/Type/AssertionReconciler.php b/src/Psalm/Internal/Type/AssertionReconciler.php index 3b341c2a2..4387bd020 100644 --- a/src/Psalm/Internal/Type/AssertionReconciler.php +++ b/src/Psalm/Internal/Type/AssertionReconciler.php @@ -783,6 +783,31 @@ class AssertionReconciler extends \Psalm\Type\Reconciler $any_scalar_type_match_found = false; + if ($code_location + && $key + && !$is_equality + && $new_type_part instanceof TNamedObject + && !$new_type_has_interface + && (!($statements_analyzer->getSource()->getSource() instanceof TraitAnalyzer) + || ($key !== '$this' + && !($existing_var_type->hasLiteralClassString() && $new_type->hasLiteralClassString()))) + && TypeAnalyzer::isContainedBy( + $codebase, + $existing_var_type, + $new_type + ) + ) { + self::triggerIssueForImpossible( + $existing_var_type, + $old_var_type_string, + $key, + $assertion, + true, + $code_location, + $suppressed_issues + ); + } + $new_type = self::filterTypeWithAnother( $codebase, $existing_var_type, diff --git a/tests/TypeReconciliation/RedundantConditionTest.php b/tests/TypeReconciliation/RedundantConditionTest.php index cf1dafdad..b1bf00916 100644 --- a/tests/TypeReconciliation/RedundantConditionTest.php +++ b/tests/TypeReconciliation/RedundantConditionTest.php @@ -1307,6 +1307,39 @@ class RedundantConditionTest extends \Psalm\Tests\TestCase if (!is_int(returnsInt())) {}', 'error_message' => 'TypeDoesNotContainType', ], + 'classAlwaysParent' => [ + ' 'RedundantCondition', + ], + 'staticClassIsAlwaysNull' => [ + ' 'RedundantConditionGivenDocblockType', + ], ]; } }