6
6
trait RewriteCloneWith {
7
7
8
8
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 ;
10
11
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:
12
13
// `function($args) { $this->id= $args['id']; return $this; }`, then bind
13
14
// this closure to the cloned instance before invoking it with the named
14
15
// 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 ( );
18
19
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 );
22
31
}
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 ' );
31
32
}
32
33
}
0 commit comments