33
33
final class BitArray
34
34
{
35
35
/**
36
- * @var mixed[]|mixed| int[]|null
36
+ * @var mixed[]|int[]|null
37
37
*/
38
38
private $ bits ;
39
39
/**
@@ -56,7 +56,10 @@ public function __construct($bits = [], $size = 0)
56
56
}
57
57
}
58
58
59
- private static function makeArray ($ size )
59
+ /**
60
+ * @psalm-return array<empty, empty>
61
+ */
62
+ private static function makeArray ($ size ): array
60
63
{
61
64
return [];
62
65
}
@@ -74,7 +77,7 @@ public function getSizeInBytes()
74
77
/**
75
78
* Sets bit i.
76
79
*
77
- * @param bit $i to set
80
+ * @param int $i bit to set
78
81
*/
79
82
public function set ($ i ): void
80
83
{
@@ -85,7 +88,7 @@ public function set($i): void
85
88
/**
86
89
* Flips bit i.
87
90
*
88
- * @param bit $i to set
91
+ * @param int $i bit to set
89
92
*/
90
93
public function flip ($ i ): void
91
94
{
@@ -94,9 +97,9 @@ public function flip($i): void
94
97
}
95
98
96
99
/**
97
- * @param first $from bit to check
100
+ * @param int $from first bit to check
98
101
*
99
- * @return index of first bit that is set, starting from the given index, or size if none are set
102
+ * @return int index of first bit that is set, starting from the given index, or size if none are set
100
103
* at or beyond this given index
101
104
* @see #getNextUnset(int)
102
105
*/
@@ -121,9 +124,9 @@ public function getNextSet($from)
121
124
}
122
125
123
126
/**
124
- * @param index $from to start looking for unset bit
127
+ * @param int $from index to start looking for unset bit
125
128
*
126
- * @return index of next unset bit, or {@code size} if none are unset until the end
129
+ * @return int index of next unset bit, or {@code size} if none are unset until the end
127
130
* @see #getNextSet(int)
128
131
*/
129
132
public function getNextUnset ($ from )
@@ -149,8 +152,8 @@ public function getNextUnset($from)
149
152
/**
150
153
* Sets a block of 32 bits, starting at bit i.
151
154
*
152
- * @param first $i bit to set
153
- * @param the $newBits new value of the next 32 bits. Note again that the least-significant bit
155
+ * @param int $i first bit to set
156
+ * @param int $newBits the new value of the next 32 bits. Note again that the least-significant bit
154
157
* corresponds to bit i, the next-least-significant to i+1, and so on.
155
158
*/
156
159
public function setBulk ($ i , $ newBits ): void
@@ -161,8 +164,10 @@ public function setBulk($i, $newBits): void
161
164
/**
162
165
* Sets a range of bits.
163
166
*
164
- * @param start $start of range, inclusive.
165
- * @param end $end of range, exclusive
167
+ * @param int $start start of range, inclusive.
168
+ * @param int $end end of range, exclusive
169
+ *
170
+ * @return void
166
171
*/
167
172
public function setRange ($ start , $ end )
168
173
{
@@ -205,12 +210,13 @@ public function clear(): void
205
210
/**
206
211
* Efficient method to check if a range of bits is set, or not set.
207
212
*
208
- * @param start $ start of range, inclusive.
209
- * @param end $end of range, exclusive
210
- * @param if $value true, checks that bits in range are set, otherwise checks that they are not set
213
+ * @param int $start start of range, inclusive.
214
+ * @param int $ end end of range, exclusive
215
+ * @param bool $value if true, checks that bits in range are set, otherwise checks that they are not set
211
216
*
212
- * @return true iff all bits are set or not set in range, according to value argument
213
- * @throws InvalidArgumentException if end is less than or equal to start
217
+ * @return bool iff all bits are set or not set in range, according to value argument
218
+ *
219
+ * @throws \InvalidArgumentException if end is less than or equal to start
214
220
*/
215
221
public function isRange ($ start , $ end , $ value ): bool
216
222
{
@@ -251,10 +257,10 @@ public function isRange($start, $end, $value): bool
251
257
* least-significant. For example, appending 6 bits from 0x000001E will append the bits
252
258
* 0, 1, 1, 1, 1, 0 in that order.
253
259
*
254
- * @param $value {@code int} containing bits to append
255
- * @param bits $numBits from value to append
260
+ * @param int $value {@code int} containing bits to append
261
+ * @param int $numBits bits from value to append
256
262
*/
257
- public function appendBits ($ value , $ numBits )
263
+ public function appendBits ($ value , $ numBits ): void
258
264
{
259
265
if ($ numBits < 0 || $ numBits > 32 ) {
260
266
throw new \InvalidArgumentException ("Num bits must be between 0 and 32 " );
@@ -274,7 +280,7 @@ private function ensureCapacity($size): void
274
280
}
275
281
}
276
282
277
- public function appendBit ($ bit ): void
283
+ public function appendBit (bool $ bit ): void
278
284
{
279
285
$ this ->ensureCapacity ($ this ->size + 1 );
280
286
if ($ bit ) {
@@ -292,7 +298,7 @@ public function appendBitArray($other): void
292
298
}
293
299
}
294
300
295
- public function _xor ($ other )
301
+ public function _xor ($ other ): void
296
302
{
297
303
if ((is_countable ($ this ->bits ) ? count ($ this ->bits ) : 0 ) !== (is_countable ($ other ->bits ) ? count ($ other ->bits ) : 0 )) {
298
304
throw new \InvalidArgumentException ("Sizes don't match " );
@@ -307,11 +313,11 @@ public function _xor($other)
307
313
308
314
/**
309
315
*
310
- * @param first $bitOffset bit to start writing
316
+ * @param int $bitOffset first bit to start writing
311
317
* @param array $array to write into. Bytes are written most-significant byte first. This is the opposite
312
318
* of the internal representation, which is exposed by {@link #getBitArray()}
313
- * @param position $offset in array to start writing
314
- * @param how $numBytes many bytes to write
319
+ * @param int $offset position in array to start writing
320
+ * @param int $numBytes how many bytes to write
315
321
*/
316
322
public function toBytes ($ bitOffset , array &$ array , $ offset , $ numBytes ): void
317
323
{
@@ -329,21 +335,23 @@ public function toBytes($bitOffset, array &$array, $offset, $numBytes): void
329
335
330
336
/**
331
337
* @param $i ; bit to get
338
+ * @param float|int first $i
332
339
*
333
- * @return true iff bit i is set
340
+ * @return bool iff bit i is set
334
341
*/
335
- public function get ($ i ): bool
342
+ public function get (int | float $ i ): bool
336
343
{
337
344
$ key = (int )($ i / 32 );
338
345
339
346
return ($ this ->bits [$ key ] & (1 << ($ i & 0x1F ))) != 0 ;
340
347
}
341
348
342
349
/**
343
- * @return array underlying array of ints. The first element holds the first 32 bits, and the least
344
- * significant bit is bit 0.
350
+ * @return (int|mixed)[]|null underlying array of ints. The first element holds the first 32 bits, and the least significant bit is bit 0.
351
+ *
352
+ * @psalm-return array<int|mixed>|null
345
353
*/
346
- public function getBitArray ()
354
+ public function getBitArray (): array | null
347
355
{
348
356
return $ this ->bits ;
349
357
}
@@ -405,7 +413,7 @@ public function hashCode()
405
413
return 31 * $ this ->size + hashCode ($ this ->bits );
406
414
}
407
415
408
- public function toString ()
416
+ public function toString (): string
409
417
{
410
418
$ result = '' ;
411
419
for ($ i = 0 ; $ i < $ this ->size ; $ i ++) {
0 commit comments