Skip to content

Commit

Permalink
Merge pull request #10572 from vimeo/fix_10552
Browse files Browse the repository at this point in the history
Fix #10552
  • Loading branch information
danog committed Jan 18, 2024
2 parents 3f284e9 + f7edaa6 commit b7a18bd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
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
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

0 comments on commit b7a18bd

Please sign in to comment.