Completed
Push — master ( f71faa...7b17ad )
by Tom
03:02
created

Value::processString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
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 = new ValueData($data);
52
		$this->last = null;
53
54
		if (empty($tokens)) return [$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
76
	//Dot moves $data to the next object in $data foo.bar moves the $data pointer from `foo` to `bar`
77
	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...
78
		if ($this->last !== null) $this->data->traverse($this->last);
79
		else $this->data = new ValueData($this->result->pop());
80
81
		$this->last = null;
82
	}
83
84
	private function processSquareBracket($token) {
85
		if ($this->last !== null) $this->data->traverse($this->last);
86
		$parser = new Value($this->baseData, $this->autoLookup);
87
		$this->last = $parser->parseTokens($token['value'], null)[0];
88
	}
89
90
	private function processSeparator($token) {
91
		$this->result->setMode($token['type']);
92
		//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...
93
		$this->result = $this->processLast();
94
	}
95
96
	private function processScalar($token) {
97
		$this->last = $token['value'];
98
	}
99
100
	private function processString($token) {
101
		$this->result->processValue($token['value']);
102
	}
103
104
	private function processBrackets($token) {
105
		if ($this->baseData instanceof \Transphporm\Functionset && $this->baseData->hasFunction($this->last)) {
106
			$this->callTransphpormFunctions($token);
107
		}
108
		else if ($this->data->isFunctionSet()) {
109
			$this->result = $this->result->processValue($this->data->call($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...rray($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...
110
			$this->last = null;
111
		}
112
		else {
113
			$this->processNested($token);
114
		}
115
	}
116
117
	private function processNested($token) {
118
		$parser = new Value($this->baseData, $this->autoLookup);
119
		$funcResult = $this->data->parseNested($parser, $token, $this->last);
120
		$this->result->processValue($funcResult);
121
		$this->last = null;
122
	}
123
124
	private function callTransphpormFunctions($token) {
125
		$this->result->processValue($this->baseData->{$this->last}($token['value']));
126
		foreach ($this->result->getResult() as $i => $value) {
127
			$val = $this->data->read($value);
128
			if ($val) $this->result[$i] = $val;
129
		}
130
		$this->last = null;
131
	}
132
133
	//Applies the current operation to whatever is in $last based on $mode
134
	private function processLast() {
135
		if ($this->last !== null) {
136
			try {
137
				$value = $this->data->extract($this->last, $this->autoLookup);
138
				$this->result->processValue($value);
139
			}
140
			catch (\UnexpectedValueException $e) {
141
				if (!$this->autoLookup) {
142
					$this->result->processValue($this->last);
143
				}
144
				else {
145
					$this->result->clear();
146
					$this->result[0] = false;
147
				}
148
			}			
149
		}
150
		return $this->result;
151
	}	
152
}