Skip to content

Commit e543e7b

Browse files
committed
Issue #12: Consistency in constructor arguments.
1 parent c3de5fc commit e543e7b

8 files changed

+53
-29
lines changed

spec/drupol/phptree/Node/KeyValueNodeSpec.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,16 @@ public function it_can_be_set_with_a_key_and_value()
2424

2525
public function it_can_throw_an_error_when_capacity_is_invalid()
2626
{
27-
$this->beConstructedWith(-5);
27+
$this->beConstructedWith('key', 'value', -5);
2828

2929
$this
3030
->capacity()
31-
->shouldReturn(0);
31+
->shouldReturn(-5);
3232
}
3333

3434
public function it_is_initializable()
3535
{
36+
$this->beConstructedWith('key', 'value');
3637
$this->shouldHaveType(KeyValueNode::class);
3738
}
3839
}

spec/drupol/phptree/Node/NaryNodeSpec.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,19 @@ public function it_can_have_children()
6868

6969
public function it_can_throw_an_error_when_capacity_is_invalid()
7070
{
71-
$this->beConstructedWith(-5);
71+
$this->beConstructedWith(null);
7272

7373
$this
7474
->capacity()
75-
->shouldReturn(-5);
75+
->shouldBeNull();
7676

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

8181
public function it_can_use_a_different_traverser()
8282
{
83-
$this->beConstructedWith(2, null, new PreOrder());
83+
$this->beConstructedWith(2, new PreOrder());
8484

8585
$this->getTraverser()->shouldBeAnInstanceOf(PreOrder::class);
8686
}

spec/drupol/phptree/Node/TrieNodeSpec.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class TrieNodeSpec extends NodeObjectBehavior
1111
{
1212
public function it_can_add_node()
1313
{
14-
$this->beConstructedWith('root', 'root');
14+
$this->beConstructedWith('key', 'value');
1515

1616
$nodes = [
1717
1000,
@@ -42,6 +42,7 @@ public function it_can_add_node()
4242

4343
public function it_is_initializable()
4444
{
45+
$this->beConstructedWith('key', 'value');
4546
$this->shouldHaveType(TrieNode::class);
4647
}
4748
}

src/Node/KeyValueNode.php

+12-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace drupol\phptree\Node;
66

7+
use drupol\phptree\Traverser\TraverserInterface;
8+
79
/**
810
* Class KeyValueNode.
911
*/
@@ -14,12 +16,18 @@ class KeyValueNode extends ValueNode implements KeyValueNodeInterface
1416
*
1517
* @param null|mixed $key
1618
* @param null|mixed $value
17-
* @param int $capacity
19+
* @param null|int $capacity
20+
* @param null|\drupol\phptree\Traverser\TraverserInterface $traverser
1821
* @param null|\drupol\phptree\Node\NodeInterface $parent
1922
*/
20-
public function __construct($key = null, $value = null, int $capacity = 0, NodeInterface $parent = null)
21-
{
22-
parent::__construct($value, $capacity, $parent);
23+
public function __construct(
24+
$key,
25+
$value,
26+
?int $capacity = 0,
27+
?TraverserInterface $traverser = null,
28+
?NodeInterface $parent = null
29+
) {
30+
parent::__construct($value, $capacity, $traverser, $parent);
2331

2432
$this->storage()->set('key', $key);
2533
}

src/Node/NaryNode.php

+17-10
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,30 @@ class NaryNode extends Node implements NaryNodeInterface
1515
/**
1616
* NaryNode constructor.
1717
*
18-
* @param int $capacity
19-
* The maximum children a node can have
20-
* @param null|\drupol\phptree\Node\NodeInterface $parent
21-
* The parent
18+
* @param null|int $capacity
19+
* The maximum children a node can have. Null for no children,
20+
* if 0 then any number of children is allowed.
2221
* @param null|\drupol\phptree\Traverser\TraverserInterface $traverser
23-
* The traverser
22+
* The traverser.
23+
* @param null|\drupol\phptree\Node\NodeInterface $parent
24+
* The parent.
2425
*/
25-
public function __construct(int $capacity = 0, NodeInterface $parent = null, TraverserInterface $traverser = null)
26-
{
26+
public function __construct(
27+
?int $capacity = 0,
28+
?TraverserInterface $traverser = null,
29+
?NodeInterface $parent = null
30+
) {
2731
parent::__construct($parent);
2832

2933
$this->storage()->set(
3034
'capacity',
3135
$capacity
3236
);
3337

34-
$this->storage()->set('traverser', $traverser ?? new BreadthFirst());
38+
$this->storage()->set(
39+
'traverser',
40+
$traverser ?? new BreadthFirst()
41+
);
3542
}
3643

3744
/**
@@ -65,7 +72,7 @@ public function add(NodeInterface ...$nodes): NodeInterface
6572
/**
6673
* {@inheritdoc}
6774
*/
68-
public function capacity(): int
75+
public function capacity(): ?int
6976
{
7077
return $this->storage()->get('capacity');
7178
}
@@ -115,7 +122,7 @@ protected function findFirstAvailableNode(NodeInterface $tree): ?NodeInterface
115122

116123
$capacity = $candidate->capacity();
117124

118-
if (0 > $capacity) {
125+
if (null === $capacity) {
119126
continue;
120127
}
121128

src/Node/NaryNodeInterface.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ interface NaryNodeInterface extends NodeInterface
1414
/**
1515
* Get the node capacity.
1616
*
17-
* @return int
18-
* The node capacity
17+
* @return null|int
18+
* The node capacity or null if no children is allowed.
1919
*/
20-
public function capacity(): int;
20+
public function capacity(): ?int;
2121

2222
/**
2323
* Get the traverser in use.

src/Node/TrieNode.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ public function add(NodeInterface ...$nodes): NodeInterface
4141
/**
4242
* @param \drupol\phptree\Node\ValueNodeInterface $node
4343
*
44+
* @throws \Exception
45+
*
4446
* @return \drupol\phptree\Node\NodeInterface|\drupol\phptree\Node\ValueNodeInterface
4547
*/
4648
private function append(ValueNodeInterface $node)
4749
{
48-
/** @var \drupol\phptree\Node\ValueNodeInterface $child */
4950
foreach ($this->children() as $child) {
50-
/** @var \drupol\phptree\Node\ValueNodeInterface $node */
5151
if ($node->getValue() === $child->getValue()) {
5252
return $child;
5353
}

src/Node/ValueNode.php

+11-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace drupol\phptree\Node;
66

7+
use drupol\phptree\Traverser\TraverserInterface;
8+
79
/**
810
* Class ValueNode.
911
*/
@@ -13,12 +15,17 @@ class ValueNode extends NaryNode implements ValueNodeInterface
1315
* ValueNode constructor.
1416
*
1517
* @param null|mixed $value
16-
* @param int $capacity
18+
* @param null|int $capacity
19+
* @param null|\drupol\phptree\Traverser\TraverserInterface $traverser
1720
* @param null|\drupol\phptree\Node\NodeInterface $parent
1821
*/
19-
public function __construct($value, int $capacity = 0, NodeInterface $parent = null)
20-
{
21-
parent::__construct($capacity, $parent);
22+
public function __construct(
23+
$value,
24+
?int $capacity = 0,
25+
?TraverserInterface $traverser = null,
26+
?NodeInterface $parent = null
27+
) {
28+
parent::__construct($capacity, $traverser, $parent);
2229

2330
$this->storage()->set('value', $value);
2431
}

0 commit comments

Comments
 (0)