Skip to content

Commit

Permalink
Issue #12: Consistency in constructor arguments.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Jun 15, 2019
1 parent c3de5fc commit e543e7b
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 29 deletions.
5 changes: 3 additions & 2 deletions spec/drupol/phptree/Node/KeyValueNodeSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@ public function it_can_be_set_with_a_key_and_value()

public function it_can_throw_an_error_when_capacity_is_invalid()
{
$this->beConstructedWith(-5);
$this->beConstructedWith('key', 'value', -5);

$this
->capacity()
->shouldReturn(0);
->shouldReturn(-5);
}

public function it_is_initializable()
{
$this->beConstructedWith('key', 'value');
$this->shouldHaveType(KeyValueNode::class);
}
}
6 changes: 3 additions & 3 deletions spec/drupol/phptree/Node/NaryNodeSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,19 @@ public function it_can_have_children()

public function it_can_throw_an_error_when_capacity_is_invalid()
{
$this->beConstructedWith(-5);
$this->beConstructedWith(null);

$this
->capacity()
->shouldReturn(-5);
->shouldBeNull();

$this->shouldThrow(\Exception::class)
->during('add', [new NaryNode()]);
}

public function it_can_use_a_different_traverser()
{
$this->beConstructedWith(2, null, new PreOrder());
$this->beConstructedWith(2, new PreOrder());

$this->getTraverser()->shouldBeAnInstanceOf(PreOrder::class);
}
Expand Down
3 changes: 2 additions & 1 deletion spec/drupol/phptree/Node/TrieNodeSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class TrieNodeSpec extends NodeObjectBehavior
{
public function it_can_add_node()
{
$this->beConstructedWith('root', 'root');
$this->beConstructedWith('key', 'value');

$nodes = [
1000,
Expand Down Expand Up @@ -42,6 +42,7 @@ public function it_can_add_node()

public function it_is_initializable()
{
$this->beConstructedWith('key', 'value');
$this->shouldHaveType(TrieNode::class);
}
}
16 changes: 12 additions & 4 deletions src/Node/KeyValueNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace drupol\phptree\Node;

use drupol\phptree\Traverser\TraverserInterface;

/**
* Class KeyValueNode.
*/
Expand All @@ -14,12 +16,18 @@ class KeyValueNode extends ValueNode implements KeyValueNodeInterface
*
* @param null|mixed $key
* @param null|mixed $value
* @param int $capacity
* @param null|int $capacity
* @param null|\drupol\phptree\Traverser\TraverserInterface $traverser
* @param null|\drupol\phptree\Node\NodeInterface $parent
*/
public function __construct($key = null, $value = null, int $capacity = 0, NodeInterface $parent = null)
{
parent::__construct($value, $capacity, $parent);
public function __construct(
$key,
$value,
?int $capacity = 0,
?TraverserInterface $traverser = null,
?NodeInterface $parent = null
) {
parent::__construct($value, $capacity, $traverser, $parent);

$this->storage()->set('key', $key);
}
Expand Down
27 changes: 17 additions & 10 deletions src/Node/NaryNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,30 @@ class NaryNode extends Node implements NaryNodeInterface
/**
* NaryNode constructor.
*
* @param int $capacity
* The maximum children a node can have
* @param null|\drupol\phptree\Node\NodeInterface $parent
* The parent
* @param null|int $capacity
* The maximum children a node can have. Null for no children,
* if 0 then any number of children is allowed.
* @param null|\drupol\phptree\Traverser\TraverserInterface $traverser
* The traverser
* The traverser.
* @param null|\drupol\phptree\Node\NodeInterface $parent
* The parent.
*/
public function __construct(int $capacity = 0, NodeInterface $parent = null, TraverserInterface $traverser = null)
{
public function __construct(
?int $capacity = 0,
?TraverserInterface $traverser = null,
?NodeInterface $parent = null
) {
parent::__construct($parent);

$this->storage()->set(
'capacity',
$capacity
);

$this->storage()->set('traverser', $traverser ?? new BreadthFirst());
$this->storage()->set(
'traverser',
$traverser ?? new BreadthFirst()
);
}

/**
Expand Down Expand Up @@ -65,7 +72,7 @@ public function add(NodeInterface ...$nodes): NodeInterface
/**
* {@inheritdoc}
*/
public function capacity(): int
public function capacity(): ?int
{
return $this->storage()->get('capacity');
}
Expand Down Expand Up @@ -115,7 +122,7 @@ protected function findFirstAvailableNode(NodeInterface $tree): ?NodeInterface

$capacity = $candidate->capacity();

if (0 > $capacity) {
if (null === $capacity) {
continue;
}

Expand Down
6 changes: 3 additions & 3 deletions src/Node/NaryNodeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ interface NaryNodeInterface extends NodeInterface
/**
* Get the node capacity.
*
* @return int
* The node capacity
* @return null|int
* The node capacity or null if no children is allowed.
*/
public function capacity(): int;
public function capacity(): ?int;

/**
* Get the traverser in use.
Expand Down
4 changes: 2 additions & 2 deletions src/Node/TrieNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ public function add(NodeInterface ...$nodes): NodeInterface
/**
* @param \drupol\phptree\Node\ValueNodeInterface $node
*
* @throws \Exception
*
* @return \drupol\phptree\Node\NodeInterface|\drupol\phptree\Node\ValueNodeInterface
*/
private function append(ValueNodeInterface $node)
{
/** @var \drupol\phptree\Node\ValueNodeInterface $child */
foreach ($this->children() as $child) {
/** @var \drupol\phptree\Node\ValueNodeInterface $node */
if ($node->getValue() === $child->getValue()) {
return $child;
}
Expand Down
15 changes: 11 additions & 4 deletions src/Node/ValueNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace drupol\phptree\Node;

use drupol\phptree\Traverser\TraverserInterface;

/**
* Class ValueNode.
*/
Expand All @@ -13,12 +15,17 @@ class ValueNode extends NaryNode implements ValueNodeInterface
* ValueNode constructor.
*
* @param null|mixed $value
* @param int $capacity
* @param null|int $capacity
* @param null|\drupol\phptree\Traverser\TraverserInterface $traverser
* @param null|\drupol\phptree\Node\NodeInterface $parent
*/
public function __construct($value, int $capacity = 0, NodeInterface $parent = null)
{
parent::__construct($capacity, $parent);
public function __construct(
$value,
?int $capacity = 0,
?TraverserInterface $traverser = null,
?NodeInterface $parent = null
) {
parent::__construct($capacity, $traverser, $parent);

$this->storage()->set('value', $value);
}
Expand Down

0 comments on commit e543e7b

Please sign in to comment.