Skip to content

Commit 106526d

Browse files
committed
Fix get_class in match expression
1 parent e4ef00c commit 106526d

File tree

4 files changed

+70
-13
lines changed

4 files changed

+70
-13
lines changed

src/Analyser/TypeSpecifier.php

+19-9
Original file line numberDiff line numberDiff line change
@@ -334,15 +334,7 @@ public function specifyTypesInCondition(
334334
&& isset($exprNode->getArgs()[0])
335335
&& $constantType instanceof ConstantStringType
336336
) {
337-
return $this->specifyTypesInCondition(
338-
$scope,
339-
new Instanceof_(
340-
$exprNode->getArgs()[0]->value,
341-
new Name($constantType->getValue()),
342-
),
343-
$context,
344-
$rootExpr,
345-
);
337+
return $this->specifyTypesInCondition($scope, new Expr\BinaryOp\Identical($expr->left, $expr->right), $context, $rootExpr);
346338
}
347339
}
348340

@@ -1118,6 +1110,24 @@ private function specifyTypesForConstantStringBinaryExpression(
11181110
}
11191111
}
11201112

1113+
if (
1114+
$context->true()
1115+
&& $unwrappedExprNode instanceof FuncCall
1116+
&& $unwrappedExprNode->name instanceof Name
1117+
&& strtolower($unwrappedExprNode->name->toString()) === 'get_class'
1118+
&& isset($unwrappedExprNode->getArgs()[0])
1119+
) {
1120+
return $this->specifyTypesInCondition(
1121+
$scope,
1122+
new Instanceof_(
1123+
$unwrappedExprNode->getArgs()[0]->value,
1124+
new Name($constantType->getValue()),
1125+
),
1126+
$context,
1127+
$rootExpr,
1128+
)->unionWith($this->create($exprNode, $constantType, $context, false, $scope, $rootExpr));
1129+
}
1130+
11211131
if (
11221132
$context->true()
11231133
&& $exprNode instanceof FuncCall

tests/PHPStan/Analyser/TypeSpecifierTest.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,8 @@ public function dataCondition(): array
193193
]),
194194
new String_('Foo'),
195195
),
196-
['$foo' => 'Foo'],
197-
['$foo' => '~Foo'],
196+
['$foo' => 'Foo', 'get_class($foo)' => '\'Foo\''],
197+
['get_class($foo)' => '~\'Foo\''],
198198
],
199199
[
200200
new Equal(
@@ -203,8 +203,8 @@ public function dataCondition(): array
203203
new Arg(new Variable('foo')),
204204
]),
205205
),
206-
['$foo' => 'Foo'],
207-
['$foo' => '~Foo'],
206+
['$foo' => 'Foo', 'get_class($foo)' => '\'Foo\''],
207+
['get_class($foo)' => '~\'Foo\''],
208208
],
209209
[
210210
new BooleanNot(

tests/PHPStan/Analyser/data/match-expr.php

+24
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace MatchExpr;
44

5+
use function get_class;
56
use function PHPStan\Testing\assertType;
67

78
class Foo
@@ -77,3 +78,26 @@ public function doGettype(int|float|bool|string|object|array $value): void
7778
}
7879

7980
}
81+
82+
final class FinalFoo
83+
{
84+
85+
}
86+
87+
final class FinalBar
88+
{
89+
90+
}
91+
92+
class TestGetClass
93+
{
94+
95+
public function doMatch(FinalFoo|FinalBar $class): void
96+
{
97+
match (get_class($class)) {
98+
FinalFoo::class => assertType(FinalFoo::class, $class),
99+
FinalBar::class => assertType(FinalBar::class, $class),
100+
};
101+
}
102+
103+
}

tests/PHPStan/Rules/Comparison/data/match-expr.php

+23
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,26 @@ function (): string {
180180
-1 => 'down',
181181
};
182182
};
183+
184+
final class FinalFoo
185+
{
186+
187+
}
188+
189+
final class FinalBar
190+
{
191+
192+
}
193+
194+
class TestGetClass
195+
{
196+
197+
public function doMatch(FinalFoo|FinalBar $class): void
198+
{
199+
match (get_class($class)) {
200+
FinalFoo::class => 1,
201+
FinalBar::class => 2,
202+
};
203+
}
204+
205+
}

0 commit comments

Comments
 (0)