Completed
Push — master ( 51fab9...63db47 )
by duan
02:00
created

Bucket::isNull()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Yiranzai\File;
4
5
use DateTime;
6
7
/**
8
 * Class Bucket
9
 * @package Yiranzai\Dht
10
 */
11
class Bucket
12
{
13
    /**
14
     * @var Node
15
     */
16
    protected $head;
17
18
    /**
19
     * Bucket constructor.
20
     * @param string        $key
21
     * @param string        $data
22
     * @param DateTime|null $date
23
     */
24 12
    public function __construct(string $key, string $data = null, DateTime $date = null)
25
    {
26 12
        $this->head = new Node($key, $data, $date);
0 ignored issues
show
Bug introduced by
It seems like $data can also be of type null; however, parameter $data of Yiranzai\File\Node::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

26
        $this->head = new Node($key, /** @scrutinizer ignore-type */ $data, $date);
Loading history...
27 12
    }
28
29
    /**
30
     * 链表有几个元素
31
     *
32
     * @return int
33
     */
34 3
    public function countNode(): int
35
    {
36 3
        $cur = $this->head;
37 3
        $i   = 1;
38 3
        while ($cur->next !== null) {
39 3
            ++$i;
40 3
            $cur = $cur->next;
41
        }
42 3
        return $i;
43
    }
44
45
    /**
46
     * 增加节点
47
     *
48
     * @param string        $key
49
     * @param string        $data
50
     * @param DateTime|null $date
51
     * @return Bucket
52
     */
53 3
    public function addNode(string $key, string $data = null, DateTime $date = null): Bucket
54
    {
55 3
        $cur = $this->head;
56 3
        while ($cur->next !== null) {
57 3
            $cur = $cur->next;
58
        }
59 3
        $new       = new Node($key, $data, $date);
0 ignored issues
show
Bug introduced by
It seems like $data can also be of type null; however, parameter $data of Yiranzai\File\Node::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

59
        $new       = new Node($key, /** @scrutinizer ignore-type */ $data, $date);
Loading history...
60 3
        $cur->next = $new;
61 3
        return $this;
62
    }
63
64
    /**
65
     * 增加节点
66
     *
67
     * @param string        $key
68
     * @param string        $data
69
     * @param DateTime|null $date
70
     * @return Bucket
71
     */
72 3
    public function putNode(string $key, string $data = null, DateTime $date = null): Bucket
73
    {
74 3
        $cur = $this->head;
75 3
        if ($cur->key === $key) {
76 3
            $cur->data = $data;
77 3
            return $this;
78
        }
79 3
        while ($cur->next !== null) {
80 3
            $cur = $cur->next;
81 3
            if ($cur->key === $key) {
82 3
                $cur->data = $data;
83 3
                return $this;
84
            }
85
        }
86 3
        $new       = new Node($key, $data, $date);
0 ignored issues
show
Bug introduced by
It seems like $data can also be of type null; however, parameter $data of Yiranzai\File\Node::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

86
        $new       = new Node($key, /** @scrutinizer ignore-type */ $data, $date);
Loading history...
87 3
        $cur->next = $new;
88 3
        return $this;
89
    }
90
91
    /**
92
     * 紧接着插在$noNum后
93
     *
94
     * @param string        $k
95
     * @param string        $index
96
     * @param string        $data
97
     * @param DateTime|null $date
98
     * @return Bucket
99
     */
100 3
    public function insertNode(string $k, string $index, string $data = null, DateTime $date = null): Bucket
101
    {
102 3
        $cur = $this->head;
103 3
        $new = new Node($k, $data, $date);
0 ignored issues
show
Bug introduced by
It seems like $data can also be of type null; however, parameter $data of Yiranzai\File\Node::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

103
        $new = new Node($k, /** @scrutinizer ignore-type */ $data, $date);
Loading history...
104 3
        if ($cur->key !== $index) {
105 3
            while ($cur->next !== null) {
106 3
                $cur = $cur->next;
107 3
                if ($cur->key === $index) {
108 3
                    break;
109
                }
110
            }
111
        }
112 3
        $new->next = $cur->next;
113 3
        $cur->next = $new;
114 3
        return $this;
115
    }
116
117
    /**
118
     * 删除第$no个节点
119
     *
120
     * @param string $key
121
     * @return Bucket
122
     */
123 9
    public function delNode(string $key): Bucket
124
    {
125 9
        $cur = $this->head;
126 9
        if ($cur->key === $key) {
127 9
            $this->head = $cur->next;
128
        }
129 9
        while ($cur->next !== null) {
130 3
            if ($cur->next->key === $key) {
131 3
                $cur->next = $cur->next->next;
132 3
                break;
133
            }
134 3
            $cur = $cur->next;
135
        }
136 9
        return $this;
137
    }
138
139
    /**
140
     * 遍历链表
141
     *
142
     * @return Bucket
143
     * @throws \Exception
144
     */
145 3
    public function showNode(): Bucket
146
    {
147 3
        $cur = $this->head;
148 3
        while ($cur->next !== null) {
149 3
            $cur = $cur->next;
150 3
            echo $cur->getData() . PHP_EOL;
151
        }
152 3
        return $this;
153
    }
154
155
    /**
156
     * 寻找某节点的值
157
     *
158
     * @param string $key
159
     * @return string|null
160
     * @throws \Exception
161
     */
162 3
    public function find(string $key): ?string
163
    {
164 3
        $node = $this->findNode($key);
165 3
        return $node === null ? null : $node->getData();
166
    }
167
168
    /**
169
     * 寻找节点
170
     *
171
     * @param string $key
172
     * @return Node|null
173
     */
174 9
    public function findNode(string $key): ?Node
175
    {
176 9
        $cur = $this->head;
177 9
        if ($cur->key === $key) {
178 9
            return $cur;
179
        }
180 3
        while ($cur->next !== null) {
181 3
            $cur = $cur->next;
182 3
            if ($cur->key === $key) {
183 3
                return $cur;
184
            }
185
        }
186 3
        return null;
187
    }
188
189
    /**
190
     * @return bool
191
     */
192 6
    public function isNull(): bool
193
    {
194 6
        return $this->head === null;
195
    }
196
}
197