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

Added DeepCopy to replace __clone implementations #25

Open
wants to merge 1 commit into
base: master
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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"scriptfusion/retry": "^1.1",
"scriptfusion/retry-exception-handlers": "^1",
"eloquent/enumeration": "^5",
"myclabs/deep-copy": "^1",
"psr/cache": "^1",
"zendframework/zend-uri": "^2"
},
Expand Down
32 changes: 31 additions & 1 deletion src/Porter.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
<?php
namespace ScriptFUSION\Porter;

use DeepCopy\DeepCopy;
use DeepCopy\Filter\KeepFilter;
use DeepCopy\Matcher\PropertyTypeMatcher;
use DeepCopy\TypeFilter\ShallowCopyFilter;
use DeepCopy\TypeMatcher\TypeMatcher;
use Eloquent\Enumeration\MultitonInterface;
use Mockery\MockInterface;
use ScriptFUSION\Porter\Cache\CacheAdvice;
use ScriptFUSION\Porter\Cache\CacheToggle;
use ScriptFUSION\Porter\Cache\CacheUnavailableException;
Expand Down Expand Up @@ -45,6 +52,11 @@ class Porter
*/
private $maxFetchAttempts = self::DEFAULT_FETCH_ATTEMPTS;

/**
* @var DeepCopy
*/
private $deepCopy;

/**
* @var callable
*/
Expand All @@ -53,6 +65,7 @@ class Porter
public function __construct()
{
$this->defaultCacheAdvice = CacheAdvice::SHOULD_NOT_CACHE();
$this->deepCopy = $this->createDeepCopier();
}

/**
Expand All @@ -66,7 +79,9 @@ public function __construct()
*/
public function import(ImportSpecification $specification)
{
$specification = clone $specification;
/** @var ImportSpecification $specification */
$specification = $this->deepCopy->copy($specification);

$records = $this->fetch($specification->getResource(), $specification->getCacheAdvice());

if (!$records instanceof ProviderRecords) {
Expand Down Expand Up @@ -316,4 +331,19 @@ public function setFetchExceptionHandler(callable $fetchExceptionHandler)
{
$this->fetchExceptionHandler = $fetchExceptionHandler;
}

/**
* @return DeepCopy
*/
private function createDeepCopier()
{
$copier = new DeepCopy;

// Enumerations are immutable thus do not require cloning.
$copier->addFilter(new KeepFilter, new PropertyTypeMatcher(MultitonInterface::class));

$copier->addTypeFilter(new ShallowCopyFilter, new TypeMatcher(MockInterface::class));

return $copier;
}
}
15 changes: 0 additions & 15 deletions src/Specification/ImportSpecification.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,6 @@ public function __construct(ProviderResource $resource)
$this->clearTransformers();
}

public function __clone()
{
$this->resource = clone $this->resource;

$transformers = $this->transformers;
$this->clearTransformers()->addTransformers(array_map(
function (Transformer $transformer) {
return clone $transformer;
},
$transformers
));

is_object($this->context) && $this->context = clone $this->context;
}

/**
* @return ProviderResource
*/
Expand Down