-
Notifications
You must be signed in to change notification settings - Fork 0
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
thekid
wants to merge
14
commits into
master
Choose a base branch
from
feature/clone-with
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
6e193ca
Implement "clone with"
thekid 39c14a6
MFH
thekid 3a36357
Adjust to current version of RFC using `clone(<expr>, $properties)`
thekid 2818d88
Simplify implementation
thekid 16aab10
Add test verifying private & protected accessibility from "clone with"
thekid f1e5bdc
Raise an error when non-array is passed to `withProperties`
thekid 511d04f
Add tests for clone used as callable
thekid f9c63e4
Support `clone(...)` first-class callable syntax
thekid 513a199
Fix PHP 8.3 emitter
thekid 443aefe
Support clone with unpacking
thekid 8feae1c
Implement support for partial unpacking
thekid 7aba1b9
Support clone with unpack on PHP 7.4
thekid 7caf0e9
Merge branch 'master' into feature/clone-with
thekid 959a5e8
Rewrite `clone with` and `clone(...)` callable
thekid File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php namespace lang\ast\emit; | ||
|
||
use lang\ast\nodes\Literal; | ||
|
||
/** @see https://wiki.php.net/rfc/clone_with_v2 */ | ||
trait RewriteCallableClone { | ||
|
||
protected function emitCallable($result, $callable) { | ||
if ($callable->expression instanceof Literal && 'clone' === $callable->expression->expression) { | ||
$result->out->write('fn($o) => clone $o'); | ||
} else { | ||
parent::emitCallable($result, $callable); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php namespace lang\ast\emit; | ||
|
||
use lang\ast\nodes\{ArrayLiteral, UnpackExpression}; | ||
|
||
/** @see https://wiki.php.net/rfc/clone_with_v2 */ | ||
trait RewriteCloneWith { | ||
|
||
protected function emitClone($result, $clone) { | ||
static $wrapper= '(function($c, array $w) { foreach ($w as $p=>$v) { $c->$p=$v; } return $c;})'; | ||
|
||
$expr= $clone->arguments['object'] ?? $clone->arguments[0] ?? null; | ||
$with= $clone->arguments['withProperties'] ?? $clone->arguments[1] ?? null; | ||
|
||
// Built ontop of a wrapper function which iterates over the property-value pairs, | ||
// assigning them to the clone. Unwind unpack statements, e.g. `clone(...$args)`, | ||
// into an array, manually unpacking it for invocation. | ||
if ($expr instanceof UnpackExpression || $with instanceof UnpackExpression) { | ||
$t= $result->temp(); | ||
$result->out->write('('.$t.'='); | ||
$this->emitOne($result, new ArrayLiteral($with ? [[null, $expr], [null, $with]] : [[null, $expr]], $clone->line)); | ||
$result->out->write(')?'); | ||
$result->out->write($wrapper.'(clone ('.$t.'["object"] ?? '.$t.'[0]), '.$t.'["withProperties"] ?? '.$t.'[1] ?? [])'); | ||
$result->out->write(':null'); | ||
} else if ($with) { | ||
$result->out->write($wrapper.'(clone '); | ||
$this->emitOne($result, $expr); | ||
$result->out->write(','); | ||
$this->emitOne($result, $with); | ||
$result->out->write(')'); | ||
} else { | ||
$result->out->write('clone '); | ||
$this->emitOne($result, $expr); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Once the PR in
php-src
is merged, we can drop these and natively emitclone
expressions as function calls with their arguments.