Skip to content
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

Make DeepCopy API final and immutable #121

Open
wants to merge 6 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
52 changes: 27 additions & 25 deletions src/DeepCopy/DeepCopy.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,31 +44,38 @@ final class DeepCopy
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
Copy link
Contributor

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.

*/
public function __construct(bool $useCloneMethod = false)
{
public function __construct(
bool $useCloneMethod = false,
bool $skipUncloneable = true,
array $filters = [],
array $typeFilters = []
Copy link
Contributor

Choose a reason for hiding this comment

The 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(
$filters[] = [
new DateIntervalFilter(),
new TypeMatcher(DateInterval::class)
);
$this->addTypeFilter(
];

foreach ($filters as [$filter, $matcher]) {
$this->addFilter($filter, $matcher);
}

$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;

return $this;
foreach ($typeFilters as [$filter, $matcher]) {
$this->addTypeFilter($filter, $matcher);
}
}

/**
Expand All @@ -85,12 +92,12 @@ public function copy($value)
return $this->recursiveCopy($value);
}

public function addFilter(Filter $filter, Matcher $matcher): void
private function addFilter(Filter $filter, Matcher $matcher): void
{
$this->filters[] = [$matcher, $filter];
}

public function addTypeFilter(TypeFilter $filter, TypeMatcher $matcher): void
private function addTypeFilter(TypeFilter $filter, TypeMatcher $matcher): void
{
$this->typeFilters[] = [$matcher, $filter];
}
Expand Down Expand Up @@ -146,12 +153,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;
Expand Down
9 changes: 9 additions & 0 deletions src/DeepCopy/Exception/CloneException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,13 @@

class CloneException extends UnexpectedValueException
{
final public static function unclonableClass(string $class): self
{
return new self(
sprintf(
'The class "%s" is not cloneable.',
$class
)
);
}
}
20 changes: 14 additions & 6 deletions src/DeepCopy/deep_copy.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@
* Deep copies the given value.
*
* @param mixed $value
* @param bool $useCloneMethod
*
* @return mixed
* @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
*/
function deep_copy($value, $useCloneMethod = false)
{
return (new DeepCopy($useCloneMethod))->copy($value);
public function deep_copy(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

public here is a syntax error

$value,
bool $useCloneMethod = false,
bool $skipUncloneable = true,
array $filters = [],
array $typeFilters = []
) {
return (new DeepCopy($useCloneMethod, $skipUncloneable, $filters, $typeFilters))->copy($value);
}