Completed
Push — master ( 7b17ad...a29029 )
by Tom
02:30
created

ValueData::traverse()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 9.2
cc 4
eloc 3
nc 3
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
/** Holds the data used by `ValueParser` */
9
class ValueData {
10
	private $data;
11
12
	public function __construct($data) {
13
		$this->data = $data;
14
	}
15
16
	public function traverse($key) {
17
		if (isset($this->data->{$key})) $this->data = $this->data->{$key};
18
		else if (is_array($this->data) && isset($this->data[$key])) $this->data = $this->data[$key];
19
	}
20
21
	public function read($value) {
22
		if (is_array($this->data)) {
23
			if (isset($this->data[$value])) return $this->data[$value];
24
		}
25
		else if (isset($this->data->$value)) return $this->data->$value;
26
		else return false;
27
	}
28
29
	public function isFunctionSet() {
30
		return $this->data instanceof \Transphporm\Functioset;
0 ignored issues
show
Bug introduced by
The class Transphporm\Functioset does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
31
	}
32
33
	public function call($func, $args) {
34
		return $this->data->$func(...$args);
35
	}
36
37
	public function parseNested($parser, $token, $funcName) {
38
		$args = $parser->parseTokens($token['value'], $this->data);
39
		if ($args[0] == $this->data) $args = [];
40
		return $this->callFunc($funcName, $args, $this->data);
41
	}
42
43
	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...
44
		return $this->callFuncOnObject($this->data, $name, $args);
45
	}
46
47
	private function callFuncOnObject($obj, $func, $args) {
48
		if (isset($obj->$func) && is_callable($obj->$func)) return call_user_func_array($obj->$func, $args);
49
		else return call_user_func_array([$obj, $func], $args);
50
	}
51
52
	public function extract($last, $autoLookup) {
53
		$value = $this->read($last);
54
		if ($value && ($autoLookup || is_array($this->data)) ) {
55
			return $value;
56
		}
57
		throw new \UnexpectedValueException('Not found');
58
	}
59
}