Completed
Push — master ( f9a7a2...eb8de6 )
by Tom
02:33
created

Value::processValue()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 1
Metric Value
c 4
b 1
f 1
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
14
	/*
15
		Stores the last value e.g. 
16
			"a" + "b"
17
		Will store "a" before reading the token for the + and perfoming the concatenate operation
18
	*/
19
	private $last;
20
	private $data;
21
	private $result;
22
23
	private $tokenFuncs = [
24
			Tokenizer::NOT => 'processComparator',
25
			Tokenizer::EQUALS => 'processComparator',
26
			Tokenizer::DOT => 'processDot',
27
			Tokenizer::OPEN_SQUARE_BRACKET => 'processSquareBracket',
28
			Tokenizer::ARG => 'processSeparator',
29
			Tokenizer::CONCAT => 'processSeparator',
30
			Tokenizer::NAME => 'processScalar',
31
			Tokenizer::NUMERIC => 'processScalar',
32
			Tokenizer::BOOL => 'processScalar',
33
			Tokenizer::STRING => 'processString',
34
			Tokenizer::OPEN_BRACKET => 'processBrackets'
35
	];
36
37
	public function __construct($data, $autoLookup = false) {
38
		$this->baseData = $data;
39
		$this->autoLookup = $autoLookup;
40
	}
41
42
	public function parse($str) {
43
		$tokenizer = new Tokenizer($str);
44
		$tokens = $tokenizer->getTokens();
45
		$this->result = $this->parseTokens($tokens, $this->baseData);
46
		return $this->result;
47
	}
48
49
	public function parseTokens($tokens, $data) {
50
		$this->result = new ValueResult;
51
		$this->data = $data;
52
		$this->last = null;
53
54
		if (empty($tokens)) return [$this->data];
55
		
56
		foreach ($tokens as $token) {
57
			$this->{$this->tokenFuncs[$token['type']]}($token);	
58
		}
59
60
		$this->processLast();
61
		return $this->result->getResult();
62
	}
63
64
	private function processComparator($token) {
65
		$this->result = $this->processLast();
66
67
		if ($this->result->getMode() == Tokenizer::NOT && $token['type'] == Tokenizer::EQUALS) {
68
			$this->result->setMode(Tokenizer::NOT);
69
		}
70
		else $this->result->setMode($token['type']);
71
	}
72
73
74
	//Reads the last selected value from $data regardless if it's an array or object and overrides $this->data with the new value
75
	private function moveLastToData() {
76
		if (isset($this->data->{$this->last})) $this->data = $this->data->{$this->last};
77 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...
78
	}
79
80
	//Dot moves $data to the next object in $data foo.bar moves the $data pointer from `foo` to `bar`
81
	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...
82
		if ($this->last !== null) $this->moveLastToData();
83
		else $this->data = $this->result->pop();
84
85
		$this->last = null;
86
	}
87
88
	private function processSquareBracket($token) {
89
		if ($this->last !== null) $this->moveLastToData();
90
		$parser = new Value($this->baseData, $this->autoLookup);
91
		$this->last = $parser->parseTokens($token['value'], null)[0];
92
	}
93
94
	private function processSeparator($token) {
95
		$this->result->setMode($token['type']);
96
		//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...
97
		$this->result = $this->processLast();
98
	}
99
100
	private function processScalar($token) {
101
		$this->last = $token['value'];
102
	}
103
104
	private function processString($token) {
105
		$this->result->processValue($token['value']);
106
	}
107
108
	private function processBrackets($token) {
109
		if ($this->baseData instanceof \Transphporm\Functionset && $this->baseData->hasFunction($this->last)) {
110
			$this->callTransphpormFunctions($token);
111
		}
112
		else if ($this->data instanceof \Transphporm\Functionset) {
113
			$this->result = $this->result->processValue($this->data->{$this->last}($token['value']));
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $this->result is correct as $this->result->processVa...last}($token['value'])) (which targets Transphporm\Parser\ValueResult::processValue()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
114
			$this->last = null;
115
		}
116
		else {
117
			$parser = new Value($this->baseData, $this->autoLookup);
118
			$args = $parser->parseTokens($token['value'], $this->data);
119
			if ($args[0] == $this->data) $args = [];
120
			$funcResult = $this->callFunc($this->last, $args, $this->data);
121
			$this->result->processValue($funcResult);
122
			$this->last = null;
123
		}
124
	}
125
126
	private function callTransphpormFunctions($token) {
127
		$this->result->processValue($this->baseData->{$this->last}($token['value']));
128
		foreach ($this->result->getResult() as $i => $value) {
129
			if (is_array($this->data)) {
130
				if (isset($this->data[$value])) $this->result[$i] = $this->data[$value];
131
			}
132
			else if (is_scalar($value) && isset($this->data->$value)) $this->result[$i] = $this->data->$value;
133
		}
134
		$this->last = null;
135
	}
136
137
	//Applies the current operation to whatever is in $last based on $mode
138
	private function processLast() {
139
		if ($this->last !== null) {
140
			try {
141
				$this->extractLast($this->result);
142
			}
143
			catch (\UnexpectedValueException $e) {
144
				if (!$this->autoLookup) {
145
					$this->result->processValue($this->last);
146
				}
147
				else {
148
					$this->result->clear();
149
					$this->result[0] = false;
150
				}
151
			}			
152
		}
153
		return $this->result;
154
	}
155
156
	//Extracts $last from $data. If "last" is "bar" from value "foo.bar",
157
	//$data contains "foo" and this function reads $data[$bar] or $data->$bar
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
158
	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...
159
		if ($this->autoLookup && isset($this->data->{$this->last})) {
160
			return $this->result->processValue($this->data->{$this->last});
161
		}
162 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...
163
			return $this->result->processValue($this->data[$this->last]);
164
		}
165
		throw new \UnexpectedValueException('Not found');
166
	}	
167
168
	private function callFunc($name, $args, $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...
169
		return $this->callFuncOnObject($this->data, $name, $args);
170
	}
171
172
	private function callFuncOnObject($obj, $func, $args) {
173
		if (isset($obj->$func) && is_callable($obj->$func)) return call_user_func_array($obj->$func, $args);
174
		else return call_user_func_array([$obj, $func], $args);
175
	}
176
}