Skip to content

Commit

Permalink
Fix gettype in match expression
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jun 19, 2023
1 parent b4a6eb4 commit e4ef00c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,11 @@ private function specifyTypesForConstantStringBinaryExpression(
?Expr $rootExpr,
): ?SpecifiedTypes
{
$unwrappedExprNode = $exprNode;
if ($exprNode instanceof AlwaysRememberedExpr) {
$unwrappedExprNode = $exprNode->getExpr();
}

if (
$context->truthy()
&& $exprNode instanceof FuncCall
Expand Down Expand Up @@ -1075,10 +1080,10 @@ private function specifyTypesForConstantStringBinaryExpression(
}

if (
$exprNode instanceof FuncCall
&& $exprNode->name instanceof Name
&& strtolower($exprNode->name->toString()) === 'gettype'
&& isset($exprNode->getArgs()[0])
$unwrappedExprNode instanceof FuncCall
&& $unwrappedExprNode->name instanceof Name
&& strtolower($unwrappedExprNode->name->toString()) === 'gettype'
&& isset($unwrappedExprNode->getArgs()[0])
) {
$type = null;
if ($constantType->getValue() === 'string') {
Expand Down Expand Up @@ -1107,8 +1112,8 @@ private function specifyTypesForConstantStringBinaryExpression(
}

if ($type !== null) {
$callType = $this->create($exprNode, $constantType, $context, false, $scope, $rootExpr);
$argType = $this->create($exprNode->getArgs()[0]->value, $type, $context, false, $scope, $rootExpr);
$callType = $this->create($unwrappedExprNode, $constantType, $context, false, $scope, $rootExpr);
$argType = $this->create($unwrappedExprNode->getArgs()[0]->value, $type, $context, false, $scope, $rootExpr);
return $callType->unionWith($argType);
}
}
Expand Down
8 changes: 8 additions & 0 deletions tests/PHPStan/Analyser/data/match-expr.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,12 @@ public function doBar(int $i): void
};
}

public function doGettype(int|float|bool|string|object|array $value): void
{
match (gettype($value)) {
'integer' => assertType('int', $value),
'string' => assertType('string', $value),
};
}

}

0 comments on commit e4ef00c

Please sign in to comment.