Skip to content

Commit e4ef00c

Browse files
committed
Fix gettype in match expression
1 parent b4a6eb4 commit e4ef00c

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

src/Analyser/TypeSpecifier.php

+11-6
Original file line numberDiff line numberDiff line change
@@ -1043,6 +1043,11 @@ private function specifyTypesForConstantStringBinaryExpression(
10431043
?Expr $rootExpr,
10441044
): ?SpecifiedTypes
10451045
{
1046+
$unwrappedExprNode = $exprNode;
1047+
if ($exprNode instanceof AlwaysRememberedExpr) {
1048+
$unwrappedExprNode = $exprNode->getExpr();
1049+
}
1050+
10461051
if (
10471052
$context->truthy()
10481053
&& $exprNode instanceof FuncCall
@@ -1075,10 +1080,10 @@ private function specifyTypesForConstantStringBinaryExpression(
10751080
}
10761081

10771082
if (
1078-
$exprNode instanceof FuncCall
1079-
&& $exprNode->name instanceof Name
1080-
&& strtolower($exprNode->name->toString()) === 'gettype'
1081-
&& isset($exprNode->getArgs()[0])
1083+
$unwrappedExprNode instanceof FuncCall
1084+
&& $unwrappedExprNode->name instanceof Name
1085+
&& strtolower($unwrappedExprNode->name->toString()) === 'gettype'
1086+
&& isset($unwrappedExprNode->getArgs()[0])
10821087
) {
10831088
$type = null;
10841089
if ($constantType->getValue() === 'string') {
@@ -1107,8 +1112,8 @@ private function specifyTypesForConstantStringBinaryExpression(
11071112
}
11081113

11091114
if ($type !== null) {
1110-
$callType = $this->create($exprNode, $constantType, $context, false, $scope, $rootExpr);
1111-
$argType = $this->create($exprNode->getArgs()[0]->value, $type, $context, false, $scope, $rootExpr);
1115+
$callType = $this->create($unwrappedExprNode, $constantType, $context, false, $scope, $rootExpr);
1116+
$argType = $this->create($unwrappedExprNode->getArgs()[0]->value, $type, $context, false, $scope, $rootExpr);
11121117
return $callType->unionWith($argType);
11131118
}
11141119
}

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

+8
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,12 @@ public function doBar(int $i): void
6868
};
6969
}
7070

71+
public function doGettype(int|float|bool|string|object|array $value): void
72+
{
73+
match (gettype($value)) {
74+
'integer' => assertType('int', $value),
75+
'string' => assertType('string', $value),
76+
};
77+
}
78+
7179
}

0 commit comments

Comments
 (0)