1 | <?php |
||
9 | class Value { |
||
10 | private $dataFunction; |
||
11 | private $callParamsAsArray; |
||
12 | private $parent; |
||
13 | const IS_NOT_FUNCTION = 'isNotFunction'; |
||
14 | |||
15 | public function __construct($dataFunction, Value $parent = null, $callParamsAsArray = true) { |
||
20 | |||
21 | private function extractQuotedString($marker, $str) { |
||
27 | |||
28 | private function parseFunction($function) { |
||
39 | |||
40 | public function parse($function, \DomElement $element, $rules = []) { |
||
41 | $stringExtractor = new StringExtractor($function); |
||
42 | $parts = explode('+', $stringExtractor); |
||
43 | |||
44 | $result = []; |
||
45 | foreach ($parts as $part) { |
||
46 | $part = $stringExtractor->rebuild($part); |
||
47 | $result = array_merge($result, $this->parseString(trim($part), $element, $rules)); |
||
48 | } |
||
49 | |||
50 | return $result; |
||
51 | } |
||
52 | |||
53 | private function parseString($function, $element, $rules) { |
||
54 | $result = []; |
||
55 | if ($function && in_array($function[0], ['\'', '"'])) { |
||
56 | $finalPos = $this->findMatchingPos($function, $function[0]); |
||
57 | $result[] = $this->extractQuotedString($function[0], $function); |
||
58 | } |
||
59 | else { |
||
60 | $func = $this->parseFunction($function); |
||
61 | $finalPos = $func['endPoint']; |
||
62 | |||
63 | if (($data = $this->getFunctionValue($func['name'], $func['params'], $element, $rules)) !== self::IS_NOT_FUNCTION) $result = $this->appendToArray($result, $data); |
||
64 | else $result[] = trim($function); |
||
65 | } |
||
66 | $remaining = trim(substr($function, $finalPos+1)); |
||
67 | return $this->parseNextValue($remaining, $result, $element); |
||
68 | } |
||
69 | |||
70 | private function getFunctionValue($name, $params, $element, $rules) { |
||
71 | if (($data = $this->callFunc($name, $params, $element, $rules)) !== self::IS_NOT_FUNCTION) { |
||
72 | return $data; |
||
73 | } |
||
74 | else if ($this->parent != null && ($data = $this->parent->callFunc($name, $params, $element, $rules)) !== self::IS_NOT_FUNCTION) { |
||
75 | return $data; |
||
76 | } |
||
77 | else return self::IS_NOT_FUNCTION; |
||
78 | } |
||
79 | |||
80 | private function appendToArray($array, $value) { |
||
85 | |||
86 | private function callFunc($name, $params, $element, $rules) { |
||
87 | if ($name && $this->isCallable($this->dataFunction, $name)) { |
||
88 | if ($this->callParamsAsArray) return $this->dataFunction->$name($this->parse($params, $element), $element, $rules); |
||
89 | else { |
||
90 | return $this->callFuncOnObject($this->dataFunction, $name, $this->parse($params, $element)); |
||
91 | } |
||
92 | } |
||
93 | return self::IS_NOT_FUNCTION; |
||
94 | } |
||
95 | |||
96 | //is_callable does not detect closures on properties, only methods defined in the class! |
||
97 | private function isCallable($obj, $func) { |
||
100 | |||
101 | private function callFuncOnObject($obj, $func, $params) { |
||
102 | $args = []; |
||
103 | foreach ($params as $param) { |
||
104 | $stringExtractor = new StringExtractor($param); |
||
110 | |||
111 | private function callFuncOrClosure($obj, $func, $args) { |
||
115 | |||
116 | private function parseNextValue($remaining, $result, $element) { |
||
120 | |||
121 | private function findMatchingPos($string, $char, $start = 0, $escape = '\\') { |
||
132 | } |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.