Skip to content

Commit 3a36357

Browse files
committed
Adjust to current version of RFC using clone(<expr>, $properties)
1 parent 39c14a6 commit 3a36357

File tree

2 files changed

+20
-26
lines changed

2 files changed

+20
-26
lines changed

src/main/php/lang/ast/emit/PHP.class.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,16 +1083,9 @@ protected function emitNewClass($result, $new) {
10831083
}
10841084

10851085
protected function emitClone($result, $clone) {
1086-
$result->out->write('clone ');
1087-
if (empty($clone->with)) {
1088-
$this->emitOne($result, $clone->expression);
1089-
} else {
1090-
$result->out->write('(');
1091-
$this->emitOne($result, $clone->expression);
1092-
$result->out->write(',');
1093-
$this->emitArguments($result, $clone->with);
1094-
$result->out->write(')');
1095-
}
1086+
$result->out->write('clone(');
1087+
$this->emitArguments($result, $clone->arguments);
1088+
$result->out->write(')');
10961089
}
10971090

10981091
protected function emitCallable($result, $callable) {

src/main/php/lang/ast/emit/RewriteCloneWith.class.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,28 @@
66
trait RewriteCloneWith {
77

88
protected function emitClone($result, $clone) {
9-
if (empty($clone->with)) return parent::emitClone($result, $clone);
9+
$expr= $clone->arguments['object'] ?? $clone->arguments[0] ?? null;
10+
$with= $clone->arguments['withProperties'] ?? $clone->arguments[1] ?? null;
1011

11-
// Wrap clone with, e.g. clone($x, id: 6100), inside an IIFE as follows:
12+
// Wrap clone with, e.g. clone($x, ['id' => 6100]), inside an IIFE as follows:
1213
// `function($args) { $this->id= $args['id']; return $this; }`, then bind
1314
// this closure to the cloned instance before invoking it with the named
1415
// arguments so we can access non-public members.
15-
$t= $result->temp();
16-
$result->out->write('('.$t.'=clone ');
17-
$this->emitOne($result, $clone->expression);
16+
if ($with) {
17+
$c= $result->temp();
18+
$a= $result->temp();
1819

19-
$result->out->write(')?(function($a) {');
20-
foreach ($clone->with as $name => $argument) {
21-
$result->out->write('$this->'.$name.'=$a["'.$name.'"];');
20+
$result->out->write('['.$c.'=clone ');
21+
$this->emitOne($result, $expr);
22+
$result->out->write(','.$a.'=');
23+
$this->emitOne($result, $with);
24+
$result->out->write(']?(function($a) { foreach ($a as $p=>$v) { $this->$p= $v; }return $this;})');
25+
$result->out->write('->bindTo('.$c.','.$c.')('.$a.'):null');
26+
} else if (isset($clone->arguments['object'])) {
27+
$result->out->write('clone ');
28+
$this->emitOne($result, $expr);
29+
} else {
30+
return parent::emitClone($result, $clone);
2231
}
23-
24-
$result->out->write('return $this;})->bindTo('.$t.','.$t.')([');
25-
foreach ($clone->with as $name => $argument) {
26-
$result->out->write('"'.$name.'"=>');
27-
$this->emitOne($result, $argument);
28-
$result->out->write(',');
29-
}
30-
$result->out->write(']):null');
3132
}
3233
}

0 commit comments

Comments
 (0)