Skip to content

Commit a18eb44

Browse files
committed
Test match on class const fetch
1 parent ac8d489 commit a18eb44

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

src/Psalm/Internal/Analyzer/Statements/Expression/MatchAnalyzer.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ public static function analyze(
110110
$stmt->cond->getAttributes(),
111111
);
112112
}
113+
} elseif ($stmt->cond instanceof PhpParser\Node\Expr\ClassConstFetch
114+
&& $stmt->cond->name instanceof PhpParser\Node\Identifier
115+
&& $stmt->cond->name->toString() === 'class'
116+
) {
117+
// do nothing
113118
} elseif ($stmt->cond instanceof PhpParser\Node\Expr\ConstFetch
114119
&& $stmt->cond->name->toString() === 'true'
115120
) {

tests/MatchTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,26 @@ function bar(mixed $foo): int {
147147
'ignored_issues' => [],
148148
'php_version' => '8.0',
149149
],
150+
'matchOnConstClassFetch' => [
151+
'code' => '<?php
152+
final class Obj1 {
153+
public string $propFromObj1 = "str";
154+
}
155+
final class Obj2 {
156+
public int $propFromObj2 = 42;
157+
}
158+
159+
function process(Obj1|Obj2 $obj): int|string
160+
{
161+
return match ($obj::class) {
162+
Obj1::class => $obj->propFromObj1,
163+
Obj2::class => $obj->propFromObj2,
164+
};
165+
}',
166+
'assertions' => [],
167+
'ignored_issues' => [],
168+
'php_version' => '8.0',
169+
],
150170
];
151171
}
152172

0 commit comments

Comments
 (0)