Passed
Push — master ( d9f8e7...9b6b4f )
by Pierrick
07:21
created

OperationNode::setParent()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 4
nop 1
dl 0
loc 7
ccs 6
cts 6
cp 1
crap 3
rs 10
c 0
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
namespace Nuglif\Nacl;
15
16
class OperationNode extends Node
17
{
18
    const ADD          = '+';
19
    const SUB          = '-';
20
    const OR_OPERATOR  = '|';
21
    const AND_OPERATOR = '&';
22
    const SHIFT_LEFT   = '<<';
23
    const SHIFT_RIGHT  = '>>';
24
    const MOD          = '%';
25
    const DIV          = '/';
26
    const MUL          = '*';
27
    const POW          = '**';
28
    const CONCAT       = '.';
29
30
    private $left;
31
    private $right;
32
    private $operand;
33
34 91
    public function __construct($left, $right, $operand)
35
    {
36 91
        $this->left    = $left;
37 91
        $this->right   = $right;
38 91
        $this->operand = $operand;
39 91
    }
40
41 7
    public function setParent(Node $parent)
42
    {
43 7
        if ($this->left instanceof Node) {
44 6
            $this->left->setParent($parent);
45 6
        }
46 7
        if ($this->right instanceof Node) {
47 3
            $this->right->setParent($parent);
48 3
        }
49 7
    }
50
51 91
    public function getNativeValue()
52
    {
53 91
        $left  = $this->left instanceof Node ? $this->left->getNativeValue() : $this->left;
54 91
        $right = $this->right instanceof Node ? $this->right->getNativeValue() : $this->right;
55
56 91
        switch ($this->operand) {
57 91
            case self::ADD:
58 2
                return $left + $right;
59 90
            case self::OR_OPERATOR:
60 1
                return $left | $right;
61 90
            case self::AND_OPERATOR:
62 1
                return $left & $right;
63 90
            case self::SHIFT_LEFT:
64 1
                return $left << $right;
65 90
            case self::SHIFT_RIGHT:
66 1
                return $left >> $right;
67 90
            case self::SUB:
68 2
                return $left - $right;
69 89
            case self::MUL:
70 1
                return $left * $right;
71 89
            case self::DIV:
72 1
                return $left / $right;
73 89
            case self::MOD:
74 1
                return $left % $right;
75 89
            case self::POW:
76 1
                return $left ** $right;
77 88
            case self::CONCAT:
78 88
                return $left . $right;
79
        }
80
    }
81
}
82