-
-
Notifications
You must be signed in to change notification settings - Fork 105
Make DeepCopy API final and immutable #121
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
theofidry
wants to merge
6
commits into
myclabs:2.x
Choose a base branch
from
theofidry:feature/immutable
base: 2.x
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
6 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
parameters: | ||
ignoreErrors: | ||
- '#PHPDoc tag \@param has invalid value \(Array\<Filter\, Matcher\>#' | ||
- '#PHPDoc tag \@param has invalid value \(Array\<TypeFilter\, TypeMatcher\>#' |
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 |
---|---|---|
|
@@ -21,7 +21,6 @@ | |
use function is_object; | ||
use function is_resource; | ||
use function spl_object_id; | ||
use function sprintf; | ||
|
||
final class DeepCopy | ||
{ | ||
|
@@ -40,35 +39,43 @@ final class DeepCopy | |
*/ | ||
private $typeFilters = []; | ||
|
||
private $skipUncloneable = false; | ||
private $skipUncloneable; | ||
private $useCloneMethod; | ||
|
||
/** | ||
* @param bool $useCloneMethod If set to true, when an object implements the __clone() function, it will be used | ||
* instead of the regular deep cloning. | ||
* @param bool $useCloneMethod If set to true, when an object implements the __clone() function, it will | ||
* be used instead of the regular deep cloning. | ||
* @param bool $skipUncloneable If enabled, will not throw an exception when coming across an uncloneable | ||
* property. | ||
* @param Array<Filter, Matcher> List of filter-matcher pairs | ||
* @param Array<TypeFilter, TypeMatcher> List of type filter-matcher pairs | ||
*/ | ||
public function __construct(bool $useCloneMethod = false) | ||
{ | ||
public function __construct( | ||
bool $useCloneMethod = false, | ||
bool $skipUncloneable = false, | ||
array $filters = [], | ||
array $typeFilters = [] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we can upgrade this typehint to iterable and in deep_copy function too. |
||
) { | ||
$this->useCloneMethod = $useCloneMethod; | ||
|
||
$this->addTypeFilter( | ||
foreach ($filters as [$filter, $matcher]) { | ||
$this->addFilter($filter, $matcher); | ||
} | ||
|
||
$typeFilters[] = [ | ||
new DateIntervalFilter(), | ||
new TypeMatcher(DateInterval::class) | ||
); | ||
$this->addTypeFilter( | ||
]; | ||
$typeFilters[] = [ | ||
new SplDoublyLinkedListFilter($this), | ||
new TypeMatcher(SplDoublyLinkedList::class) | ||
); | ||
} | ||
]; | ||
|
||
/** | ||
* If enabled, will not throw an exception when coming across an uncloneable property. | ||
*/ | ||
public function skipUncloneable(bool $skipUncloneable = true): self | ||
{ | ||
$this->skipUncloneable = $skipUncloneable; | ||
foreach ($typeFilters as [$filter, $matcher]) { | ||
$this->addTypeFilter($filter, $matcher); | ||
} | ||
|
||
return $this; | ||
$this->skipUncloneable = $skipUncloneable; | ||
} | ||
|
||
/** | ||
|
@@ -85,16 +92,6 @@ public function copy($value) | |
return $this->recursiveCopy($value); | ||
} | ||
|
||
public function addFilter(Filter $filter, Matcher $matcher): void | ||
{ | ||
$this->filters[] = [$matcher, $filter]; | ||
} | ||
|
||
public function addTypeFilter(TypeFilter $filter, TypeMatcher $matcher): void | ||
{ | ||
$this->typeFilters[] = [$matcher, $filter]; | ||
} | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
|
@@ -146,12 +143,7 @@ private function copyObject(object $object): object | |
return $object; | ||
} | ||
|
||
throw new CloneException( | ||
sprintf( | ||
'The class "%s" is not cloneable.', | ||
$reflectedObject->getName() | ||
) | ||
); | ||
throw CloneException::unclonableClass($reflectedObject->getName()); | ||
} | ||
|
||
$newObject = clone $object; | ||
|
@@ -217,6 +209,16 @@ function ($object) { | |
$property->setValue($object, $this->recursiveCopy($propertyValue)); | ||
} | ||
|
||
private function addFilter(Filter $filter, Matcher $matcher): void | ||
{ | ||
$this->filters[] = [$matcher, $filter]; | ||
} | ||
|
||
private function addTypeFilter(TypeFilter $filter, TypeMatcher $matcher): void | ||
{ | ||
$this->typeFilters[] = [$matcher, $filter]; | ||
} | ||
|
||
/** | ||
* @return TypeFilter|null The first filter that matches variable or `null` if no such filter found | ||
*/ | ||
|
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
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.
I don't like very much the use of generics in PHPDoc because there is zero support for them into the language.
Someone could interpret this as a key-value pair, which it isn't here.