Completed
Push — master ( e6458b...c9bd8d )
by Tom
03:29
created

Value::processLast()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 15
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 15
rs 8.2222
cc 7
eloc 10
nc 5
nop 4
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 $data;
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
14
	public function __construct($data, $autoLookup = false) {
15
		$this->data = $data;
16
		$this->autoLookup = $autoLookup;
17
	}
18
19
	public function parse($str, $element = null, $returnTokens = false) {
20
		$tokenizer = new Tokenizer($str);
21
		$tokens = $tokenizer->getTokens();
22
		if ($returnTokens) return $tokens;
23
		$result = $this->parseTokens($tokens, $element, $this->data);
24
		return $result;
25
	}
26
27
	public function parseTokens($tokens, $element, $data) {
28
		$result = [];
29
		$mode = Tokenizer::ARG;
30
		$last = null;
31
32
		if (empty($tokens)) return [$data];
33
34
		foreach ($tokens as $token) {
35
		if (is_string($token)) throw new \Exception($token);
36
37
			if (in_array($token['type'], [Tokenizer::NOT, Tokenizer::EQUALS])) {
38
				// ($last !== null) $result = $this->processValue($result, $mode, $last);
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
39
40
				$result = $this->processLast($last, $result, $mode, $data);
41
42
				if ($mode == Tokenizer::NOT && $token['type'] == Tokenizer::EQUALS) {
43
					$mode = Tokenizer::NOT;
44
				}
45
				else $mode = $token['type'];
46
			}
47
48
			if ($token['type'] === Tokenizer::DOT) {
49 View Code Duplication
				if ($last !== null) {
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...
50
					if (isset($data->$last)) $data = $data->$last;
51
					else if (is_array($data) && isset($data[$last])) $data = $data[$last];
52
				}
53
				else $data = array_pop($result);
54
55
				$last = null;
56
			}
57
58
			if ($token['type'] == Tokenizer::OPEN_SQUARE_BRACKET) {
59
				if ($last !== null) {
60
					if (isset($data->$last)) $data = $data->$last;
61
					else if (is_array($data) && isset($data[$last])) $data = $data[$last];
62
				}
63
64
				$last = $this->parseTokens($token['value'], $element, null)[0];
65
			}
66
67
			if (in_array($token['type'], [Tokenizer::ARG, Tokenizer::CONCAT])) {
68
				$mode = $token['type'];
69
				//if ($last !== null) $result = $this->processValue($result, $mode, $last);
0 ignored issues
show
Unused Code Comprehensibility introduced by
61% 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...
70
				$result = $this->processLast($last, $result, $mode, $data);
71
			}
72
73
			if ($token['type'] === Tokenizer::STRING) {
74
				$result = $this->processValue($result, $mode, $token['value']);
75
			}
76
77
			if (in_array($token['type'], [Tokenizer::NAME, Tokenizer::NUMERIC, Tokenizer::BOOL])) {
78
				$last = $token['value'];
79
			}
80
81
			if ($token['type'] == Tokenizer::OPEN_BRACKET) {
82
83
				if ($this->data instanceof \Transphporm\Functionset && ($last == 'data' || $last == 'iteration' || $last == 'attr')) {
84
					$result = $this->processValue($result, $mode, $this->data->$last($token['value'], $element));
85
86
					foreach ($result as $i => $value) {
87 View Code Duplication
						if (is_array($data)) {
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...
88
							if (isset($data[$value])) $result[$i] = $data[$value];
89
						}
90
						else if (is_scalar($value) && isset($data->$value)) $result[$i] = $data->$value;
91
					}
92
					$last = null;
93
				}
94
				else if ($data instanceof \Transphporm\Functionset) {
95
					$result = $this->processValue($result, $mode, $data->$last($token['value'], $element));
96
					$last = null;
97
				}
98
				else {
99
					$args = $this->parseTokens($token['value'], $element, $data);
100
					if ($args[0] == $data) $args = [];
101
					$funcResult = $this->callFunc($last, $args, $element, $data);
102
					$result = $this->processValue($result, $mode, $funcResult);
103
					$last = null;
104
				}
105
			}
106
/*
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% 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...
107
			if ($token['type'] == Tokenizer::OPEN_SQUARE_BRACKET) {
108
				if ($this->autoLookup === true) {
109
					$result = $this->processValue($result, $mode, $data->$last($token['value'], $element));
110
					$last = null;
111
112
				}
113
			}//*/
114
		}
115
116
		return $this->processLast($last, $result, $mode, $data);
117
	}
118
119
	private function processLast($last, $result, $mode, $data) {
120
		if ($last !== null) {
121
			if ($this->autoLookup && isset($data->$last)) {
122
				$result = $this->processValue($result, $mode, $data->$last);
123
			}
124
			else if (is_array($data) && isset($data[$last])) {
125
				$result = $this->processValue($result, $mode, $data[$last]);
126
			}
127
			else if (!$this->autoLookup) {
128
				$result = $this->processValue($result, $mode, $last);
129
			}
130
			else $result = [false];
131
		}
132
		return $result;
133
	}
134
135
	private function processValue($result, $mode, $newValue) {
136
		if ($mode == Tokenizer::ARG) {
137
			$result[] = $newValue;
138
		}
139
		else if ($mode == Tokenizer::CONCAT) {
140
				$result[count($result)-1] .= $newValue;
141
		}
142
		else if ($mode == Tokenizer::NOT) {
143
			$result[count($result)-1] = $result[count($result)-1] != $newValue;
144
		}
145
		else if ($mode == Tokenizer::EQUALS) {
146
			$result[count($result)-1] = $result[count($result)-1] == $newValue;
147
		}
148
149
		return $result;
150
	}
151
152
	private function callFunc($name, $args, $element, $data) {
153
		if ($data instanceof \Transphporm\FunctionSet) return $data->$name($args, $element);
154
		else return $this->callFuncOnObject($data, $name, $args, $element);
155
	}
156
157
	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...
158
		if (isset($obj->$func) && is_callable($obj->$func)) return call_user_func_array($obj->$func, $args);
159
		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...
160
161
		}
162
		else return call_user_func_array([$obj, $func], $args);
163
	}
164
}
165