From f7edaa66acbb0f6ac2861ad95251b0019e603746 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Thu, 18 Jan 2024 20:33:54 +0100 Subject: [PATCH] Fix #10552 --- .../Expression/Call/MethodCallAnalyzer.php | 15 ++++++++++++- tests/MethodCallTest.php | 21 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/MethodCallAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/MethodCallAnalyzer.php index ab4f1f0428b..ceff9788724 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/MethodCallAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/MethodCallAnalyzer.php @@ -28,11 +28,13 @@ use Psalm\Issue\UndefinedMethod; use Psalm\IssueBuffer; use Psalm\Type; +use Psalm\Type\Atomic\TConditional; use Psalm\Type\Atomic\TNamedObject; use Psalm\Type\Atomic\TObject; use Psalm\Type\Atomic\TTemplateParam; use Psalm\Type\Union; +use function array_merge; use function array_reduce; use function count; use function is_string; @@ -175,6 +177,17 @@ public static function analyze( $lhs_types = $class_type->getAtomicTypes(); + foreach ($lhs_types as $k => $lhs_type_part) { + if ($lhs_type_part instanceof TConditional) { + $lhs_types = array_merge( + $lhs_types, + $lhs_type_part->if_type->getAtomicTypes(), + $lhs_type_part->else_type->getAtomicTypes(), + ); + unset($lhs_types[$k]); + } + } + $result = new AtomicMethodCallAnalysisResult(); $possible_new_class_types = []; @@ -398,7 +411,7 @@ public static function analyze( $types = $class_type->getAtomicTypes(); foreach ($types as $key => &$type) { - if (!$type instanceof TNamedObject && !$type instanceof TObject) { + if (!$type instanceof TNamedObject && !$type instanceof TObject && !$type instanceof TConditional) { unset($types[$key]); } else { $type = $type->setFromDocblock(false); diff --git a/tests/MethodCallTest.php b/tests/MethodCallTest.php index 4c411170272..fa56564e790 100644 --- a/tests/MethodCallTest.php +++ b/tests/MethodCallTest.php @@ -1230,6 +1230,27 @@ public function bar(&$object): void {} $x = new Foo(); $x->bar($x);', ], + 'conditional' => [ + 'code' => 'x(); + } + }', + ], ]; }