Expression   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 45.45%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 85
ccs 10
cts 22
cp 0.4545
rs 10
c 0
b 0
f 0
wmc 9

8 Methods

Rating   Name   Duplication   Size   Complexity  
A createFromTerm() 0 3 1
A setTerms() 0 3 1
A setConstant() 0 3 1
A isConstant() 0 3 1
A getValue() 0 7 2
A getTerms() 0 3 1
A getConstant() 0 3 1
A __construct() 0 4 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Ctefan\Kiwi;
5
6
/**
7
 * An expression of terms and a constant.
8
 */
9
class Expression
10
{
11
    /**
12
     * @var array|Term[]
13
     */
14
    protected $terms;
15
16
    /**
17
     * @var float
18
     */
19
    protected $constant;
20
21
    /**
22
     * Expression constructor.
23
     *
24
     * @param array $terms
25
     * @param float $constant
26
     */
27 21
    public function __construct(array $terms = [], float $constant = 0.0)
28
    {
29 21
        $this->terms = $terms;
30 21
        $this->constant = $constant;
31 21
    }
32
33
    /**
34
     * Create a new Expression from a Term.
35
     *
36
     * @param Term $term
37
     * @return Expression
38
     */
39 20
    static public function createFromTerm(Term $term): self
40
    {
41 20
        return new self([$term], 0.0);
42
    }
43
44
    /**
45
     * @return float
46
     */
47
    public function getValue(): float
48
    {
49
        $value = $this->constant;
50
        foreach ($this->terms as $term) {
51
            $value += $term->getValue();
52
        }
53
        return $value;
54
    }
55
56
    /**
57
     * @return bool
58
     */
59
    public function isConstant(): bool
60
    {
61
        return 0 === count($this->terms);
62
    }
63
64
    /**
65
     * @return array|Term[]
66
     */
67 21
    public function getTerms(): array
68
    {
69 21
        return $this->terms;
70
    }
71
72
    /**
73
     * @param array $terms
74
     */
75
    public function setTerms(array $terms): void
76
    {
77
        $this->terms = $terms;
78
    }
79
80
    /**
81
     * @return float
82
     */
83 21
    public function getConstant(): float
84
    {
85 21
        return $this->constant;
86
    }
87
88
    /**
89
     * @param float $constant
90
     */
91
    public function setConstant(float $constant): void
92
    {
93
        $this->constant = $constant;
94
    }
95
}