Completed
Push — master ( c9bd8d...4ed61c )
by Tom
03:17
created

Value::processValue()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 16
rs 8.8571
cc 5
eloc 10
nc 5
nop 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
/** Parses "string" and function(args) e.g. data(foo) or iteration(bar) */
9
class Value {
10
	private $baseData;
11
	private $autoLookup;
12
	private $tokens;
0 ignored issues
show
Unused Code introduced by
The property $tokens is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
13
	private $mode;
14
	private $last;
15
	private $data;
16
	private $result;
17
	private $element;
18
19
	public function __construct($data, $autoLookup = false) {
20
		$this->baseData = $data;
21
		$this->autoLookup = $autoLookup;
22
	}
23
24
	public function parse($str, $element = null, $returnTokens = false) {
25
		$tokenizer = new Tokenizer($str);
26
		$tokens = $tokenizer->getTokens();
27
		if ($returnTokens) return $tokens;
28
		$this->result = $this->parseTokens($tokens, $element, $this->baseData);
29
		return $this->result;
30
	}
31
32
	public function parseTokens($tokens, $element, $data) {
33
		$this->result = [];
34
		$this->mode = Tokenizer::ARG;
35
		$this->data = $data;
36
		$this->last = null;
37
		$this->element = $element;
38
39
		if (empty($tokens)) return [$this->data];
40
41
		$tokenFuncs = [
42
			Tokenizer::NOT => 'processComparator',
43
			Tokenizer::EQUALS => 'processComparator',
44
			Tokenizer::DOT => 'processDot',
45
			Tokenizer::OPEN_SQUARE_BRACKET => 'processSquareBracket',
46
			Tokenizer::ARG => 'processSeparator',
47
			Tokenizer::CONCAT => 'processSeparator',
48
			Tokenizer::NAME => 'processScalar',
49
			Tokenizer::NUMERIC => 'processScalar',
50
			Tokenizer::BOOL => 'processScalar',
51
			Tokenizer::STRING => 'processString',
52
			Tokenizer::OPEN_BRACKET => 'processBrackets'
53
		];
54
55
		foreach ($tokens as $token) {
56
			$this->{$tokenFuncs[$token['type']]}($token);	
57
		}
58
59
		return $this->processLast();
60
	}
61
62
	private function processComparator($token) {
63
		$this->result = $this->processLast();
64
65
		if ($this->mode == Tokenizer::NOT && $token['type'] == Tokenizer::EQUALS) {
66
			$this->mode = Tokenizer::NOT;
67
		}
68
		else $this->mode = $token['type'];
69
	}
70
71
72
	//Reads the last selected value from $data regardless if it's an array or object and overrides $this->data with the new value
73
	private function moveLastToData() {
74
		if (isset($this->data->{$this->last})) $this->data = $this->data->{$this->last};
75 View Code Duplication
		else if (is_array($this->data) && isset($this->data[$this->last])) $this->data = $this->data[$this->last];
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
	}
77
78
	//Dot moves $data to the next object in $data foo.bar moves the $data pointer from `foo` to `bar`
79
	private function processDot($token) {
0 ignored issues
show
Unused Code introduced by
The parameter $token is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
80
		if ($this->last !== null) $this->moveLastToData();
81
		else $this->data = array_pop($this->result);
82
83
		$this->last = null;
84
	}
85
86
	private function processSquareBracket($token) {
87
		if ($this->last !== null) $this->moveLastToData();
88
89
		$parser = new Value($this->baseData, $this->autoLookup);
90
		$this->last = $parser->parseTokens($token['value'], $this->element, null)[0];
91
	}
92
93
	private function processSeparator($token) {
94
		$this->mode = $token['type'];
95
		//if ($this->last !== null) $this->result = $this->processValue($this->result, $this->mode, $this->last);
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
96
		$this->result = $this->processLast();
97
	}
98
99
	private function processScalar($token) {
100
		$this->last = $token['value'];
101
	}
102
103
	private function processString($token) {
104
		$this->result = $this->processValue($token['value']);
105
	}
106
107
	private function processBrackets($token) {
108
		if ($this->baseData instanceof \Transphporm\Functionset && $this->baseData->hasFunction($this->last)) {
109
			$this->callTransphpormFunctions($token);
110
		}
111
		else if ($this->data instanceof \Transphporm\Functionset) {
112
			$this->result = $this->processValue($this->data->{$this->last}($token['value'], $this->element));
113
			$this->last = null;
114
		}
115
		else {
116
			$parser = new Value($this->baseData, $this->autoLookup);
117
			$args = $parser->parseTokens($token['value'], $this->element, $this->data);
118
			if ($args[0] == $this->data) $args = [];
119
			$funcResult = $this->callFunc($this->last, $args, $this->element, $this->data);
120
			$this->result = $this->processValue($funcResult);
121
			$this->last = null;
122
		}
123
	}
124
125
	private function callTransphpormFunctions($token) {
126
		$this->result = $this->processValue($this->baseData->{$this->last}($token['value'], $this->element));
127
		foreach ($this->result as $i => $value) {
128
			if (is_array($this->data)) {
129
				if (isset($this->data[$value])) $this->result[$i] = $this->data[$value];
130
			}
131
			else if (is_scalar($value) && isset($this->data->$value)) $this->result[$i] = $this->data->$value;
132
		}
133
		$this->last = null;
134
	}
135
136
	//Processes the last entry down an object graph using foo.bar.baz and adds it to the result
137
	private function processLast() {
138
		if ($this->last !== null) {
139
			try {
140
				$this->result = $this->extractLast($this->result);
141
			}
142
			catch (\UnexpectedValueException $e) {
143
				if (!$this->autoLookup) {
144
					$this->result = $this->processValue($this->last);
145
				}
146
				else $this->result = [false];			
147
			}			
148
		}
149
		return $this->result;
150
	}
151
152
	private function extractLast($result) {
0 ignored issues
show
Unused Code introduced by
The parameter $result is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
153
		if ($this->autoLookup && isset($this->data->{$this->last})) {
154
			return $this->processValue($this->data->{$this->last});
155
		}
156 View Code Duplication
		else if (is_array($this->data) && isset($this->data[$this->last])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
157
			return $this->processValue($this->data[$this->last]);
158
		}
159
		throw new \UnexpectedValueException('Not found');
160
	}	
161
162
	private function processValue($newValue) {
163
		if ($this->mode == Tokenizer::ARG) {
164
			$this->result[] = $newValue;
165
		}
166
		else if ($this->mode == Tokenizer::CONCAT) {
167
				$this->result[count($this->result)-1] .= $newValue;
168
		}
169
		else if ($this->mode == Tokenizer::NOT) {
170
			$this->result[count($this->result)-1] = $this->result[count($this->result)-1] != $newValue;
171
		}
172
		else if ($this->mode == Tokenizer::EQUALS) {
173
			$this->result[count($this->result)-1] = $this->result[count($this->result)-1] == $newValue;
174
		}
175
176
		return $this->result;
177
	}
178
179
	private function callFunc($name, $args, $element, $data) {
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
180
		if ($this->data instanceof \Transphporm\FunctionSet) return $this->data->$name($args, $element);
181
		else return $this->callFuncOnObject($this->data, $name, $args, $element);
182
	}
183
184
	private function callFuncOnObject($obj, $func, $args, $element) {
0 ignored issues
show
Unused Code introduced by
The parameter $element is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
185
		if (isset($obj->$func) && is_callable($obj->$func)) return call_user_func_array($obj->$func, $args);
186
		else if (isset($obj->$func) && is_array($obj->$func))  {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
187
188
		}
189
		else return call_user_func_array([$obj, $func], $args);
190
	}
191
}
192