Skip to content

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
wants to merge 6 commits into
base: 2.x
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 .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ before_script:
- mkdir -p build/logs

script:
- vendor/bin/phpstan analyse -l 7 src/ tests/ fixtures/
- vendor/bin/phpstan analyse -l7 -cphpstan.neon src/ tests/ fixtures/
- vendor/bin/phpunit --coverage-clover build/logs/clover.xml

after_script:
Expand Down
102 changes: 62 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ function deep_copy($var)
static $copier = null;

if (null === $copier) {
$copier = new DeepCopy(true);
$copier = new DeepCopy();
}

return $copier->copy($var);
Expand All @@ -124,8 +124,11 @@ function deep_copy($var)
You can add filters to customize the copy process by adding filters:

```php
$copier = new DeepCopy();
$copier->addFilter($filter, $matcher);
$copier = new DeepCopy(
false,
false,
[[$filter, $matcher]]
);
```

During the copy process, when a property is matched by a [matcher][matcher], then the [filter][filter] associated to
Expand Down Expand Up @@ -203,8 +206,11 @@ use DeepCopy\Filter\SetNullFilter;
$object = MyClass::load(123);
echo $object->id; // 123

$copier = new DeepCopy();
$copier->addFilter(new SetNullFilter(), new PropertyNameMatcher('id'));
$copier = new DeepCopy(
false,
false,
[[new SetNullFilter(), new PropertyNameMatcher('id')]]
);

$copy = $copier->copy($object);

Expand All @@ -221,10 +227,10 @@ use DeepCopy\DeepCopy;
use DeepCopy\Filter\KeepFilter;
use DeepCopy\Matcher\PropertyMatcher;

$copier = new DeepCopy();
$copier->addFilter(
new KeepFilter(),
new PropertyMatcher(MyClass::class, 'category')
$copier = new DeepCopy(
false,
false,
[[new KeepFilter(), new PropertyMatcher(MyClass::class, 'category')]]
);

$copy = $copier->copy($object); // $object is an instance of MyClass
Expand All @@ -242,10 +248,10 @@ use DeepCopy\Filter\Doctrine\DoctrineCollectionFilter;
use DeepCopy\Matcher\PropertyTypeMatcher;
use Doctrine\Common\Collections\Collection;

$copier = new DeepCopy();
$copier->addFilter(
new DoctrineCollectionFilter(),
new PropertyTypeMatcher(Collection::class)
$copier = new DeepCopy(
false,
false,
[[new DoctrineCollectionFilter(), new PropertyTypeMatcher(Collection::class)]]
);

$copy = $copier->copy($object);
Expand All @@ -262,10 +268,10 @@ use DeepCopy\DeepCopy;
use DeepCopy\Filter\Doctrine\DoctrineEmptyCollectionFilter;
use DeepCopy\Matcher\PropertyMatcher;

$copier = new DeepCopy();
$copier->addFilter(
new DoctrineEmptyCollectionFilter(),
new PropertyMatcher(MyClass::class, 'myProperty')
$copier = new DeepCopy(
false,
false,
[[new DoctrineEmptyCollectionFilter(), new PropertyMatcher(MyClass::class, 'myProperty')]]
);

$copy = $copier->copy($object);
Expand All @@ -292,9 +298,14 @@ use DeepCopy\Filter\SetNullFilter;
use DeepCopy\Matcher\Doctrine\DoctrineProxyMatcher;
use DeepCopy\Matcher\PropertyNameMatcher;

$copier = new DeepCopy();
$copier->addFilter(new DoctrineProxyFilter(), new DoctrineProxyMatcher());
$copier->addFilter(new SetNullFilter(), new PropertyNameMatcher('id'));
$copier = new DeepCopy(
false,
false,
[
[new DoctrineProxyFilter(), new DoctrineProxyMatcher()],
[new SetNullFilter(), new PropertyNameMatcher('id')],
]
);

$copy = $copier->copy($object);

Expand All @@ -311,14 +322,19 @@ use DeepCopy\DeepCopy;
use DeepCopy\Filter\ReplaceFilter;
use DeepCopy\Matcher\PropertyMatcher;

$copier = new DeepCopy();
$copier->addFilter(
new ReplaceFilter(
function ($currentValue): string {
return $currentValue . ' (copy)'
}
),
new PropertyMatcher(MyClass::class, 'title')
$copier = new DeepCopy(
false,
false,
[
[
new ReplaceFilter(
function ($currentValue): string {
return $currentValue . ' (copy)'
}
),
new PropertyMatcher(MyClass::class, 'title'),
],
]
);

$copy = $copier->copy($object); // $object is an instance of MyClass
Expand All @@ -333,14 +349,19 @@ use DeepCopy\DeepCopy;
use DeepCopy\TypeFilter\ReplaceFilter;
use DeepCopy\TypeMatcher\TypeMatcher;

$copier = new DeepCopy();
$copier->addFilter(
new ReplaceFilter(
function (MyClass $myClass): string {
return get_class($myClass)
}
),
new TypeMatcher(MyClass::class)
$copier = new DeepCopy(
false,
false,
[
[
new ReplaceFilter(
function (MyClass $myClass): string {
return get_class($myClass)
}
),
new TypeMatcher(MyClass::class),
],
]
);

$copy = $copier->copy([new MyClass, 'some string', new MyClass]);
Expand All @@ -359,10 +380,11 @@ use DeepCopy\TypeFilter\ShallowCopyFilter;
use DeepCopy\TypeMatcher\TypeMatcher;
use Mockery as m;

$copier = new DeepCopy();
$copier->addTypeFilter(
new ShallowCopyFilter,
new TypeMatcher(m\MockInterface::class)
$copier = new DeepCopy(
false,
false,
[],
[[new ShallowCopyFilter, new TypeMatcher(m\MockInterface::class)]]
);

$myServiceWithMocks = new MyService(
Expand Down
4 changes: 4 additions & 0 deletions phpstan.neon
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\>#'
70 changes: 36 additions & 34 deletions src/DeepCopy/DeepCopy.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use function is_object;
use function is_resource;
use function spl_object_id;
use function sprintf;

final class DeepCopy
{
Expand All @@ -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
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 = false,
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(
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;
}

/**
Expand All @@ -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
*/
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
*/
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
)
);
}
}
19 changes: 14 additions & 5 deletions src/DeepCopy/deep_copy.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,20 @@
* 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)
function deep_copy(
$value,
bool $useCloneMethod = false,
bool $skipUncloneable = false,
array $filters = [],
array $typeFilters = []
)
{
return (new DeepCopy($useCloneMethod))->copy($value);
return (new DeepCopy($useCloneMethod, $skipUncloneable, $filters, $typeFilters))->copy($value);
}
Loading