• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemsdk / phpcpd / #4

30 Dec 2024 04:42PM UTC coverage: 75.789%. Remained the same
#4

push

DKravtsov
phpcpd 8.0.0 release. Made codebase refactoring. Updated requirements php 8.3, updated composer dependencies, updated tests to the PHPUnit 11. Updated dev environment to the php 8.4, Phing 3.0, added code quality tools: ecs, phpstan.

271 of 379 new or added lines in 20 files covered. (71.5%)

65 existing lines in 13 files now uncovered.

648 of 855 relevant lines covered (75.79%)

2.18 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

35.56
/src/Detector/Strategy/SuffixTree/PairList.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Systemsdk\PhpCPD\Detector\Strategy\SuffixTree;
6

7
use Systemsdk\PhpCPD\Exceptions\OutOfBoundsException;
8

9
/**
10
 * A list for storing pairs in a specific order.
11
 *
12
 * @author $Author: hummelb $
13
 *
14
 * @version $Rev: 51770 $
15
 *
16
 * @ConQAT.Rating GREEN Hash: 7459D6D0F59028B37DD23DD091BDCEEA
17
 *
18
 * @template T
19
 * @template S
20
 */
21
class PairList
22
{
23
    /**
24
     * Version used for serialization.
25
     *
26
     * @var int
27
     */
28
    private $serialVersionUID = 1;
29

30
    /**
31
     * The current size.
32
     *
33
     * @var int
34
     */
35
    private $size = 0;
36

37
    /**
38
     * The array used for storing the S.
39
     *
40
     * @var array<int, mixed>
41
     */
42
    private $firstElements;
43

44
    /**
45
     * The array used for storing the T.
46
     *
47
     * @var array<int, mixed>
48
     */
49
    private $secondElements;
50

51
    public function __construct(int $initialCapacity) // , $firstType, $secondType
52
    {
53
        if ($initialCapacity < 1) {
1✔
UNCOV
54
            $initialCapacity = 1;
×
55
        }
56
        $this->firstElements = array_fill(0, $initialCapacity, null);
1✔
57
        $this->secondElements = array_fill(0, $initialCapacity, null);
1✔
58
    }
59

60
    public function serialVersion(): int
61
    {
NEW
62
        return $this->serialVersionUID;
×
63
    }
64

65
    /**
66
     * Returns whether the list is empty.
67
     */
68
    public function isEmpty(): bool
69
    {
UNCOV
70
        return $this->size == 0;
×
71
    }
72

73
    /**
74
     * Returns the size of the list.
75
     */
76
    public function size(): int
77
    {
78
        return $this->size;
1✔
79
    }
80

81
    /**
82
     * Add the given pair to the list.
83
     *
84
     * @param S $first
85
     * @param T $second
86
     */
87
    public function add($first, $second): void
88
    {
89
        $this->firstElements[$this->size] = $first;
1✔
90
        $this->secondElements[$this->size] = $second;
1✔
91
        $this->size++;
1✔
92
    }
93

94
    /**
95
     * Adds all pairs from another list.
96
     */
97
    public function addAll(self $other): void
98
    {
99
        // we have to store this in a local var, as other.$this->size may change if
100
        // other == this
UNCOV
101
        $otherSize = $other->size;
×
102

103
        for ($i = 0; $i < $otherSize; $i++) {
×
NEW
104
            $this->firstElements[$this->size] = $other->firstElements[$i];
×
105
            $this->secondElements[$this->size] = $other->secondElements[$i];
×
106
            $this->size++;
×
107
        }
108
    }
109

110
    /**
111
     * Returns the first element at given index.
112
     *
113
     * @return S
114
     */
115
    public function getFirst(int $i)
116
    {
117
        $this->checkWithinBounds($i);
1✔
118

119
        return $this->firstElements[$i];
1✔
120
    }
121

122
    /**
123
     * Sets the first element at given index.
124
     *
125
     * @param S $value
126
     */
127
    public function setFirst(int $i, $value): void
128
    {
UNCOV
129
        $this->checkWithinBounds($i);
×
UNCOV
130
        $this->firstElements[$i] = $value;
×
131
    }
132

133
    /**
134
     * Returns the second element at given index.
135
     *
136
     * @return T
137
     */
138
    public function getSecond(int $i)
139
    {
140
        $this->checkWithinBounds($i);
1✔
141

142
        return $this->secondElements[$i];
1✔
143
    }
144

145
    /**
146
     * Sets the first element at given index.
147
     *
148
     * @param T $value
149
     */
150
    public function setSecond(int $i, $value): void
151
    {
UNCOV
152
        $this->checkWithinBounds($i);
×
UNCOV
153
        $this->secondElements[$i] = $value;
×
154
    }
155

156
    /**
157
     * Creates a new list containing all first elements.
158
     *
159
     * @return S[]
160
     */
161
    public function extractFirstList(): array
162
    {
163
        $result = [];
1✔
164

165
        for ($i = 0; $i < $this->size; $i++) {
1✔
166
            $result[] = $this->firstElements[$i];
1✔
167
        }
168

169
        return $result;
1✔
170
    }
171

172
    /**
173
     * Creates a new list containing all second elements.
174
     *
175
     * @return T[]
176
     */
177
    public function extractSecondList(): array
178
    {
UNCOV
179
        $result = [];
×
180

181
        for ($i = 0; $i < $this->size; $i++) {
×
UNCOV
182
            $result[] = $this->secondElements[$i];
×
183
        }
184

UNCOV
185
        return $result;
×
186
    }
187

188
    /**
189
     * Swaps the entries located at indexes $i and $j.
190
     */
191
    public function swapEntries(int $i, int $j): void
192
    {
UNCOV
193
        $tmp1 = $this->getFirst($i);
×
UNCOV
194
        $tmp2 = $this->getSecond($i);
×
UNCOV
195
        $this->setFirst($i, $this->getFirst($j));
×
UNCOV
196
        $this->setSecond($i, $this->getSecond($j));
×
197
        $this->setFirst($j, $tmp1);
×
198
        $this->setSecond($j, $tmp2);
×
199
    }
200

201
    /**
202
     * Clears this list.
203
     */
204
    public function clear(): void
205
    {
UNCOV
206
        $this->size = 0;
×
207
    }
208

209
    /**
210
     * Removes the last element of the list.
211
     */
212
    public function removeLast(): void
213
    {
214
        $this->size--;
×
215
    }
216

217
    public function hashCode(): int
218
    {
UNCOV
219
        $prime = 31;
×
NEW
220
        $hash = $this->size;
×
NEW
221
        $hash = $prime * $hash + crc32(serialize($this->firstElements));
×
222

UNCOV
223
        return $prime * $hash + crc32(serialize($this->secondElements));
×
224
    }
225

226
    /**
227
     * Checks whether the given <code>$i</code> is within the bounds. Throws an
228
     * exception otherwise.
229
     */
230
    private function checkWithinBounds(int $i): void
231
    {
232
        if ($i < 0 || $i >= $this->size) {
1✔
UNCOV
233
            throw new OutOfBoundsException('Out of bounds: ' . $i);
×
234
        }
235
    }
236
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc