Skip to content

Commit 75240d6

Browse files
committed
API cleanup: TreeCompiler variadic write fn
1 parent f1e1a0b commit 75240d6

File tree

1 file changed

+60
-55
lines changed

1 file changed

+60
-55
lines changed

src/TreeCompiler.php

Lines changed: 60 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function visit(array $ast, $fnName, $expr)
2727
->write('use JmesPath\\Utils;')
2828
->write('')
2929
->write("// {$expr}")
30-
->write('function %s(Ti $interpreter, $value) {', [$fnName])
30+
->write('function %s(Ti $interpreter, $value) {', $fnName)
3131
->indent()
3232
->write('$current = $value;')
3333
->dispatch($ast)
@@ -66,15 +66,20 @@ private function makeVar($prefix)
6666
}
6767

6868
/**
69-
* Writes the given line of source code.
70-
* @param string $str String to write
71-
* @param array $positional Positional substitutions.
69+
* Writes the given line of source code. Pass positional arguments to write
70+
* that match the format of sprintf.
71+
*
72+
* @param string $str String to write
7273
* @return $this
7374
*/
74-
private function write($str, array $positional = [])
75+
private function write($str)
7576
{
76-
$this->source .= $this->indentation
77-
. (!$positional ? $str : vsprintf($str, $positional)) . "\n";
77+
$this->source .= $this->indentation;
78+
if (func_num_args() == 1) {
79+
$this->source .= $str . "\n";
80+
return $this;
81+
}
82+
$this->source .= vsprintf($str, array_slice(func_get_args(), 1)) . "\n";
7883
return $this;
7984
}
8085

@@ -102,11 +107,11 @@ private function visit_or(array $node)
102107
{
103108
$a = $this->makeVar('beforeOr');
104109
return $this
105-
->write('%s = $value;', [$a])
110+
->write('%s = $value;', $a)
106111
->dispatch($node['children'][0])
107112
->write('if (!$value && $value !== "0" && $value !== 0) {')
108113
->indent()
109-
->write('$value = %s;', [$a])
114+
->write('$value = %s;', $a)
110115
->dispatch($node['children'][1])
111116
->outdent()
112117
->write('}');
@@ -129,11 +134,11 @@ private function visit_field(array $node)
129134
$obj = '$value->{' . var_export($node['value'], true) . '}';
130135
$this->write('if (is_array($value) || $value instanceof \\ArrayAccess) {')
131136
->indent()
132-
->write('$value = isset(%s) ? %s : null;', [$arr, $arr])
137+
->write('$value = isset(%s) ? %s : null;', $arr, $arr)
133138
->outdent()
134139
->write('} elseif ($value instanceof \\stdClass) {')
135140
->indent()
136-
->write('$value = isset(%s) ? %s : null;', [$obj, $obj])
141+
->write('$value = isset(%s) ? %s : null;', $obj, $obj)
137142
->outdent()
138143
->write("} else {")
139144
->indent()
@@ -151,16 +156,16 @@ private function visit_index(array $node)
151156
return $this->write(
152157
'$value = (is_array($value) || $value instanceof \\ArrayAccess)'
153158
. ' && isset(%s) ? %s : null;',
154-
[$check, $check]
159+
$check, $check
155160
);
156161
}
157162

158163
$a = $this->makeVar('count');
159164
return $this
160165
->write('if (is_array($value) || ($value instanceof \\ArrayAccess && $value instanceof \\Countable)) {')
161166
->indent()
162-
->write('%s = count($value) + %s;', [$a, $node['value']])
163-
->write('$value = isset($value[%s]) ? $value[%s] : null;', [$a, $a])
167+
->write('%s = count($value) + %s;', $a, $node['value'])
168+
->write('$value = isset($value[%s]) ? $value[%s] : null;', $a, $a)
164169
->outdent()
165170
->write('} else {')
166171
->indent()
@@ -171,7 +176,7 @@ private function visit_index(array $node)
171176

172177
private function visit_literal(array $node)
173178
{
174-
return $this->write('$value = %s;', [var_export($node['value'], true)]);
179+
return $this->write('$value = %s;', var_export($node['value'], true));
175180
}
176181

177182
private function visit_pipe(array $node)
@@ -194,29 +199,29 @@ private function visit_multi_select_hash(array $node)
194199
$value = $this->makeVar('prev');
195200
$this->write('if ($value !== null) {')
196201
->indent()
197-
->write('%s = [];', [$listVal])
198-
->write('%s = $current;', [$tmpCurrent])
199-
->write('%s = $value;', [$value]);
202+
->write('%s = [];', $listVal)
203+
->write('%s = $current;', $tmpCurrent)
204+
->write('%s = $value;', $value);
200205

201206
$first = true;
202207
foreach ($node['children'] as $child) {
203208
if (!$first) {
204-
$this->write('$value = %s;', [$value]);
209+
$this->write('$value = %s;', $value);
205210
}
206211
$first = false;
207212
if ($node['type'] == 'multi_select_hash') {
208213
$this->dispatch($child['children'][0]);
209214
$key = var_export($child['value'], true);
210-
$this->write('%s[%s] = $value;', [$listVal, $key]);
215+
$this->write('%s[%s] = $value;', $listVal, $key);
211216
} else {
212217
$this->dispatch($child);
213-
$this->write('%s[] = $value;', [$listVal]);
218+
$this->write('%s[] = $value;', $listVal);
214219
}
215220
}
216221

217222
return $this
218-
->write('$value = %s;', [$listVal])
219-
->write('$current = %s;', [$tmpCurrent])
223+
->write('$value = %s;', $listVal)
224+
->write('$current = %s;', $tmpCurrent)
220225
->outdent()
221226
->write('}');
222227
}
@@ -226,32 +231,32 @@ private function visit_function(array $node)
226231
$value = $this->makeVar('val');
227232
$current = $this->makeVar('current');
228233
$args = $this->makeVar('args');
229-
$this->write('%s = $value;', [$value])
230-
->write('%s = $current;', [$current])
231-
->write('%s = [];', [$args]);
234+
$this->write('%s = $value;', $value)
235+
->write('%s = $current;', $current)
236+
->write('%s = [];', $args);
232237

233238
foreach ($node['children'] as $arg) {
234239
$this->dispatch($arg);
235-
$this->write('%s[] = $value;', [$args])
236-
->write('$current = %s;', [$current])
237-
->write('$value = %s;', [$value]);
240+
$this->write('%s[] = $value;', $args)
241+
->write('$current = %s;', $current)
242+
->write('$value = %s;', $value);
238243
}
239244

240245
return $this->write(
241246
'$value = Fn::getInstance()->__invoke("%s", %s);',
242-
[$node['value'], $args]
247+
$node['value'], $args
243248
);
244249
}
245250

246251
private function visit_slice(array $node)
247252
{
248253
return $this
249254
->write('$value = !is_string($value) && !Utils::isArray($value)')
250-
->write(' ? null : Utils::slice($value, %s, %s, %s);', [
255+
->write(' ? null : Utils::slice($value, %s, %s, %s);',
251256
var_export($node['value'][0], true),
252257
var_export($node['value'][1], true),
253258
var_export($node['value'][2], true)
254-
]);
259+
);
255260
}
256261

257262
private function visit_current(array $node)
@@ -264,7 +269,7 @@ private function visit_expref(array $node)
264269
$child = var_export($node['children'][0], true);
265270
return $this->write('$value = function ($value) use ($interpreter) {')
266271
->indent()
267-
->write('return $interpreter->visit(%s, $value);', [$child])
272+
->write('return $interpreter->visit(%s, $value);', $child)
268273
->outdent()
269274
->write('};');
270275
}
@@ -283,21 +288,21 @@ private function visit_flatten(array $node)
283288
->outdent()
284289
->write('} else {')
285290
->indent()
286-
->write('%s = [];', [$merged])
287-
->write('foreach ($value as %s) {', [$val])
291+
->write('%s = [];', $merged)
292+
->write('foreach ($value as %s) {', $val)
288293
->indent()
289-
->write('if (is_array(%s) && isset(%s[0])) {', [$val, $val])
294+
->write('if (is_array(%s) && isset(%s[0])) {', $val, $val)
290295
->indent()
291-
->write('%s = array_merge(%s, %s);', [$merged, $merged, $val])
296+
->write('%s = array_merge(%s, %s);', $merged, $merged, $val)
292297
->outdent()
293-
->write('} elseif (%s !== []) {', [$val])
298+
->write('} elseif (%s !== []) {', $val)
294299
->indent()
295-
->write('%s[] = %s;', [$merged, $val])
300+
->write('%s[] = %s;', $merged, $val)
296301
->outdent()
297302
->write('}')
298303
->outdent()
299304
->write('}')
300-
->write('$value = %s;', [$merged])
305+
->write('$value = %s;', $merged)
301306
->outdent()
302307
->write('}');
303308

@@ -322,19 +327,19 @@ private function visit_projection(array $node)
322327

323328
$this->write('if ($value !== null) {')
324329
->indent()
325-
->write('%s = [];', [$collected])
326-
->write('foreach ((array) $value as %s) {', [$val])
330+
->write('%s = [];', $collected)
331+
->write('foreach ((array) $value as %s) {', $val)
327332
->indent()
328-
->write('$value = %s;', [$val])
333+
->write('$value = %s;', $val)
329334
->dispatch($node['children'][1])
330335
->write('if ($value !== null) {')
331336
->indent()
332-
->write('%s[] = $value;', [$collected])
337+
->write('%s[] = $value;', $collected)
333338
->outdent()
334339
->write('}')
335340
->outdent()
336341
->write('}')
337-
->write('$value = %s;', [$collected])
342+
->write('$value = %s;', $collected)
338343
->outdent()
339344
->write('}');
340345

@@ -362,28 +367,28 @@ private function visit_comparator(array $node)
362367

363368
$this
364369
->write('// Visiting comparator node')
365-
->write('%s = $value;', [$value])
366-
->write('%s = $current;', [$tmpCurrent])
370+
->write('%s = $value;', $value)
371+
->write('%s = $current;', $tmpCurrent)
367372
->dispatch($node['children'][0])
368-
->write('%s = $value;', [$a])
369-
->write('$value = %s;', [$value])
373+
->write('%s = $value;', $a)
374+
->write('$value = %s;', $value)
370375
->dispatch($node['children'][1])
371-
->write('%s = $value;', [$b]);
376+
->write('%s = $value;', $b);
372377

373378
if ($node['value'] == '==') {
374-
$this->write('$result = Utils::equal(%s, %s);', [$a, $b]);
379+
$this->write('$result = Utils::equal(%s, %s);', $a, $b);
375380
} elseif ($node['value'] == '!=') {
376-
$this->write('$result = !Utils::equal(%s, %s);', [$a, $b]);
381+
$this->write('$result = !Utils::equal(%s, %s);', $a, $b);
377382
} else {
378383
$this->write(
379384
'$result = is_int(%s) && is_int(%s) && %s %s %s;',
380-
[$a, $b, $a, $node['value'], $b]
385+
$a, $b, $a, $node['value'], $b
381386
);
382387
}
383388

384389
return $this
385-
->write('$value = $result === true ? %s : null;', [$value])
386-
->write('$current = %s;', [$tmpCurrent]);
390+
->write('$value = $result === true ? %s : null;', $value)
391+
->write('$current = %s;', $tmpCurrent);
387392
}
388393

389394
/** @internal */

0 commit comments

Comments
 (0)