Completed
Push — master ( 5e182e...aa1b46 )
by Tom
14:48
created

ValueResult   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 20
c 2
b 0
f 0
lcom 1
cbo 0
dl 0
loc 89
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
A arg() 0 3 1
A not() 0 3 1
A setMode() 0 3 1
A getMode() 0 3 1
A getResult() 0 3 1
A pop() 0 3 1
A processValue() 0 17 4
A concat() 0 3 1
A equals() 0 3 1
A add() 0 3 1
A sub() 0 3 1
A mult() 0 3 1
A div() 0 3 1
A write() 0 5 3
A clear() 0 3 1
1
<?php
2
/* @description     Transformation Style Sheets - Revolutionising PHP templating    *
3
 * @author          Tom Butler [email protected]                                             *
4
 * @copyright       2015 Tom Butler <[email protected]> | https://r.je/                      *
5
 * @license         http://www.opensource.org/licenses/bsd-license.php  BSD License *
6
 * @version         1.0                                                             */
7
namespace Transphporm\Parser;
8
class ValueResult {
9
	private $result = [];
10
	/*
11
		The next operation to perform. Will be one of the following:
12
			ARG - A new value e.g,  "a","b"  becomes ["a", "b"]
13
			CONCAT - Concat onto the current arg e.g "a" + "b" becomes ["ab"]
14
			NOT - Boolean operation "a" != "b" becomes [true]
15
			EQUALS - Boolean operation "a" = "b" becomes [false]
16
	*/
17
	private $mode = Tokenizer::ARG;
18
19
	//Processes $newValue using $mode. Either concats to the current argument, adds a new argument
20
	//Or usess the two arguments for a boolean comparison
21
	public function processValue($newValue) {
22
		$funcs = [
23
			Tokenizer::ARG => 'arg',
24
			Tokenizer::CONCAT => 'concat',
25
			Tokenizer::EQUALS => 'equals',
26
			Tokenizer::NOT => 'not',
27
			Tokenizer::SUBTRACT => 'sub',
28
			Tokenizer::MULTIPLY => 'mult',
29
			Tokenizer::DIVIDE => 'div'
30
		];
31
32
		if ($funcs[$this->mode] === 'concat' && is_numeric($newValue)
33
			&& is_numeric($this->result[count($this->result)-1]))
34
			$this->add($newValue);
35
		else
36
			$this->{$funcs[$this->mode]}($newValue);
37
	}
38
39
	public function arg($value) {
40
		$this->result[] = $value;
41
	}
42
43
	public function concat($value) {
44
		$this->result[count($this->result)-1] .= $value;
45
	}
46
47
	public function not($value) {
48
		$this->result[count($this->result)-1] = $this->result[count($this->result)-1] != $value;
49
	}
50
51
	public function equals($value) {
52
		$this->result[count($this->result)-1] = $this->result[count($this->result)-1] == $value;
53
	}
54
55
	public function add($value) {
56
		$this->result[count($this->result)-1] += $value;
57
	}
58
59
	public function sub($value) {
60
		$this->result[count($this->result)-1] -= $value;
61
	}
62
63
	public function mult($value) {
64
		$this->result[count($this->result)-1] *= $value;
65
	}
66
67
	public function div($value) {
68
		$this->result[count($this->result)-1] /= $value;
69
	}
70
71
	public function setMode($mode) {
72
		$this->mode = $mode;
73
	}
74
75
	public function getMode() {
76
		return $this->mode;
77
	}
78
79
	public function getResult() {
80
		return $this->result;
81
	}
82
83
	public function pop() {
84
		return array_pop($this->result);
85
	}
86
87
	public function write($index, $value, $allowNull = false) {
88
		if ($value !== null || $allowNull == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
89
			$this->result[$index] = $value;
90
		}
91
	}
92
93
	public function clear() {
94
		$this->result = [];
95
	}
96
}
97