Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #10552 #10572

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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);
Expand Down
21 changes: 21 additions & 0 deletions tests/MethodCallTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1230,6 +1230,27 @@ public function bar(&$object): void {}
$x = new Foo();
$x->bar($x);',
],
'conditional' => [
'code' => '<?php

class Old {
public function x(): void {}
}
class Child1 extends Old {}
class Child2 extends Old {}

/**
* @template IsClient of bool
*/
class A {
/**
* @psalm-param (IsClient is true ? Child1 : Child2) $var
*/
public function test(Old $var): void {
$var->x();
}
}',
],
];
}

Expand Down
Loading