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

refactor arithmetics #6240

Merged
merged 2 commits into from
Aug 4, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -317,45 +317,12 @@ private static function analyzeNonDivOperands(
|| $right instanceof PhpParser\Node\Expr\BinaryOp)
) {
// time for some arithmetic!
$calculated_type = null;

if ($parent instanceof PhpParser\Node\Expr\BinaryOp\Plus) {
$result = $left_type_part->value + $right_type_part->value;
$calculated_type = self::getNumericalType($result);
} elseif ($parent instanceof PhpParser\Node\Expr\BinaryOp\Minus) {
$result = $left_type_part->value - $right_type_part->value;
$calculated_type = self::getNumericalType($result);
} elseif ($parent instanceof PhpParser\Node\Expr\BinaryOp\Mod) {
$calculated_type = Type::getInt(false, $left_type_part->value % $right_type_part->value);
} elseif ($parent instanceof PhpParser\Node\Expr\BinaryOp\Mul) {
$result = $left_type_part->value * $right_type_part->value;
$calculated_type = self::getNumericalType($result);
} elseif ($parent instanceof PhpParser\Node\Expr\BinaryOp\Pow) {
$result = $left_type_part->value ** $right_type_part->value;
$calculated_type = self::getNumericalType($result);
} elseif ($parent instanceof PhpParser\Node\Expr\BinaryOp\BitwiseOr) {
$calculated_type = Type::getInt(false, $left_type_part->value | $right_type_part->value);
} elseif ($parent instanceof PhpParser\Node\Expr\BinaryOp\BitwiseAnd) {
$calculated_type = Type::getInt(false, $left_type_part->value & $right_type_part->value);
} elseif ($parent instanceof PhpParser\Node\Expr\BinaryOp\BitwiseXor) {
$calculated_type = Type::getInt(false, $left_type_part->value ^ $right_type_part->value);
} elseif ($parent instanceof PhpParser\Node\Expr\BinaryOp\ShiftLeft) {
$calculated_type = Type::getInt(false, $left_type_part->value << $right_type_part->value);
} elseif ($parent instanceof PhpParser\Node\Expr\BinaryOp\ShiftRight) {
$calculated_type = Type::getInt(false, $left_type_part->value >> $right_type_part->value);
} elseif ($parent instanceof PhpParser\Node\Expr\BinaryOp\Div) {
if ($right_type_part->value === 0) {
$calculated_type = Type::getEmpty();
} else {
$value = $left_type_part->value / $right_type_part->value;

if (is_int($value)) {
$calculated_type = Type::getInt(false, $value);
} else {
$calculated_type = Type::getFloat($value);
}
}
}
$calculated_type = self::arithmeticOperation(
$parent,
$left_type_part->value,
$right_type_part->value,
true
);

if ($calculated_type) {
if ($result_type) {
Expand Down Expand Up @@ -849,4 +816,53 @@ private static function analyzeNonDivOperands(

return null;
}

/**
* @param PhpParser\Node $operation
* @param float|int $operand1
* @param float|int $operand2
*/
public static function arithmeticOperation(
PhpParser\Node $operation,
$operand1,
$operand2,
bool $allow_float_result
): ?Type\Union {
if ($operation instanceof PhpParser\Node\Expr\BinaryOp\Plus) {
$result = $operand1 + $operand2;
} elseif ($operation instanceof PhpParser\Node\Expr\BinaryOp\Minus) {
$result = $operand1 - $operand2;
} elseif ($operation instanceof PhpParser\Node\Expr\BinaryOp\Mod) {
$result = $operand1 % $operand2;
} elseif ($operation instanceof PhpParser\Node\Expr\BinaryOp\Mul) {
$result = $operand1 * $operand2;
} elseif ($operation instanceof PhpParser\Node\Expr\BinaryOp\Pow) {
$result = $operand1 ** $operand2;
} elseif ($operation instanceof PhpParser\Node\Expr\BinaryOp\BitwiseOr) {
$result = $operand1 | $operand2;
} elseif ($operation instanceof PhpParser\Node\Expr\BinaryOp\BitwiseAnd) {
$result = $operand1 & $operand2;
} elseif ($operation instanceof PhpParser\Node\Expr\BinaryOp\BitwiseXor) {
$result = $operand1 ^ $operand2;
} elseif ($operation instanceof PhpParser\Node\Expr\BinaryOp\ShiftLeft) {
$result = $operand1 << $operand2;
} elseif ($operation instanceof PhpParser\Node\Expr\BinaryOp\ShiftRight) {
$result = $operand1 >> $operand2;
} elseif ($operation instanceof PhpParser\Node\Expr\BinaryOp\Div) {
if ($operand2 === 0) {
return Type::getEmpty();
}

$result = $operand1 / $operand2;
} else {
return null;
}

$calculated_type = self::getNumericalType($result);
if (!$allow_float_result && $calculated_type->isFloat()) {
return null;
}

return $calculated_type;
}
}