Skip to content

Commit 41f8a58

Browse files
authored
Merge pull request #185 from xp-framework/feature/clone-with
Implement "clone with"
2 parents 33cc068 + 1150567 commit 41f8a58

13 files changed

+231
-9
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"require" : {
99
"xp-framework/core": "^12.0 | ^11.6 | ^10.16",
1010
"xp-framework/reflection": "^3.2 | ^2.15",
11-
"xp-framework/ast": "^11.6",
11+
"xp-framework/ast": "^11.7",
1212
"php" : ">=7.4.0"
1313
},
1414
"require-dev" : {

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,12 @@ private function emitQuoted($result, $node) {
4848
}
4949

5050
protected function emitCallable($result, $callable) {
51-
$result->out->write('\Closure::fromCallable(');
52-
$this->emitQuoted($result, $callable->expression);
53-
$result->out->write(')');
51+
if ($callable->expression instanceof Literal && 'clone' === $callable->expression->expression) {
52+
$result->out->write('fn($o) => clone $o');
53+
} else {
54+
$result->out->write('\Closure::fromCallable(');
55+
$this->emitQuoted($result, $callable->expression);
56+
$result->out->write(')');
57+
}
5458
}
5559
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,6 +1082,12 @@ protected function emitNewClass($result, $new) {
10821082
$result->codegen->leave();
10831083
}
10841084

1085+
protected function emitClone($result, $clone) {
1086+
$result->out->write('clone(');
1087+
$this->emitArguments($result, $clone->arguments);
1088+
$result->out->write(')');
1089+
}
1090+
10851091
protected function emitCallable($result, $callable) {
10861092

10871093
// Disambiguate the following:

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class PHP74 extends PHP {
2222
OmitConstantTypes,
2323
ReadonlyClasses,
2424
RewriteBlockLambdaExpressions,
25+
RewriteCloneWith,
2526
RewriteEnums,
2627
RewriteExplicitOctals,
2728
RewriteProperties,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class PHP80 extends PHP {
2525
OmitConstantTypes,
2626
ReadonlyClasses,
2727
RewriteBlockLambdaExpressions,
28+
RewriteCloneWith,
2829
RewriteDynamicClassConstants,
2930
RewriteEnums,
3031
RewriteExplicitOctals,

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class PHP81 extends PHP {
2222
use
2323
EmulatePipelines,
2424
RewriteBlockLambdaExpressions,
25+
RewriteCallableClone,
26+
RewriteCloneWith,
2527
RewriteDynamicClassConstants,
2628
RewriteStaticVariableInitializations,
2729
RewriteProperties,

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class PHP82 extends PHP {
2222
use
2323
EmulatePipelines,
2424
RewriteBlockLambdaExpressions,
25+
RewriteCallableClone,
26+
RewriteCloneWith,
2527
RewriteDynamicClassConstants,
2628
RewriteStaticVariableInitializations,
2729
RewriteProperties,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* @see https://wiki.php.net/rfc#php_83
2020
*/
2121
class PHP83 extends PHP {
22-
use EmulatePipelines, RewriteBlockLambdaExpressions, RewriteProperties;
22+
use EmulatePipelines, RewriteCallableClone, RewriteCloneWith, RewriteBlockLambdaExpressions, RewriteProperties;
2323

2424
public $targetVersion= 80300;
2525

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* @see https://wiki.php.net/rfc#php_84
2020
*/
2121
class PHP84 extends PHP {
22-
use EmulatePipelines, RewriteBlockLambdaExpressions;
22+
use EmulatePipelines, RewriteCallableClone, RewriteCloneWith, RewriteBlockLambdaExpressions;
2323

2424
public $targetVersion= 80400;
2525

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* @see https://wiki.php.net/rfc#php_85
2020
*/
2121
class PHP85 extends PHP {
22-
use RewriteBlockLambdaExpressions;
22+
use RewriteBlockLambdaExpressions, RewriteCallableClone, RewriteCloneWith; // TODO: Remove once PR is merged!
2323

2424
public $targetVersion= 80500;
2525

0 commit comments

Comments
 (0)