Skip to content

Commit

Permalink
Issue #6: Replace unserialize() and serialize() with proper clone calls.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Jun 14, 2019
1 parent d71bcb1 commit 7be956b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 26 deletions.
21 changes: 11 additions & 10 deletions spec/drupol/phptree/Node/NodeSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@

class NodeSpec extends NodeObjectBehavior
{
public function it_can_add(NodeInterface $node)
public function it_can_add()
{
$node->setParent($this)->shouldBeCalledOnce();

$this->add($node)
$this->add(new Node())
->shouldReturn($this);
}

Expand All @@ -40,8 +38,9 @@ public function it_can_check_if_node_is_root()

$node = new Node();

$node->add($this->getWrappedObject());

$this
->setParent($node)
->isRoot()
->shouldReturn(false);
}
Expand Down Expand Up @@ -77,7 +76,7 @@ public function it_can_delete()

$this
->delete($node2)
->shouldReturn($node2->setParent(null));
->shouldReturn($node2);

$this
->degree()
Expand All @@ -89,7 +88,7 @@ public function it_can_delete()

$this
->delete($node3)
->shouldReturn($node3->setParent(null));
->shouldReturn($node3);

$this
->degree()
Expand All @@ -112,10 +111,12 @@ public function it_can_get_and_set_the_parent(NodeInterface $parent)
->getParent()
->shouldReturn(null);

$node = new Node();
$node->add($this->getWrappedObject());

$this
->setParent($parent)
->getParent()
->shouldReturn($parent);
->shouldReturn($node);
}

public function it_can_get_its_ancestors()
Expand All @@ -128,7 +129,7 @@ public function it_can_get_its_ancestors()
$level1 = new Node($root);
$level2 = new Node($level1);

$this->setParent($level2);
$level2->add($this->getWrappedObject());

$this
->getAncestors()
Expand Down
29 changes: 13 additions & 16 deletions src/Node/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ public function __construct(NodeInterface $parent = null)
$this->storage()->setParent($parent);
}

/**
* {@inheritdoc}
*/
public function __clone()
{
$this->storage = clone $this->storage;

foreach ($this->children() as $child) {
$child->setParent($this);
}
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -71,22 +83,7 @@ public function children(): \Traversable
*/
public function clone(): NodeInterface
{
// @todo: Replace with something better.
return \unserialize(\serialize($this));
// @todo: Unable to get this code working yet.
/*
$root = $this;
$root = clone $root;
$children = [];
foreach ($root->children() as $key => $child) {
$children[] = $child->clone();
}
return $root->withChildren(...$children);
*/
return clone $this;
}

/**
Expand Down
14 changes: 14 additions & 0 deletions src/Storage/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ public function __construct()
$this->storage = new \ArrayObject();
}

/**
* {@inheritdoc}
*/
public function __clone()
{
$this->storage = clone $this->storage;

$children = new \ArrayObject();
foreach ($this->get('children') as $key => $child) {
$children[] = clone $child;
}
$this->set('children', $children);
}

/**
* {@inheritdoc}
*/
Expand Down

0 comments on commit 7be956b

Please sign in to comment.