Skip to content

Commit a2d8529

Browse files
authored
Merge pull request #6 from CrimsonNynja/PHP-7.4
upgraded to utilize PHP 7.4 features
2 parents 87372fc + 8ad0b38 commit a2d8529

File tree

10 files changed

+50
-59
lines changed

10 files changed

+50
-59
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
}
1111
],
1212
"require": {
13-
"php": ">=7.2.0"
13+
"php": ">=7.4.0"
1414
},
1515
"autoload": {
1616
"psr-4": {

composer.lock

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/PhpTrees/BinarySearchTree.php

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
class BinarySearchTree implements \Iterator
1212
{
1313
/* the root of the tree */
14-
private $root = null;
14+
private ?BstNode $root = null;
1515
/* used for iterating over the tree */
16-
private $iteratorPosition = false;
16+
private ?BstNode $iteratorPosition = null;
1717
/* used for iterating through the tree */
18-
private $iteratorStack = null;
18+
private ?Stack $iteratorStack = null;
1919
/* if set, used to comparing non literal values */
2020
private $comparator = null;
2121

@@ -92,9 +92,7 @@ public function getRoot() : ?BstNode
9292
*/
9393
public function find($value, BstNode $node = null) : ?BstNode
9494
{
95-
if ($node === null) {
96-
$node = $this->root;
97-
}
95+
$node ??= $this->root;
9896

9997
if ($this->root === null) {
10098
return null;
@@ -170,9 +168,7 @@ public function hasValues(...$values) : bool
170168
*/
171169
private function getMinNode(BstNode $node = null) : ?BstNode
172170
{
173-
if ($node === null) {
174-
$node = $this->root;
175-
}
171+
$node ??= $this->root;
176172

177173
if ($this->root === null) {
178174
return null;
@@ -293,7 +289,7 @@ public function hasComparator() : bool
293289
public function __clone()
294290
{
295291
$this->root = clone $this->root;
296-
$this->iteratorPosition = false;
292+
$this->iteratorPosition = null;
297293
$this->iteratorStack = null;
298294
}
299295

@@ -332,12 +328,17 @@ public function next() : void
332328
$node = $node->getLeftChild();
333329
}
334330
}
335-
$this->iteratorPosition = $this->iteratorStack->peek();
331+
if ($this->iteratorStack->peek() !== false) {
332+
$this->iteratorPosition = $this->iteratorStack->peek();
333+
}
334+
else {
335+
$this->iteratorPosition = null;
336+
}
336337
}
337338
}
338339

339340
/**
340-
* sets the iterator to the start value, or the left ;most leaf of the tree
341+
* sets the iterator to the start value, or the left most leaf of the tree
341342
*/
342343
public function rewind() : void
343344
{
@@ -358,7 +359,7 @@ public function rewind() : void
358359
*/
359360
public function valid() : bool
360361
{
361-
if ($this->iteratorPosition === false) {
362+
if ($this->iteratorPosition === null) {
362363
return false;
363364
}
364365
return true;

src/PhpTrees/BstNode.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ class BstNode
1010
/* the nodes value */
1111
private $value = null;
1212
/* the left child of the node */
13-
private $left = null;
13+
private ?BstNode $left = null;
1414
/* the right child of the node */
15-
private $right = null;
15+
private ?BstNode $right = null;
1616
/* used for quick deletions */
17-
private $parent = null;
17+
private ?BstNode $parent = null;
1818
/* the node's id, used for iterating over the tree */
19-
private $id = 0;
19+
private int $id = 0;
2020
/* if set, used to comparing non literal values */
2121
private $comparator = null;
2222

src/PhpTrees/GenericNode.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ class GenericNode
1212
/* The value of the node */
1313
private $value = null;
1414
/* The nodes children */
15-
private $children = [];
15+
private array $children = [];
1616
/* the nodes parent */
17-
private $parent = null;
17+
private ?GenericNode $parent = null;
1818
/* the nodes id */
19-
private $id = 0;
19+
private int $id = 0;
2020

2121
/**
2222
* constructs a node

src/PhpTrees/GenericTree.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
class GenericTree
1111
{
1212
/* the root of the tree */
13-
private $root;
13+
private ?GenericNode $root = null;
1414

1515
/**
1616
* construct the node
@@ -38,9 +38,8 @@ public function getRoot() : ?GenericNode
3838
*/
3939
public function findNode(int $id, ?GenericNode $node = null) : ?GenericNode
4040
{
41-
if ($node === null) {
42-
$node = $this->root;
43-
}
41+
$node ??= $this->root;
42+
4443
if ($node === null) {
4544
return null;
4645
}
@@ -68,9 +67,8 @@ public function findNode(int $id, ?GenericNode $node = null) : ?GenericNode
6867
*/
6968
public function findNodeByValue($value, ?GenericNode $node = null) : ?GenericNode
7069
{
71-
if ($node === null) {
72-
$node = $this->root;
73-
}
70+
$node ??= $this->root;
71+
7472
if ($node === null) {
7573
return null;
7674
}

src/PhpTrees/Rope.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
class Rope implements \ArrayAccess
1111
{
1212
/* the root of the tree */
13-
private $root = null;
13+
private ?RopeNode $root = null;
1414

1515
/**
1616
* constructs the rope with default value if given
@@ -52,9 +52,7 @@ public function length() : int
5252
*/
5353
public function insert(string $value, ?int $index = null) : void
5454
{
55-
if ($index === null) {
56-
$index = $this->length();
57-
}
55+
$index ??= $this->length();
5856

5957
if ($index >= $this->length()) {
6058
$r = concatRope($this, new Rope($value));
@@ -77,9 +75,8 @@ public function insert(string $value, ?int $index = null) : void
7775
*/
7876
public function index(int $index, RopeNode $node = null) : ?string
7977
{
80-
if ($node === null) {
81-
$node = $this->root;
82-
}
78+
$node ??= $this->root;
79+
8380
if ($node === null) {
8481
return null;
8582
}
@@ -104,9 +101,7 @@ public function index(int $index, RopeNode $node = null) : ?string
104101
*/
105102
public function &splitNodeAtPosition(int $index, RopeNode $node = null) : ?RopeNode
106103
{
107-
if ($node === null) {
108-
$node = $this->root;
109-
}
104+
$node ??= $this->root;
110105

111106
if ($node->getWeight() <= $index && $node->getRightChild() !== null) {
112107
return $this->splitNodeAtPosition($index - $node->getWeight(), $node->getRightChild());

src/PhpTrees/RopeNode.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
class RopeNode
99
{
1010
/* THe value of the node */
11-
private $value = null;
11+
private ?string $value = null;
1212
/* the left child of the node */
13-
private $left = null;
13+
private ?RopeNode $left = null;
1414
/* the right child of the node */
15-
private $right = null;
15+
private ?RopeNode $right = null;
1616
/* the nodes parent */
17-
private $parent = null;
17+
private ?RopeNode $parent = null;
1818
/* the weight of the node, representing the value length */
19-
private $weight = 0;
19+
private int $weight = 0;
2020

2121
/**
2222
* constructs a Rope Node

src/PhpTrees/Stack.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88
class Stack
99
{
10-
private $container = [];
10+
private array $container = [];
1111

1212
/**
1313
* pushes the given values onto the stack

tests/PhpTrees/BstNodeTest.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,8 @@ public function testHasChild()
8282
public function testAddComparator()
8383
{
8484
$n = new BstNode("12345");
85-
$n->setComparator(function($val, $val2) {
86-
return (strlen($val) <= strlen($val2));
87-
});
85+
$n->setComparator(fn($val, $val2) => strlen($val) <= strlen($val2));
86+
8887
$n->addChild("1234");
8988
$n->addChild("12345678");
9089
$n->addChild("12");
@@ -102,9 +101,7 @@ public function testHasComparator()
102101
$n = new BstNode("12345");
103102
$this->assertFalse($n->hasComparator());
104103

105-
$n->setComparator(function($v, $v2){
106-
return true;
107-
});
104+
$n->setComparator(fn($v, $v2) => true);
108105
$this->assertTrue($n->hasComparator());
109106
}
110107
}

0 commit comments

Comments
 (0)