Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cheap checks first in TypeSpecifier #2103

Merged
merged 2 commits into from Dec 13, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/Analyser/TypeSpecifier.php
Expand Up @@ -231,9 +231,9 @@ public function specifyTypesInCondition(
$exprLeftType = $scope->getType($expr->left);
$exprRightType = $scope->getType($expr->right);
if (
($exprLeftType instanceof ConstantType && !$exprRightType->equals($exprLeftType) && $exprRightType->isSuperTypeOf($exprLeftType)->yes())
|| $exprLeftType instanceof ConstantScalarType
$exprLeftType instanceof ConstantScalarType
|| $exprLeftType instanceof EnumCaseObjectType
|| ($exprLeftType instanceof ConstantType && !$exprRightType->equals($exprLeftType) && $exprRightType->isSuperTypeOf($exprLeftType)->yes())
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just moving possible expensive isSuperTypeOf to be evaluated later

) {
$types = $this->create(
$expr->right,
Expand All @@ -245,9 +245,9 @@ public function specifyTypesInCondition(
);
}
if (
($exprRightType instanceof ConstantType && !$exprLeftType->equals($exprRightType) && $exprLeftType->isSuperTypeOf($exprRightType)->yes())
|| $exprRightType instanceof ConstantScalarType
$exprRightType instanceof ConstantScalarType
|| $exprRightType instanceof EnumCaseObjectType
|| ($exprRightType instanceof ConstantType && !$exprLeftType->equals($exprRightType) && $exprLeftType->isSuperTypeOf($exprRightType)->yes())
) {
$leftType = $this->create(
$expr->left,
Expand Down Expand Up @@ -424,10 +424,6 @@ public function specifyTypesInCondition(
);

} elseif ($expr instanceof Node\Expr\BinaryOp\Smaller || $expr instanceof Node\Expr\BinaryOp\SmallerOrEqual) {
$orEqual = $expr instanceof Node\Expr\BinaryOp\SmallerOrEqual;
$offset = $orEqual ? 0 : 1;
$leftType = $scope->getType($expr->left);
$rightType = $scope->getType($expr->right);

if (
$expr->left instanceof FuncCall
Expand All @@ -452,6 +448,9 @@ public function specifyTypesInCondition(
);
}

$orEqual = $expr instanceof Node\Expr\BinaryOp\SmallerOrEqual;
$offset = $orEqual ? 0 : 1;
$leftType = $scope->getType($expr->left);
$result = new SpecifiedTypes([], [], false, [], $rootExpr);

if (
Expand Down Expand Up @@ -522,6 +521,7 @@ public function specifyTypesInCondition(
}
}

$rightType = $scope->getType($expr->right);
if ($rightType instanceof ConstantIntegerType) {
if ($expr->left instanceof Expr\PostInc) {
$result = $result->unionWith($this->createRangeTypes(
Expand Down