Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like EnumSet often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use EnumSet, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class EnumSet implements Iterator, Countable |
||
17 | { |
||
18 | /** |
||
19 | * The classname of the Enumeration |
||
20 | * @var string |
||
21 | */ |
||
22 | private $enumeration; |
||
23 | |||
24 | /** |
||
25 | * Ordinal number of current iterator position |
||
26 | * @var int |
||
27 | */ |
||
28 | private $ordinal = 0; |
||
29 | |||
30 | /** |
||
31 | * Highest possible ordinal number |
||
32 | * @var int |
||
33 | */ |
||
34 | private $ordinalMax; |
||
35 | |||
36 | /** |
||
37 | * Integer or binary (little endian) bitset used to handle attached enumerations. |
||
38 | * @var int|string |
||
39 | */ |
||
40 | private $bitset; |
||
41 | |||
42 | /**#@+ |
||
43 | * Defined the private method names to be called dependend of |
||
44 | * how the bitset type was set too. |
||
45 | * ... Integer or binary bitset. |
||
46 | * ... *Int or *Bin method |
||
47 | * |
||
48 | * @var string |
||
49 | */ |
||
50 | private $fnDoRewind = 'doRewindBin'; |
||
51 | private $fnDoCount = 'doCountBin'; |
||
52 | private $fnDoGetOrdinals = 'doGetOrdinalsBin'; |
||
53 | private $fnDoGetBit = 'doGetBitBin'; |
||
54 | private $fnDoSetBit = 'doSetBitBin'; |
||
55 | private $fnDoUnsetBit = 'doUnsetBitBin'; |
||
56 | private $fnDoGetBinaryBitsetLe = 'doGetBinaryBitsetLeBin'; |
||
57 | private $fnDoSetBinaryBitsetLe = 'doSetBinaryBitsetLeBin'; |
||
58 | /**#@-*/ |
||
59 | |||
60 | /** |
||
61 | * Constructor |
||
62 | * |
||
63 | * @param string $enumeration The classname of the enumeration |
||
64 | * @throws InvalidArgumentException |
||
65 | */ |
||
66 | 50 | public function __construct($enumeration) |
|
67 | { |
||
68 | 50 | if (!is_subclass_of($enumeration, Enum::class)) { |
|
|
|||
69 | 1 | throw new InvalidArgumentException(sprintf( |
|
70 | 1 | "%s can handle subclasses of '%s' only", |
|
71 | 1 | static::class, |
|
72 | 1 | Enum::class |
|
73 | )); |
||
74 | } |
||
75 | |||
76 | 49 | $this->enumeration = $enumeration; |
|
77 | 49 | $this->ordinalMax = count($enumeration::getConstants()); |
|
78 | |||
79 | 49 | if ($this->ordinalMax > PHP_INT_SIZE * 8) { |
|
80 | // init binary bitset with zeros |
||
81 | 9 | $this->bitset = str_repeat("\0", ceil($this->ordinalMax / 8)); |
|
82 | } else { |
||
83 | // init integer bitset |
||
84 | 40 | $this->bitset = 0; |
|
85 | 40 | $this->fnDoRewind = 'doRewindInt'; |
|
86 | 40 | $this->fnDoCount = 'doCountInt'; |
|
87 | 40 | $this->fnDoGetOrdinals = 'doGetOrdinalsInt'; |
|
88 | 40 | $this->fnDoGetBit = 'doGetBitInt'; |
|
89 | 40 | $this->fnDoSetBit = 'doSetBitInt'; |
|
90 | 40 | $this->fnDoUnsetBit = 'doUnsetBitInt'; |
|
91 | 40 | $this->fnDoGetBinaryBitsetLe = 'doGetBinaryBitsetLeInt'; |
|
92 | 40 | $this->fnDoSetBinaryBitsetLe = 'doSetBinaryBitsetLeInt'; |
|
93 | } |
||
94 | 49 | } |
|
95 | |||
96 | /** |
||
97 | * Get the classname of the enumeration |
||
98 | * @return string |
||
99 | */ |
||
100 | 1 | public function getEnumeration() |
|
104 | |||
105 | /** |
||
106 | * Attach a new enumerator or overwrite an existing one |
||
107 | * @param Enum|null|boolean|int|float|string $enumerator |
||
108 | * @return void |
||
109 | * @throws InvalidArgumentException On an invalid given enumerator |
||
110 | */ |
||
111 | 34 | public function attach($enumerator) |
|
116 | |||
117 | /** |
||
118 | * Detach the given enumerator |
||
119 | * @param Enum|null|boolean|int|float|string $enumerator |
||
120 | * @return void |
||
121 | * @throws InvalidArgumentException On an invalid given enumerator |
||
122 | */ |
||
123 | 6 | public function detach($enumerator) |
|
128 | |||
129 | /** |
||
130 | * Test if the given enumerator was attached |
||
131 | * @param Enum|null|boolean|int|float|string $enumerator |
||
132 | * @return boolean |
||
133 | */ |
||
134 | 9 | public function contains($enumerator) |
|
139 | |||
140 | /* Iterator */ |
||
141 | |||
142 | /** |
||
143 | * Get the current enumerator |
||
144 | * @return Enum|null Returns the current enumerator or NULL on an invalid iterator position |
||
145 | */ |
||
146 | 9 | public function current() |
|
155 | |||
156 | /** |
||
157 | * Get the ordinal number of the current iterator position |
||
158 | * @return int |
||
159 | */ |
||
160 | 6 | public function key() |
|
164 | |||
165 | /** |
||
166 | * Go to the next valid iterator position. |
||
167 | * If no valid iterator position is found the iterator position will be the last possible + 1. |
||
168 | * @return void |
||
169 | */ |
||
170 | 15 | public function next() |
|
179 | |||
180 | /** |
||
181 | * Go to the first valid iterator position. |
||
182 | * If no valid iterator position was found the iterator position will be 0. |
||
183 | * @return void |
||
184 | * @see doRewindBin |
||
185 | * @see doRewindInt |
||
186 | */ |
||
187 | 9 | public function rewind() |
|
191 | |||
192 | /** |
||
193 | * Go to the first valid iterator position. |
||
194 | * If no valid iterator position was found the iterator position will be 0. |
||
195 | * |
||
196 | * This is the binary bitset implementation. |
||
197 | * |
||
198 | * @return void |
||
199 | * @see rewind |
||
200 | * @see doRewindInt |
||
201 | */ |
||
202 | 3 | private function doRewindBin() |
|
203 | { |
||
204 | 3 | if (trim($this->bitset, "\0") !== '') { |
|
205 | 3 | $this->ordinal = -1; |
|
206 | 3 | $this->next(); |
|
207 | } else { |
||
208 | $this->ordinal = 0; |
||
209 | } |
||
210 | 3 | } |
|
211 | |||
212 | /** |
||
213 | * Go to the first valid iterator position. |
||
214 | * If no valid iterator position was found the iterator position will be 0. |
||
215 | * |
||
216 | * This is the binary bitset implementation. |
||
217 | * |
||
218 | * @return void |
||
219 | * @see rewind |
||
220 | * @see doRewindBin |
||
221 | */ |
||
222 | 6 | private function doRewindInt() |
|
223 | { |
||
224 | 6 | if ($this->bitset) { |
|
225 | 6 | $this->ordinal = -1; |
|
226 | 6 | $this->next(); |
|
227 | } else { |
||
228 | 1 | $this->ordinal = 0; |
|
229 | } |
||
230 | 6 | } |
|
231 | |||
232 | /** |
||
233 | * Test if the iterator in a valid state |
||
234 | * @return boolean |
||
235 | */ |
||
236 | 10 | public function valid() |
|
240 | |||
241 | /* Countable */ |
||
242 | |||
243 | /** |
||
244 | * Count the number of elements |
||
245 | * |
||
246 | * @return int |
||
247 | * @see doCountBin |
||
248 | * @see doCountInt |
||
249 | */ |
||
250 | 9 | public function count() |
|
254 | |||
255 | /** |
||
256 | * Count the number of elements. |
||
257 | * |
||
258 | * This is the binary bitset implementation. |
||
259 | * |
||
260 | * @return int |
||
261 | * @see count |
||
262 | * @see doCountInt |
||
263 | */ |
||
264 | 4 | private function doCountBin() |
|
265 | { |
||
266 | 4 | $count = 0; |
|
267 | 4 | $byteLen = strlen($this->bitset); |
|
268 | 4 | for ($bytePos = 0; $bytePos < $byteLen; ++$bytePos) { |
|
269 | 4 | if ($this->bitset[$bytePos] === "\0") { |
|
270 | // fast skip null byte |
||
271 | 4 | continue; |
|
272 | } |
||
273 | |||
274 | 4 | $byteOrd = ord($this->bitset[$bytePos]); |
|
275 | 4 | for ($bitPos = 0; $bitPos < 8; ++$bitPos) { |
|
276 | 4 | if ($byteOrd & (1 << $bitPos)) { |
|
277 | 4 | ++$count; |
|
278 | } |
||
279 | } |
||
280 | } |
||
281 | 4 | return $count; |
|
282 | } |
||
283 | |||
284 | /** |
||
285 | * Count the number of elements. |
||
286 | * |
||
287 | * This is the integer bitset implementation. |
||
288 | * |
||
289 | * @return int |
||
290 | * @see count |
||
291 | * @see doCountBin |
||
292 | */ |
||
293 | 5 | private function doCountInt() |
|
294 | { |
||
295 | 5 | $count = 0; |
|
296 | 5 | $ord = 0; |
|
297 | 5 | while ($ord !== $this->ordinalMax) { |
|
298 | 4 | if ($this->bitset & (1 << $ord++)) { |
|
299 | 4 | ++$count; |
|
300 | } |
||
301 | } |
||
302 | 5 | return $count; |
|
303 | } |
||
304 | |||
305 | /** |
||
306 | * Check if this EnumSet is the same as other |
||
307 | * @param EnumSet $other |
||
308 | * @return bool |
||
309 | */ |
||
310 | 3 | public function isEqual(EnumSet $other) |
|
315 | |||
316 | /** |
||
317 | * Check if this EnumSet is a subset of other |
||
318 | * @param EnumSet $other |
||
319 | * @return bool |
||
320 | */ |
||
321 | 4 | View Code Duplication | public function isSubset(EnumSet $other) |
329 | |||
330 | /** |
||
331 | * Check if this EnumSet is a superset of other |
||
332 | * @param EnumSet $other |
||
333 | * @return bool |
||
334 | */ |
||
335 | 4 | View Code Duplication | public function isSuperset(EnumSet $other) |
343 | |||
344 | /** |
||
345 | * Produce a new set with enumerators from both this and other (this | other) |
||
346 | * @param EnumSet ...$other Other EnumSet(s) of the same enumeration to produce the union |
||
347 | * @return EnumSet |
||
348 | */ |
||
349 | 2 | View Code Duplication | public function union(EnumSet $other) |
350 | { |
||
351 | 2 | $bitset = $this->bitset; |
|
352 | 2 | foreach (func_get_args() as $other) { |
|
353 | 2 | if (!$other instanceof self || $this->enumeration !== $other->enumeration) { |
|
354 | 1 | throw new InvalidArgumentException(sprintf( |
|
355 | 1 | "Others should be an instance of %s of the same enumeration as this %s", |
|
356 | 1 | __CLASS__, |
|
357 | 1 | $this->enumeration |
|
358 | )); |
||
359 | } |
||
360 | |||
361 | 1 | $bitset |= $other->bitset; |
|
362 | } |
||
363 | |||
364 | 1 | $clone = clone $this; |
|
365 | 1 | $clone->bitset = $bitset; |
|
366 | 1 | return $clone; |
|
367 | } |
||
368 | |||
369 | /** |
||
370 | * Produce a new set with enumerators common to both this and other (this & other) |
||
371 | * @param EnumSet ...$other Other EnumSet(s) of the same enumeration to produce the union |
||
372 | * @return EnumSet |
||
373 | */ |
||
374 | 2 | View Code Duplication | public function intersect(EnumSet $other) |
375 | { |
||
376 | 2 | $bitset = $this->bitset; |
|
377 | 2 | foreach (func_get_args() as $other) { |
|
378 | 2 | if (!$other instanceof self || $this->enumeration !== $other->enumeration) { |
|
379 | 1 | throw new InvalidArgumentException(sprintf( |
|
380 | 1 | "Others should be an instance of %s of the same enumeration as this %s", |
|
381 | 1 | __CLASS__, |
|
382 | 1 | $this->enumeration |
|
383 | )); |
||
384 | } |
||
385 | |||
386 | 1 | $bitset &= $other->bitset; |
|
387 | } |
||
388 | |||
389 | 1 | $clone = clone $this; |
|
390 | 1 | $clone->bitset = $bitset; |
|
391 | 1 | return $clone; |
|
392 | } |
||
393 | |||
394 | /** |
||
395 | * Produce a new set with enumerators in this but not in other (this - other) |
||
396 | * @param EnumSet ...$other Other EnumSet(s) of the same enumeration to produce the union |
||
397 | * @return EnumSet |
||
398 | */ |
||
399 | 2 | View Code Duplication | public function diff(EnumSet $other) |
400 | { |
||
401 | 2 | $bitset = $other->bitset; |
|
402 | 2 | foreach (func_get_args() as $other) { |
|
403 | 2 | if (!$other instanceof self || $this->enumeration !== $other->enumeration) { |
|
404 | 1 | throw new InvalidArgumentException(sprintf( |
|
405 | 1 | "Others should be an instance of %s of the same enumeration as this %s", |
|
406 | 1 | __CLASS__, |
|
407 | 1 | $this->enumeration |
|
408 | )); |
||
409 | } |
||
410 | |||
411 | 1 | $bitset |= $other->bitset; |
|
412 | } |
||
413 | |||
414 | 1 | $clone = clone $this; |
|
415 | 1 | $clone->bitset = $this->bitset & ~$bitset; |
|
416 | 1 | return $clone; |
|
417 | } |
||
418 | |||
419 | /** |
||
420 | * Produce a new set with enumerators in either this and other but not in both (this ^ (other | other)) |
||
421 | * @param EnumSet ...$other Other EnumSet(s) of the same enumeration to produce the union |
||
422 | * @return EnumSet |
||
423 | */ |
||
424 | 2 | View Code Duplication | public function symDiff(EnumSet $other) |
425 | { |
||
426 | 2 | $bitset = $other->bitset; |
|
427 | 2 | foreach (func_get_args() as $other) { |
|
428 | 2 | if (!$other instanceof self || $this->enumeration !== $other->enumeration) { |
|
429 | 1 | throw new InvalidArgumentException(sprintf( |
|
430 | 1 | "Others should be an instance of %s of the same enumeration as this %s", |
|
431 | 1 | __CLASS__, |
|
432 | 1 | $this->enumeration |
|
433 | )); |
||
434 | } |
||
435 | |||
436 | 1 | $bitset |= $other->bitset; |
|
437 | } |
||
438 | |||
439 | 1 | $clone = clone $this; |
|
440 | 1 | $clone->bitset = $this->bitset ^ $bitset; |
|
441 | 1 | return $clone; |
|
442 | } |
||
443 | |||
444 | /** |
||
445 | * Get ordinal numbers of the defined enumerators as array |
||
446 | * @return int[] |
||
447 | */ |
||
448 | 12 | public function getOrdinals() |
|
452 | |||
453 | /** |
||
454 | * Get ordinal numbers of the defined enumerators as array. |
||
455 | * |
||
456 | * This is the binary bitset implementation. |
||
457 | * |
||
458 | * @return int[] |
||
459 | * @see getOrdinals |
||
460 | * @see goGetOrdinalsInt |
||
461 | */ |
||
462 | private function doGetOrdinalsBin() |
||
463 | { |
||
464 | $ordinals = array(); |
||
465 | $byteLen = strlen($this->bitset); |
||
466 | for ($bytePos = 0; $bytePos < $byteLen; ++$bytePos) { |
||
467 | if ($this->bitset[$bytePos] === "\0") { |
||
468 | // fast skip null byte |
||
469 | continue; |
||
470 | } |
||
471 | |||
472 | $byteOrd = ord($this->bitset[$bytePos]); |
||
473 | for ($bitPos = 0; $bitPos < 8; ++$bitPos) { |
||
474 | if ($byteOrd & (1 << $bitPos)) { |
||
475 | $ordinals[] = $bytePos * 8 + $bitPos; |
||
476 | } |
||
477 | } |
||
478 | } |
||
479 | return $ordinals; |
||
480 | } |
||
481 | |||
482 | /** |
||
483 | * Get ordinal numbers of the defined enumerators as array. |
||
484 | * |
||
485 | * This is the integer bitset implementation. |
||
486 | * |
||
487 | * @return int[] |
||
488 | * @see getOrdinals |
||
489 | * @see doGetOrdinalsBin |
||
490 | */ |
||
491 | 12 | private function doGetOrdinalsInt() |
|
492 | { |
||
493 | 12 | $ordinals = array(); |
|
494 | 12 | for ($ord = 0; $ord < $this->ordinalMax; ++$ord) { |
|
495 | 12 | if ($this->bitset & (1 << $ord)) { |
|
496 | 12 | $ordinals[] = $ord; |
|
497 | } |
||
498 | } |
||
499 | 12 | return $ordinals; |
|
500 | } |
||
501 | |||
502 | /** |
||
503 | * Get values of the defined enumerators as array |
||
504 | * @return null[]|bool[]|int[]|float[]|string[] |
||
505 | */ |
||
506 | 6 | public function getValues() |
|
507 | { |
||
508 | 6 | $enumeration = $this->enumeration; |
|
509 | 6 | $values = array(); |
|
510 | 6 | foreach ($this->getOrdinals() as $ord) { |
|
511 | 6 | $values[] = $enumeration::byOrdinal($ord)->getValue(); |
|
512 | } |
||
513 | 6 | return $values; |
|
514 | } |
||
515 | |||
516 | /** |
||
517 | * Get names of the defined enumerators as array |
||
518 | * @return string[] |
||
519 | */ |
||
520 | 2 | public function getNames() |
|
521 | { |
||
522 | 2 | $enumeration = $this->enumeration; |
|
523 | 2 | $names = array(); |
|
524 | 2 | foreach ($this->getOrdinals() as $ord) { |
|
525 | 2 | $names[] = $enumeration::byOrdinal($ord)->getName(); |
|
526 | } |
||
527 | 2 | return $names; |
|
528 | } |
||
529 | |||
530 | /** |
||
531 | * Get the defined enumerators as array |
||
532 | * @return Enum[] |
||
533 | */ |
||
534 | 2 | public function getEnumerators() |
|
535 | { |
||
536 | 2 | $enumeration = $this->enumeration; |
|
537 | 2 | $enumerators = array(); |
|
538 | 2 | foreach ($this->getOrdinals() as $ord) { |
|
539 | 2 | $enumerators[] = $enumeration::byOrdinal($ord); |
|
540 | } |
||
541 | 2 | return $enumerators; |
|
542 | } |
||
543 | |||
544 | /** |
||
545 | * Get binary bitset in little-endian order |
||
546 | * |
||
547 | * @return string |
||
548 | */ |
||
549 | 4 | public function getBinaryBitsetLe() |
|
553 | |||
554 | /** |
||
555 | * Get binary bitset in little-endian order. |
||
556 | * |
||
557 | * This is the binary bitset implementation. |
||
558 | * |
||
559 | * @return string |
||
560 | */ |
||
561 | 3 | private function doGetBinaryBitsetLeBin() |
|
565 | |||
566 | /** |
||
567 | * Get binary bitset in little-endian order. |
||
568 | * |
||
569 | * This is the integer bitset implementation. |
||
570 | * |
||
571 | * @return string |
||
572 | */ |
||
573 | 1 | private function doGetBinaryBitsetLeInt() |
|
574 | { |
||
575 | 1 | $bin = pack(PHP_INT_SIZE === 8 ? 'P' : 'V', $this->bitset); |
|
576 | 1 | return substr($bin, 0, ceil($this->ordinalMax / 8)); |
|
577 | } |
||
578 | |||
579 | /** |
||
580 | * Set binary bitset in little-endian order |
||
581 | * |
||
582 | * NOTE: It resets the current position of the iterator |
||
583 | * |
||
584 | * @param string $bitset |
||
585 | * @return void |
||
586 | * @throws InvalidArgumentException On a non string is given as Parameter |
||
587 | */ |
||
588 | 8 | public function setBinaryBitsetLe($bitset) |
|
599 | |||
600 | /** |
||
601 | * Set binary bitset in little-endian order |
||
602 | * |
||
603 | * NOTE: It resets the current position of the iterator |
||
604 | * |
||
605 | * @param string $bitset |
||
606 | * @return void |
||
607 | * @throws InvalidArgumentException On a non string is given as Parameter |
||
608 | */ |
||
609 | 4 | private function doSetBinaryBitsetLeBin($bitset) |
|
610 | { |
||
611 | 4 | $size = strlen($this->bitset); |
|
612 | 4 | $sizeIn = strlen($bitset); |
|
613 | |||
614 | 4 | if ($sizeIn < $size) { |
|
615 | // add "\0" if the given bitset is not long enough |
||
616 | 1 | $bitset .= str_repeat("\0", $size - $sizeIn); |
|
617 | 3 | } elseif ($sizeIn > $size) { |
|
618 | if (trim(substr($bitset, $size), "\0") !== '') { |
||
619 | throw new InvalidArgumentException('Out-Of-Range bits detected'); |
||
620 | } |
||
621 | $bitset = substr($bitset, 0, $size); |
||
622 | } |
||
623 | |||
624 | // truncate out-of-range bits of last byte |
||
625 | 4 | $lastByteMaxOrd = $this->ordinalMax % 8; |
|
626 | 4 | if ($lastByteMaxOrd !== 0) { |
|
627 | 4 | $lastByte = $bitset[$size - 1]; |
|
628 | 4 | $lastByteExpected = chr((1 << $lastByteMaxOrd) - 1) & $lastByte; |
|
629 | 4 | if ($lastByte !== $lastByteExpected) { |
|
630 | 1 | throw new InvalidArgumentException('Out-Of-Range bits detected'); |
|
631 | } |
||
632 | |||
633 | 3 | $this->bitset = substr($bitset, 0, -1) . $lastByteExpected; |
|
634 | } |
||
635 | |||
636 | 3 | $this->bitset = $bitset; |
|
637 | 3 | } |
|
638 | |||
639 | /** |
||
640 | * Set binary bitset in little-endian order |
||
641 | * |
||
642 | * NOTE: It resets the current position of the iterator |
||
643 | * |
||
644 | * @param string $bitset |
||
645 | * @return void |
||
646 | * @throws InvalidArgumentException On a non string is given as Parameter |
||
647 | */ |
||
648 | 3 | private function doSetBinaryBitsetLeInt($bitset) |
|
649 | { |
||
650 | 3 | $len = strlen($bitset); |
|
651 | 3 | $int = 0; |
|
652 | 3 | for ($i = 0; $i < $len; ++$i) { |
|
653 | 3 | $ord = ord($bitset[$i]); |
|
654 | |||
655 | 3 | if ($ord && $i > PHP_INT_SIZE) { |
|
656 | throw new InvalidArgumentException('Out-Of-Range bits detected'); |
||
657 | } |
||
658 | |||
659 | 3 | $int |= $ord << (8 * $i); |
|
660 | } |
||
661 | |||
662 | 3 | if ($int & (~0 << $this->ordinalMax)) { |
|
663 | 2 | throw new InvalidArgumentException('Out-Of-Range bits detected'); |
|
664 | } |
||
665 | |||
666 | 1 | $this->bitset = $int; |
|
667 | 1 | } |
|
668 | |||
669 | /** |
||
670 | * Get binary bitset in big-endian order |
||
671 | * |
||
672 | * @return string |
||
673 | */ |
||
674 | 1 | public function getBinaryBitsetBe() |
|
678 | |||
679 | /** |
||
680 | * Set binary bitset in big-endian order |
||
681 | * |
||
682 | * NOTE: It resets the current position of the iterator |
||
683 | * |
||
684 | * @param string $bitset |
||
685 | * @return void |
||
686 | * @throws InvalidArgumentException On a non string is given as Parameter |
||
687 | */ |
||
688 | 2 | public function setBinaryBitsetBe($bitset) |
|
695 | |||
696 | /** |
||
697 | * Get a bit at the given ordinal number |
||
698 | * |
||
699 | * @param int $ordinal Ordinal number of bit to get |
||
700 | * @return boolean |
||
701 | */ |
||
702 | public function getBit($ordinal) |
||
710 | |||
711 | /** |
||
712 | * Get a bit at the given ordinal number. |
||
713 | * |
||
714 | * This is the binary bitset implementation. |
||
715 | * |
||
716 | * @param int $ordinal Ordinal number of bit to get |
||
717 | * @return boolean |
||
718 | * @see getBit |
||
719 | * @see doGetBitInt |
||
720 | */ |
||
721 | 6 | private function doGetBitBin($ordinal) |
|
725 | |||
726 | /** |
||
727 | * Get a bit at the given ordinal number. |
||
728 | * |
||
729 | * This is the integer bitset implementation. |
||
730 | * |
||
731 | * @param int $ordinal Ordinal number of bit to get |
||
732 | * @return boolean |
||
733 | * @see getBit |
||
734 | * @see doGetBitBin |
||
735 | */ |
||
736 | 13 | private function doGetBitInt($ordinal) |
|
740 | |||
741 | /** |
||
742 | * Set a bit at the given ordinal number |
||
743 | * |
||
744 | * @param int $ordinal Ordnal number of bit to set |
||
745 | * @param bool $bit The bit to set |
||
746 | * @return void |
||
747 | * @see doSetBitBin |
||
748 | * @see doSetBitInt |
||
749 | * @see doUnsetBin |
||
750 | * @see doUnsetInt |
||
751 | */ |
||
752 | public function setBit($ordinal, $bit) |
||
753 | { |
||
754 | if ($ordinal < 0 || $ordinal > $this->ordinalMax) { |
||
755 | throw new InvalidArgumentException("Ordinal number must be between 0 and {$this->ordinalMax}"); |
||
756 | } |
||
757 | |||
758 | if ($bit) { |
||
759 | $this->{$this->fnDoSetBit}($ordinal); |
||
760 | } else { |
||
761 | $this->{$this->fnDoUnsetBit}($ordinal); |
||
762 | } |
||
763 | } |
||
764 | |||
765 | /** |
||
766 | * Set a bit at the given ordinal number. |
||
767 | * |
||
768 | * This is the binary bitset implementation. |
||
769 | * |
||
770 | * @param int $ordinal Ordnal number of bit to set |
||
771 | * @return void |
||
772 | * @see setBit |
||
773 | * @see doSetBitInt |
||
774 | */ |
||
775 | 3 | private function doSetBitBin($ordinal) |
|
780 | |||
781 | /** |
||
782 | * Set a bit at the given ordinal number. |
||
783 | * |
||
784 | * This is the binary bitset implementation. |
||
785 | * |
||
786 | * @param int $ordinal Ordnal number of bit to set |
||
787 | * @return void |
||
788 | * @see setBit |
||
789 | * @see doSetBitBin |
||
790 | */ |
||
791 | 31 | private function doSetBitInt($ordinal) |
|
795 | |||
796 | /** |
||
797 | * Unset a bit at the given ordinal number. |
||
798 | * |
||
799 | * This is the binary bitset implementation. |
||
800 | * |
||
801 | * @param int $ordinal Ordinal number of bit to unset |
||
802 | * @return void |
||
803 | * @see setBit |
||
804 | * @see doUnsetBitInt |
||
805 | */ |
||
806 | 2 | private function doUnsetBitBin($ordinal) |
|
811 | |||
812 | /** |
||
813 | * Unset a bit at the given ordinal number. |
||
814 | * |
||
815 | * This is the integer bitset implementation. |
||
816 | * |
||
817 | * @param int $ordinal Ordinal number of bit to unset |
||
818 | * @return void |
||
819 | * @see setBit |
||
820 | * @see doUnsetBitBin |
||
821 | */ |
||
822 | 4 | private function doUnsetBitInt($ordinal) |
|
826 | } |
||
827 |