Skip to content

Commit

Permalink
Merge pull request #9739 from klimick/match-paradoxical-condition-wit…
Browse files Browse the repository at this point in the history
…h-complex-expr

Fix ParadoxicalCondition with complex match expression
  • Loading branch information
orklah committed May 4, 2023
2 parents e809fb3 + a18eb44 commit 32bd8f6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,16 @@ public static function analyze(
$stmt->cond->getAttributes(),
);
}
} elseif ($stmt->cond instanceof PhpParser\Node\Expr\FuncCall
|| $stmt->cond instanceof PhpParser\Node\Expr\MethodCall
|| $stmt->cond instanceof PhpParser\Node\Expr\StaticCall
} elseif ($stmt->cond instanceof PhpParser\Node\Expr\ClassConstFetch
&& $stmt->cond->name instanceof PhpParser\Node\Identifier
&& $stmt->cond->name->toString() === 'class'
) {
// do nothing
} elseif ($stmt->cond instanceof PhpParser\Node\Expr\ConstFetch
&& $stmt->cond->name->toString() === 'true'
) {
// do nothing
} else {
$switch_var_id = '$__tmp_switch__' . (int) $stmt->cond->getAttribute('startFilePos');

$condition_type = $statements_analyzer->node_data->getType($stmt->cond) ?? Type::getMixed();
Expand Down
34 changes: 34 additions & 0 deletions tests/MatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,20 @@ function test(array $array): array
'ignored_issues' => [],
'php_version' => '8.1',
],
'multipleIdenticalChecksInOneArm' => [
'code' => '<?php
function foo(?string $t1, string $t2): string
{
return match ($t1 ?? $t2) {
"type1", "type2", "type3" => "1 or 2 or 3",
"type4", "type5", "type6" => "4 or 5 or 6",
default => "rest",
};
}',
'assertions' => [],
'ignored_issues' => [],
'php_version' => '8.1',
],
'multipleInstanceOfConditionsInOneArm' => [
'code' => '<?php
interface Foo {}
Expand Down Expand Up @@ -133,6 +147,26 @@ function bar(mixed $foo): int {
'ignored_issues' => [],
'php_version' => '8.0',
],
'matchOnConstClassFetch' => [
'code' => '<?php
final class Obj1 {
public string $propFromObj1 = "str";
}
final class Obj2 {
public int $propFromObj2 = 42;
}
function process(Obj1|Obj2 $obj): int|string
{
return match ($obj::class) {
Obj1::class => $obj->propFromObj1,
Obj2::class => $obj->propFromObj2,
};
}',
'assertions' => [],
'ignored_issues' => [],
'php_version' => '8.0',
],
];
}

Expand Down

0 comments on commit 32bd8f6

Please sign in to comment.