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

systemsdk / phpcpd / #12

13 Apr 2025 04:42PM UTC coverage: 75.711%. Remained the same
#12

push

DKravtsov
phpcpd 8.1.1 release. Available installation via composer.

692 of 914 relevant lines covered (75.71%)

3.7 hits per line

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

36.96
/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
 * @template T
13
 * @template S
14
 */
15
class PairList
16
{
17
    /**
18
     * Version used for serialization.
19
     */
20
    private int $serialVersionUID = 1;
21

22
    /**
23
     * The current size.
24
     */
25
    private int $size = 0;
26

27
    /**
28
     * The array used for storing the S.
29
     *
30
     * @var array<int, mixed>
31
     */
32
    private array $firstElements;
33

34
    /**
35
     * The array used for storing the T.
36
     *
37
     * @var array<int, mixed>
38
     */
39
    private array $secondElements;
40

41
    public function __construct(int $initialCapacity) // , $firstType, $secondType
42
    {
43
        if ($initialCapacity < 1) {
3✔
44
            $initialCapacity = 1;
×
45
        }
46

47
        $data = array_fill(0, $initialCapacity, null);
3✔
48
        $this->firstElements = $data;
3✔
49
        $this->secondElements = $data;
3✔
50
    }
51

52
    public function serialVersion(): int
53
    {
54
        return $this->serialVersionUID;
×
55
    }
56

57
    /**
58
     * Returns whether the list is empty.
59
     */
60
    public function isEmpty(): bool
61
    {
62
        return $this->size === 0;
×
63
    }
64

65
    /**
66
     * Returns the size of the list.
67
     */
68
    public function size(): int
69
    {
70
        return $this->size;
3✔
71
    }
72

73
    /**
74
     * Add the given pair to the list.
75
     *
76
     * @param S $first
77
     * @param T $second
78
     */
79
    public function add($first, $second): void
80
    {
81
        $this->firstElements[$this->size] = $first;
3✔
82
        $this->secondElements[$this->size] = $second;
3✔
83
        $this->size++;
3✔
84
    }
85

86
    /**
87
     * Adds all pairs from another list.
88
     */
89
    public function addAll(self $other): void
90
    {
91
        // we have to store this in a local var, as other.$this->size may change if
92
        // other == this
93
        $otherSize = $other->size;
×
94

95
        for ($i = 0; $i < $otherSize; $i++) {
×
96
            $this->firstElements[$this->size] = $other->firstElements[$i];
×
97
            $this->secondElements[$this->size] = $other->secondElements[$i];
×
98
            $this->size++;
×
99
        }
100
    }
101

102
    /**
103
     * Returns the first element at given index.
104
     *
105
     * @return S
106
     */
107
    public function getFirst(int $i)
108
    {
109
        $this->checkWithinBounds($i);
3✔
110

111
        return $this->firstElements[$i];
3✔
112
    }
113

114
    /**
115
     * Sets the first element at given index.
116
     *
117
     * @param S $value
118
     */
119
    public function setFirst(int $i, $value): void
120
    {
121
        $this->checkWithinBounds($i);
×
122
        $this->firstElements[$i] = $value;
×
123
    }
124

125
    /**
126
     * Returns the second element at given index.
127
     *
128
     * @return T
129
     */
130
    public function getSecond(int $i)
131
    {
132
        $this->checkWithinBounds($i);
3✔
133

134
        return $this->secondElements[$i];
3✔
135
    }
136

137
    /**
138
     * Sets the first element at given index.
139
     *
140
     * @param T $value
141
     */
142
    public function setSecond(int $i, $value): void
143
    {
144
        $this->checkWithinBounds($i);
×
145
        $this->secondElements[$i] = $value;
×
146
    }
147

148
    /**
149
     * Creates a new list containing all first elements.
150
     *
151
     * @return S[]
152
     */
153
    public function extractFirstList(): array
154
    {
155
        $result = [];
3✔
156

157
        for ($i = 0; $i < $this->size; $i++) {
3✔
158
            $result[] = $this->firstElements[$i];
3✔
159
        }
160

161
        return $result;
3✔
162
    }
163

164
    /**
165
     * Creates a new list containing all second elements.
166
     *
167
     * @return T[]
168
     */
169
    public function extractSecondList(): array
170
    {
171
        $result = [];
×
172

173
        for ($i = 0; $i < $this->size; $i++) {
×
174
            $result[] = $this->secondElements[$i];
×
175
        }
176

177
        return $result;
×
178
    }
179

180
    /**
181
     * Swaps the entries located at indexes $i and $j.
182
     */
183
    public function swapEntries(int $i, int $j): void
184
    {
185
        $tmp1 = $this->getFirst($i);
×
186
        $tmp2 = $this->getSecond($i);
×
187
        $this->setFirst($i, $this->getFirst($j));
×
188
        $this->setSecond($i, $this->getSecond($j));
×
189
        $this->setFirst($j, $tmp1);
×
190
        $this->setSecond($j, $tmp2);
×
191
    }
192

193
    /**
194
     * Clears this list.
195
     */
196
    public function clear(): void
197
    {
198
        $this->size = 0;
×
199
    }
200

201
    /**
202
     * Removes the last element of the list.
203
     */
204
    public function removeLast(): void
205
    {
206
        $this->size--;
×
207
    }
208

209
    public function hashCode(): int
210
    {
211
        $prime = 31;
×
212
        $hash = $this->size;
×
213
        $hash = $prime * $hash + crc32(serialize($this->firstElements));
×
214

215
        return $prime * $hash + crc32(serialize($this->secondElements));
×
216
    }
217

218
    /**
219
     * Checks whether the given <code>$i</code> is within the bounds. Throws an
220
     * exception otherwise.
221
     */
222
    private function checkWithinBounds(int $i): void
223
    {
224
        if ($i < 0 || $i >= $this->size) {
3✔
225
            throw new OutOfBoundsException('Out of bounds: ' . $i);
×
226
        }
227
    }
228
}
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

© 2025 Coveralls, Inc