OperationNode::getNativeValue()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 14
nc 4
nop 0
dl 0
loc 17
ccs 15
cts 15
cp 1
crap 3
rs 9.7998
c 1
b 0
f 0
1
<?php
2
/**
3
 * This file is part of NACL.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @copyright 2019 Nuglif (2018) Inc.
9
 * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
10
 * @author    Pierrick Charron <[email protected]>
11
 * @author    Charle Demers <[email protected]>
12
 */
13
14
declare(strict_types=1);
15
16
namespace Nuglif\Nacl;
17
18
class OperationNode extends Node
19
{
20
    public const ADD          = '+';
21
    public const SUB          = '-';
22
    public const OR_OPERATOR  = '|';
23
    public const AND_OPERATOR = '&';
24
    public const SHIFT_LEFT   = '<<';
25
    public const SHIFT_RIGHT  = '>>';
26
    public const MOD          = '%';
27
    public const DIV          = '/';
28
    public const MUL          = '*';
29
    public const POW          = '**';
30
    public const CONCAT       = '.';
31
32
    private mixed $left;
33
    private mixed $right;
34
    private mixed $operand;
35
36 92
    public function __construct(mixed $left, mixed $right, string $operand)
37
    {
38 92
        $this->left    = $left;
39 92
        $this->right   = $right;
40 92
        $this->operand = $operand;
41
    }
42
43 7
    public function setParent(Node $parent): void
44
    {
45 7
        if ($this->left instanceof Node) {
46 6
            $this->left->setParent($parent);
47
        }
48 7
        if ($this->right instanceof Node) {
49 3
            $this->right->setParent($parent);
50
        }
51
    }
52
53
    /**
54
     * @psalm-suppress UnhandledMatchCondition
55
     */
56 92
    public function getNativeValue(): mixed
57
    {
58 92
        $left  = $this->left instanceof Node ? $this->left->getNativeValue() : $this->left;
59 92
        $right = $this->right instanceof Node ? $this->right->getNativeValue() : $this->right;
60
61 92
        return match ($this->operand) {
62 92
            self::ADD => $left + $right,
63 92
            self::OR_OPERATOR => $left | $right,
64 92
            self::AND_OPERATOR => $left & $right,
65 92
            self::SHIFT_LEFT => $left << $right,
66 92
            self::SHIFT_RIGHT => $left >> $right,
67 92
            self::SUB => $left - $right,
68 92
            self::MUL => $left * $right,
69 92
            self::DIV => $left / $right,
70 92
            self::MOD => $left % $right,
71 92
            self::POW => $left ** $right,
72 92
            self::CONCAT => $left . $right,
73 92
        };
74
    }
75
}
76