diff --git a/src/Psalm/Internal/Analyzer/ClassLikeAnalyzer.php b/src/Psalm/Internal/Analyzer/ClassLikeAnalyzer.php index 27f982cef..7c56bb95c 100644 --- a/src/Psalm/Internal/Analyzer/ClassLikeAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/ClassLikeAnalyzer.php @@ -596,10 +596,6 @@ abstract class ClassLikeAnalyzer extends SourceAnalyzer return $emit_issues ? null : true; case self::VISIBILITY_PROTECTED: - if ($appearing_property_class === $context->self) { - return null; - } - if (!$context->self) { if ($emit_issues && IssueBuffer::accepts( new InaccessibleProperty( diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php b/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php index cc890ba45..2e80854f6 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php @@ -437,6 +437,40 @@ class AssertionFinder )) { // fall through } + } else { + // both side of the Identical can be asserted to the intersection of both + $intersection_type = Type::intersectUnionTypes($var_type, $other_type, $codebase); + + if ($intersection_type !== null && $intersection_type->isSingle()) { + $assertion = $intersection_type->getAssertionString(); + + $if_types = []; + + $var_name_left = ExpressionIdentifier::getArrayVarId( + $conditional->left, + $this_class_name, + $source + ); + + if ($var_name_left && + (!$var_type->isSingle() || $var_type->getAssertionString() !== $assertion)) { + $if_types[$var_name_left] = [['~'.$assertion]]; + } + + $var_name_right = ExpressionIdentifier::getArrayVarId( + $conditional->right, + $this_class_name, + $source + ); + + if ($var_name_right && + (!$other_type->isSingle() || $other_type->getAssertionString() !== $assertion)) { + $if_types[$var_name_right] = [['~'.$assertion]]; + } + + return $if_types ? [$if_types] : []; + } + } } diff --git a/src/Psalm/Type.php b/src/Psalm/Type.php index f9c35a8ea..2ea91d96a 100644 --- a/src/Psalm/Type.php +++ b/src/Psalm/Type.php @@ -1,6 +1,7 @@ initialized && !$type_2->initialized) { $combined_type->initialized = false; } diff --git a/tests/TypeReconciliation/TypeAlgebraTest.php b/tests/TypeReconciliation/TypeAlgebraTest.php index cc61399a1..d1c67221c 100644 --- a/tests/TypeReconciliation/TypeAlgebraTest.php +++ b/tests/TypeReconciliation/TypeAlgebraTest.php @@ -1093,6 +1093,15 @@ class TypeAlgebraTest extends \Psalm\Tests\TestCase [], '8.1', ], + 'narrowedTypeAfterIdenticalCheckWithOtherType' => [ + '