Skip to content

Implement "clone with" #185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"require" : {
"xp-framework/core": "^12.0 | ^11.6 | ^10.16",
"xp-framework/reflection": "^3.2 | ^2.15",
"xp-framework/ast": "^11.5",
"xp-framework/ast": "dev-feature/clone-with as 11.6.0",
"php" : ">=7.4.0"
},
"require-dev" : {
Expand Down
13 changes: 13 additions & 0 deletions src/main/php/lang/ast/emit/PHP.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,19 @@ protected function emitNewClass($result, $new) {
$result->codegen->leave();
}

protected function emitClone($result, $clone) {
$result->out->write('clone ');
if (empty($clone->with)) {
$this->emitOne($result, $clone->expression);
} else {
$result->out->write('(');
$this->emitOne($result, $clone->expression);
$result->out->write(',');
$this->emitArguments($result, $clone->with);
$result->out->write(')');
}
}

protected function emitCallable($result, $callable) {

// Disambiguate the following:
Expand Down
1 change: 1 addition & 0 deletions src/main/php/lang/ast/emit/PHP74.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class PHP74 extends PHP {
OmitConstantTypes,
ReadonlyClasses,
RewriteBlockLambdaExpressions,
RewriteCloneWith,
RewriteEnums,
RewriteExplicitOctals,
RewriteProperties,
Expand Down
1 change: 1 addition & 0 deletions src/main/php/lang/ast/emit/PHP80.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class PHP80 extends PHP {
OmitConstantTypes,
ReadonlyClasses,
RewriteBlockLambdaExpressions,
RewriteCloneWith,
RewriteDynamicClassConstants,
RewriteEnums,
RewriteExplicitOctals,
Expand Down
1 change: 1 addition & 0 deletions src/main/php/lang/ast/emit/PHP81.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
class PHP81 extends PHP {
use
RewriteBlockLambdaExpressions,
RewriteCloneWith,
RewriteDynamicClassConstants,
RewriteStaticVariableInitializations,
RewriteProperties,
Expand Down
1 change: 1 addition & 0 deletions src/main/php/lang/ast/emit/PHP82.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
class PHP82 extends PHP {
use
RewriteBlockLambdaExpressions,
RewriteCloneWith,
RewriteDynamicClassConstants,
RewriteStaticVariableInitializations,
RewriteProperties,
Expand Down
2 changes: 1 addition & 1 deletion src/main/php/lang/ast/emit/PHP83.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* @see https://wiki.php.net/rfc#php_83
*/
class PHP83 extends PHP {
use RewriteBlockLambdaExpressions, RewriteProperties;
use RewriteBlockLambdaExpressions, RewriteCloneWith, RewriteProperties;

public $targetVersion= 80300;

Expand Down
2 changes: 1 addition & 1 deletion src/main/php/lang/ast/emit/PHP84.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* @see https://wiki.php.net/rfc#php_84
*/
class PHP84 extends PHP {
use RewriteBlockLambdaExpressions;
use RewriteBlockLambdaExpressions, RewriteCloneWith;

public $targetVersion= 80400;

Expand Down
32 changes: 32 additions & 0 deletions src/main/php/lang/ast/emit/RewriteCloneWith.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php namespace lang\ast\emit;

use lang\ast\nodes\{Signature, Parameter};

/** @see https://wiki.php.net/rfc/clone_with_v2 */
trait RewriteCloneWith {

protected function emitClone($result, $clone) {
if (empty($clone->with)) return parent::emitClone($result, $clone);

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

$result->out->write(')?(function($a) {');
foreach ($clone->with as $name => $argument) {
$result->out->write('$this->'.$name.'=$a["'.$name.'"];');
}

$result->out->write('return $this;})->bindTo('.$t.','.$t.')([');
foreach ($clone->with as $name => $argument) {
$result->out->write('"'.$name.'"=>');
$this->emitOne($result, $argument);
$result->out->write(',');
}
$result->out->write(']):null');
}
}
29 changes: 26 additions & 3 deletions src/test/php/lang/ast/unittest/emit/CloningTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ class CloningTest extends EmittingTest {
#[Before]
public function fixture() {
$this->fixture= new class() {
public $id= 1;
private $id= 1;
private $name= 'Test';

public function toString() {
return "<id: {$this->id}, name: {$this->name}>";
}

public function with($id) {
$this->id= $id;
Expand Down Expand Up @@ -50,8 +55,26 @@ public function clone_interceptor_called() {
public function run($in) {
return clone $in;
}
}', $this->fixture->with(id: 1));
}', $this->fixture->with(1));

Assert::equals(
['<id: 1, name: Test>', '<id: 2, name: Test>'],
[$this->fixture->toString(), $clone->toString()]
);
}

#[Test]
public function clone_with() {
$clone= $this->run('class %T {
private $id= 6100;
public function run($in) {
return clone($in, id: $this->id, name: "Changed");
}
}', $this->fixture->with(1));

Assert::equals([1, 2], [$this->fixture->id, $clone->id]);
Assert::equals(
['<id: 1, name: Test>', '<id: 6100, name: Changed>'],
[$this->fixture->toString(), $clone->toString()]
);
}
}
Loading