Conditions | 52 |
Paths | 55 |
Total Lines | 174 |
Code Lines | 120 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
41 | private function dispatch(array $node, $value) |
||
42 | { |
||
43 | $dispatcher = $this->fnDispatcher; |
||
44 | |||
45 | switch ($node['type']) { |
||
46 | |||
47 | case 'field': |
||
48 | if (is_array($value) || $value instanceof \ArrayAccess) { |
||
49 | return isset($value[$node['value']]) ? $value[$node['value']] : null; |
||
50 | } elseif ($value instanceof \stdClass) { |
||
51 | return isset($value->{$node['value']}) ? $value->{$node['value']} : null; |
||
52 | } |
||
53 | return null; |
||
54 | |||
55 | case 'subexpression': |
||
56 | return $this->dispatch( |
||
57 | $node['children'][1], |
||
58 | $this->dispatch($node['children'][0], $value) |
||
59 | ); |
||
60 | |||
61 | case 'index': |
||
62 | if (!Utils::isArray($value)) { |
||
63 | return null; |
||
64 | } |
||
65 | $idx = $node['value'] >= 0 |
||
66 | ? $node['value'] |
||
67 | : $node['value'] + count($value); |
||
68 | return isset($value[$idx]) ? $value[$idx] : null; |
||
69 | |||
70 | case 'projection': |
||
71 | $left = $this->dispatch($node['children'][0], $value); |
||
72 | switch ($node['from']) { |
||
73 | case 'object': |
||
74 | if (!Utils::isObject($left)) { |
||
75 | return null; |
||
76 | } |
||
77 | break; |
||
78 | case 'array': |
||
79 | if (!Utils::isArray($left)) { |
||
80 | return null; |
||
81 | } |
||
82 | break; |
||
83 | default: |
||
84 | if (!is_array($left) || !($left instanceof \stdClass)) { |
||
|
|||
85 | return null; |
||
86 | } |
||
87 | } |
||
88 | |||
89 | $collected = []; |
||
90 | foreach ((array) $left as $val) { |
||
91 | $result = $this->dispatch($node['children'][1], $val); |
||
92 | if ($result !== null) { |
||
93 | $collected[] = $result; |
||
94 | } |
||
95 | } |
||
96 | |||
97 | return $collected; |
||
98 | |||
99 | case 'flatten': |
||
100 | static $skipElement = []; |
||
101 | $value = $this->dispatch($node['children'][0], $value); |
||
102 | |||
103 | if (!Utils::isArray($value)) { |
||
104 | return null; |
||
105 | } |
||
106 | |||
107 | $merged = []; |
||
108 | foreach ($value as $values) { |
||
109 | // Only merge up arrays lists and not hashes |
||
110 | if (is_array($values) && isset($values[0])) { |
||
111 | $merged = array_merge($merged, $values); |
||
112 | } elseif ($values !== $skipElement) { |
||
113 | $merged[] = $values; |
||
114 | } |
||
115 | } |
||
116 | |||
117 | return $merged; |
||
118 | |||
119 | case 'literal': |
||
120 | return $node['value']; |
||
121 | |||
122 | case 'current': |
||
123 | return $value; |
||
124 | |||
125 | case 'or': |
||
126 | $result = $this->dispatch($node['children'][0], $value); |
||
127 | return Utils::isTruthy($result) |
||
128 | ? $result |
||
129 | : $this->dispatch($node['children'][1], $value); |
||
130 | |||
131 | case 'and': |
||
132 | $result = $this->dispatch($node['children'][0], $value); |
||
133 | return Utils::isTruthy($result) |
||
134 | ? $this->dispatch($node['children'][1], $value) |
||
135 | : $result; |
||
136 | |||
137 | case 'not': |
||
138 | return !Utils::isTruthy( |
||
139 | $this->dispatch($node['children'][0], $value) |
||
140 | ); |
||
141 | |||
142 | case 'pipe': |
||
143 | return $this->dispatch( |
||
144 | $node['children'][1], |
||
145 | $this->dispatch($node['children'][0], $value) |
||
146 | ); |
||
147 | |||
148 | case 'multi_select_list': |
||
149 | if ($value === null) { |
||
150 | return null; |
||
151 | } |
||
152 | |||
153 | $collected = []; |
||
154 | foreach ($node['children'] as $node) { |
||
155 | $collected[] = $this->dispatch($node, $value); |
||
156 | } |
||
157 | |||
158 | return $collected; |
||
159 | |||
160 | case 'multi_select_hash': |
||
161 | if ($value === null) { |
||
162 | return null; |
||
163 | } |
||
164 | |||
165 | $collected = []; |
||
166 | foreach ($node['children'] as $node) { |
||
167 | $collected[$node['value']] = $this->dispatch( |
||
168 | $node['children'][0], |
||
169 | $value |
||
170 | ); |
||
171 | } |
||
172 | |||
173 | return $collected; |
||
174 | |||
175 | case 'comparator': |
||
176 | $left = $this->dispatch($node['children'][0], $value); |
||
177 | $right = $this->dispatch($node['children'][1], $value); |
||
178 | if ($node['value'] == '==') { |
||
179 | return Utils::isEqual($left, $right); |
||
180 | } elseif ($node['value'] == '!=') { |
||
181 | return !Utils::isEqual($left, $right); |
||
182 | } else { |
||
183 | return self::relativeCmp($left, $right, $node['value']); |
||
184 | } |
||
185 | |||
186 | case 'condition': |
||
187 | return Utils::isTruthy($this->dispatch($node['children'][0], $value)) |
||
188 | ? $this->dispatch($node['children'][1], $value) |
||
189 | : null; |
||
190 | |||
191 | case 'function': |
||
192 | $args = []; |
||
193 | foreach ($node['children'] as $arg) { |
||
194 | $args[] = $this->dispatch($arg, $value); |
||
195 | } |
||
196 | return $dispatcher($node['value'], $args); |
||
197 | |||
198 | case 'slice': |
||
199 | return is_string($value) || Utils::isArray($value) |
||
200 | ? Utils::slice( |
||
201 | $value, |
||
202 | $node['value'][0], |
||
203 | $node['value'][1], |
||
204 | $node['value'][2] |
||
205 | ) : null; |
||
206 | |||
207 | case 'expref': |
||
208 | $apply = $node['children'][0]; |
||
209 | return function ($value) use ($apply) { |
||
210 | return $this->visit($apply, $value); |
||
211 | }; |
||
212 | |||
213 | default: |
||
214 | throw new \RuntimeException("Unknown node type: {$node['type']}"); |
||
215 | } |
||
236 |