Total Complexity | 9 |
Total Lines | 85 |
Duplicated Lines | 0 % |
Coverage | 45.45% |
Changes | 0 |
1 | <?php |
||
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 |
|
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 |
||
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 |
||
94 | } |
||
95 | } |