j84reginato/my-eval

View on GitHub
src/Parsing/Operations/Math/MultiplicationOperation.php

Summary

Maintainability
A
1 hr
Test Coverage
<?php

declare(strict_types=1);

namespace MyEval\Parsing\Operations\Math;

use MyEval\Exceptions\DivisionByZeroException;
use MyEval\Exceptions\UnknownOperatorException;
use MyEval\Parsing\Nodes\Node;
use MyEval\Parsing\Nodes\Operand\FloatNode;
use MyEval\Parsing\Nodes\Operand\IntegerNode;
use MyEval\Parsing\Nodes\Operand\NumericNode;
use MyEval\Parsing\Nodes\Operand\RationalNode;
use MyEval\Parsing\Nodes\Operator\InfixExpressionNode;
use MyEval\Parsing\Traits\Numeric;
use MyEval\Parsing\Traits\Sanitize;

/**
 * Factory for creating an ExpressionNode representing '*'.
 *
 * Some basic simplification is applied to the resulting Node.
 */
class MultiplicationOperation implements MathOperationInterface
{
    use Sanitize;
    use Numeric;

    /**
     * Create a Node representing 'leftOperand * rightOperand'
     *
     * Using some simplification rules, create a NumericNode (IntegerNode, RationalNode or FloatNode) or an
     * InfixExpressionNode giving an AST correctly representing 'leftOperand * rightOperand'.
     *
     * ## Simplification rules:
     *
     * - If $leftOperand and $rightOperand are both NumericNode's, return a single NumericNode containing their product;
     * - If $leftOperand or $rightOperand is a NumericNode representing 0, return '0';
     * - If $leftOperand or $rightOperand is a NumericNode representing 1, return the other factor.
     *
     * @param Node $leftOperand  First term
     * @param Node $rightOperand Second term
     *
     * @return Node
     * @throws DivisionByZeroException
     * @throws UnknownOperatorException
     */
    public function makeNode(Node $leftOperand, Node $rightOperand): Node
    {
        return $this->numericFactors($leftOperand, $rightOperand)
            ?? new InfixExpressionNode('*', $leftOperand, $rightOperand);
    }

    /**
     * Simplify a*b when a or b are certain numeric values.
     *
     * @param Node $leftOperand
     * @param Node $rightOperand
     *
     * @return Node|null
     * @throws DivisionByZeroException
     */
    private function numericFactors(Node $leftOperand, Node $rightOperand): ?Node
    {
        if (
            ($this->isNumeric($leftOperand) && (float)$leftOperand->value === 0.0) ||
            ($this->isNumeric($rightOperand) && (float)$rightOperand->value === 0.0)
        ) {
            return new IntegerNode(0);
        }

        if ($this->isNumeric($leftOperand) && (float)$leftOperand->value === 1.0) {
            return $rightOperand;
        }

        if ($this->isNumeric($rightOperand) && (float)$rightOperand->value === 1.0) {
            return $leftOperand;
        }

        if (!$this->isNumeric($leftOperand) || !$this->isNumeric($rightOperand)) {
            return null;
        }

        return $this->processNumericNodeType($leftOperand, $rightOperand);
    }

    /**
     * Simplify multiplication node when operands are numeric accordin type.
     *
     * @throws DivisionByZeroException
     */
    private function processNumericNodeType(Node $leftOperand, Node $rightOperand): ?NumericNode
    {
        $type = $this->resultingType($leftOperand, $rightOperand);

        switch ($type) {
            case Node::NUMERIC_INTEGER:
                $node = new IntegerNode($leftOperand->value * $rightOperand->value);
                break;

            case Node::NUMERIC_RATIONAL:
                $p    = $leftOperand->getNumerator() * $rightOperand->getNumerator();
                $q    = $leftOperand->getDenominator() * $rightOperand->getDenominator();
                $node = new RationalNode($p, $q);
                break;

            case Node::NUMERIC_FLOAT:
            default:
                $node = new FloatNode(($leftOperand->value * $rightOperand->value));
                break;
        }

        return $node;
    }
}