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

OperationNode::getNativeValue()   C

Complexity

Conditions 14
Paths 48

Size

Total Lines 28
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 14

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 14
eloc 25
nc 48
nop 0
dl 0
loc 28
ccs 26
cts 26
cp 1
crap 14
rs 6.2666
c 1
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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