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 BitArray 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 BitArray, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class BitArray implements \ArrayAccess, \Countable, \IteratorAggregate, \JsonSerializable |
||
28 | { |
||
29 | /** |
||
30 | * @var BitArray Empty bit array |
||
31 | */ |
||
32 | private static $empty; |
||
|
|||
33 | |||
34 | /** |
||
35 | * @var integer[] Number of bits for each value between 0 and 255 |
||
36 | */ |
||
37 | private static $count = array( |
||
38 | 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, |
||
39 | 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, |
||
40 | 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, |
||
41 | 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, |
||
42 | 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, |
||
43 | 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, |
||
44 | 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, |
||
45 | 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, |
||
46 | 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, |
||
47 | 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, |
||
48 | 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, |
||
49 | 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, |
||
50 | 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, |
||
51 | 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, |
||
52 | 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, |
||
53 | 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8 |
||
54 | ); |
||
55 | |||
56 | /** |
||
57 | * @var integer[] Mask for restricting complements |
||
58 | */ |
||
59 | private static $restrict = array(255, 1, 3, 7, 15, 31, 63, 127); |
||
60 | |||
61 | /** |
||
62 | * @var string Underlying data |
||
63 | * |
||
64 | * @since 1.0.0 |
||
65 | */ |
||
66 | private $data; |
||
67 | |||
68 | /** |
||
69 | * @var integer Size of the bit array |
||
70 | * |
||
71 | * @since 1.0.0 |
||
72 | */ |
||
73 | private $size; |
||
74 | |||
75 | /** |
||
76 | * Create a new bit array of the given size |
||
77 | * |
||
78 | * @param integer $size The BitArray size |
||
79 | * @param boolean $default The default value for bits |
||
80 | * |
||
81 | * @since 1.0.0 |
||
82 | */ |
||
83 | protected function __construct($size = 0, $default = false) |
||
97 | |||
98 | /** |
||
99 | * Remove useless bits for simplifying count operation. |
||
100 | * |
||
101 | * @return BitArray $this for chaining |
||
102 | * |
||
103 | * @since 1.2.0 |
||
104 | */ |
||
105 | protected function restrict() |
||
116 | |||
117 | /** |
||
118 | * Clone a BitArray |
||
119 | * |
||
120 | * @return void |
||
121 | * |
||
122 | * @since 1.0.0 |
||
123 | */ |
||
124 | public function __clone() |
||
128 | |||
129 | /** |
||
130 | * Convert the object to a string |
||
131 | * |
||
132 | * @return string String representation of this object |
||
133 | * |
||
134 | * @since 1.0.0 |
||
135 | */ |
||
136 | public function __toString() |
||
150 | |||
151 | /** |
||
152 | * Magic get method |
||
153 | * |
||
154 | * @param string $property The property |
||
155 | * |
||
156 | * @throws \RuntimeException If the property does not exist |
||
157 | * |
||
158 | * @return mixed The value associated to the property |
||
159 | * |
||
160 | * @since 1.0.0 |
||
161 | */ |
||
162 | public function __get($property) |
||
174 | |||
175 | /** |
||
176 | * Test the existence of an index |
||
177 | * |
||
178 | * @param integer $offset The offset |
||
179 | * |
||
180 | * @return boolean The truth value |
||
181 | * |
||
182 | * @since 1.0.0 |
||
183 | */ |
||
184 | public function offsetExists($offset) |
||
188 | |||
189 | /** |
||
190 | * Get the truth value for an index |
||
191 | * |
||
192 | * @param integer $offset The offset |
||
193 | * |
||
194 | * @return boolean The truth value |
||
195 | * |
||
196 | * @throw \OutOfRangeException Argument index must be an positive integer lesser than the size |
||
197 | * |
||
198 | * @since 1.0.0 |
||
199 | */ |
||
200 | public function offsetGet($offset) |
||
211 | |||
212 | /** |
||
213 | * Set the truth value for an index |
||
214 | * |
||
215 | * @param integer $offset The offset |
||
216 | * @param boolean $value The truth value |
||
217 | * |
||
218 | * @return void |
||
219 | * |
||
220 | * @throw \OutOfRangeException Argument index must be an positive integer lesser than the size |
||
221 | * |
||
222 | * @since 1.0.0 |
||
223 | */ |
||
224 | public function offsetSet($offset, $value) |
||
244 | |||
245 | /** |
||
246 | * Unset the existence of an index |
||
247 | * |
||
248 | * @param integer $offset The index |
||
249 | * |
||
250 | * @return void |
||
251 | * |
||
252 | * @throw \RuntimeException Values cannot be unset |
||
253 | * |
||
254 | * @since 1.0.0 |
||
255 | */ |
||
256 | public function offsetUnset($offset) |
||
260 | |||
261 | /** |
||
262 | * Return the number of true bits |
||
263 | * |
||
264 | * @return integer The number of true bits |
||
265 | * |
||
266 | * @since 1.0.0 |
||
267 | */ |
||
268 | public function count() |
||
279 | |||
280 | /** |
||
281 | * Transform the object to an array |
||
282 | * |
||
283 | * @return array Array of values |
||
284 | * |
||
285 | * @since 1.1.0 |
||
286 | */ |
||
287 | public function toArray() |
||
298 | |||
299 | /** |
||
300 | * Serialize the object |
||
301 | * |
||
302 | * @return array Array of values |
||
303 | * |
||
304 | * @since 1.0.0 |
||
305 | */ |
||
306 | public function jsonSerialize() |
||
310 | |||
311 | /** |
||
312 | * Get an iterator |
||
313 | * |
||
314 | * @return Iterator Iterator |
||
315 | * |
||
316 | * @since 1.0.0 |
||
317 | */ |
||
318 | public function getIterator() |
||
322 | |||
323 | /** |
||
324 | * Return the size |
||
325 | * |
||
326 | * @return integer The size |
||
327 | * |
||
328 | * @since 1.0.0 |
||
329 | */ |
||
330 | public function size() |
||
334 | |||
335 | /** |
||
336 | * Copy bits directly from a BitArray |
||
337 | * |
||
338 | * @param BitArray $bits A BitArray to copy |
||
339 | * @param int $index Starting index for destination |
||
340 | * @param int $offset Starting index for copying |
||
341 | * @param int $size Copy size |
||
342 | * |
||
343 | * @return BitArray This object for chaining |
||
344 | * |
||
345 | * @throw \OutOfRangeException Argument index must be an positive integer lesser than the size |
||
346 | * |
||
347 | * @since 1.1.0 |
||
348 | */ |
||
349 | public function directCopy(BitArray $bits, $index = 0, $offset = 0, $size = 0) |
||
368 | |||
369 | /** |
||
370 | * Copy bits from a BitArray |
||
371 | * |
||
372 | * @param BitArray $bits A BitArray to copy |
||
373 | * @param int $index Starting index for destination. |
||
374 | * If index is non-negative, the index parameter is used as it is, keeping its real value between 0 and size-1. |
||
375 | * If index is negative, the index parameter starts from the end, keeping its real value between 0 and size-1. |
||
376 | * @param int $offset Starting index for copying. |
||
377 | * If offset is non-negative, the offset parameter is used as it is, keeping its positive value between 0 and size-1. |
||
378 | * If offset is negative, the offset parameter starts from the end, keeping its real value between 0 and size-1. |
||
379 | * @param mixed $size Copy size. |
||
380 | * If size is given and is positive, then the copy will copy size elements. |
||
381 | * If the bits argument is shorter than the size, then only the available elements will be copied. |
||
382 | * If size is given and is negative then the copy starts from the end. |
||
383 | * If it is omitted, then the copy will have everything from offset up until the end of the bits argument. |
||
384 | * |
||
385 | * @return BitArray This object for chaining |
||
386 | * |
||
387 | * @since 1.1.0 |
||
388 | */ |
||
389 | public function copy(BitArray $bits, $index = 0, $offset = 0, $size = null) |
||
402 | |||
403 | /** |
||
404 | * Get the real offset using a positive or negative offset |
||
405 | * |
||
406 | * @param int $offset If offset is non-negative, the offset parameter is used as it is, keeping its real value between 0 and size-1. |
||
407 | * If offset is negative, the offset parameter starts from the end, keeping its real value between 0 and size-1. |
||
408 | * |
||
409 | * @return integer The real offset |
||
410 | * |
||
411 | * @since 1.1.0 |
||
412 | */ |
||
413 | protected function getRealOffset($offset) |
||
434 | |||
435 | /** |
||
436 | * Get the real offset using a positive or negative offset |
||
437 | * |
||
438 | * @param int $offset The real offset. |
||
439 | * @param mixed $size If size is given and is positive, then the real size will be between 0 and the current size-1. |
||
440 | * If size is given and is negative then the real size starts from the end. |
||
441 | * If it is omitted, then the size goes until the end of the BitArray. |
||
442 | * |
||
443 | * @return integer The real size |
||
444 | * |
||
445 | * @since 1.1.0 |
||
446 | */ |
||
447 | protected function getRealSize($offset, $size) |
||
474 | |||
475 | /** |
||
476 | * Create a new BitArray from an integer |
||
477 | * |
||
478 | * @param integer $size Size of the BitArray |
||
479 | * @param boolean $default The default value for bits |
||
480 | * |
||
481 | * @return BitArray A new BitArray |
||
482 | * |
||
483 | * @since 1.0.0 |
||
484 | */ |
||
485 | public static function fromInteger($size, $default = false) |
||
489 | |||
490 | /** |
||
491 | * Create a new BitArray from a sequence of bits. |
||
492 | * |
||
493 | * @param integer $size Size of the BitArray |
||
494 | * @param integer $values The values for the bits |
||
495 | * |
||
496 | * @return BitArray A new BitArray |
||
497 | * |
||
498 | * @since 1.2.0 |
||
499 | */ |
||
500 | public static function fromDecimal($size, $values = 0) |
||
514 | |||
515 | /** |
||
516 | * Create a new BitArray from a traversable |
||
517 | * |
||
518 | * @param \Traversable $traversable A traversable and countable |
||
519 | * |
||
520 | * @return BitArray A new BitArray |
||
521 | * |
||
522 | * @since 1.0.0 |
||
523 | */ |
||
524 | View Code Duplication | public static function fromTraversable($traversable) |
|
553 | |||
554 | /** |
||
555 | * Create a new BitArray from a bit string |
||
556 | * |
||
557 | * @param string $string A bit string |
||
558 | * |
||
559 | * @return BitArray A new BitArray |
||
560 | * |
||
561 | * @since 1.0.0 |
||
562 | */ |
||
563 | View Code Duplication | public static function fromString($string) |
|
589 | |||
590 | /** |
||
591 | * Create a new BitArray from json |
||
592 | * |
||
593 | * @param string $json A json encoded value |
||
594 | * |
||
595 | * @return BitArray A new BitArray |
||
596 | * |
||
597 | * @since 1.0.0 |
||
598 | */ |
||
599 | public static function fromJson($json) |
||
603 | |||
604 | /** |
||
605 | * Create a new BitArray using a slice |
||
606 | * |
||
607 | * @param BitArray $bits A BitArray to get the slice |
||
608 | * @param int $offset If offset is non-negative, the slice will start at that offset in the bits argument. |
||
609 | * If offset is negative, the slice will start from the end of the bits argument. |
||
610 | * @param mixed $size If size is given and is positive, then the slice will have up to that many elements in it. |
||
611 | * If the bits argument is shorter than the size, then only the available elements will be present. |
||
612 | * If size is given and is negative then the slice will stop that many elements from the end of the bits argument. |
||
613 | * If it is omitted, then the slice will have everything from offset up until the end of the bits argument. |
||
614 | * |
||
615 | * @return BitArray A new BitArray |
||
616 | * |
||
617 | * @since 1.1.0 |
||
618 | */ |
||
619 | public static function fromSlice(BitArray $bits, $offset = 0, $size = null) |
||
627 | |||
628 | /** |
||
629 | * Create a new BitArray using the concat operation |
||
630 | * |
||
631 | * @param BitArray $bits1 A BitArray |
||
632 | * @param BitArray $bits2 A BitArray |
||
633 | * |
||
634 | * @return BitArray A new BitArray |
||
635 | * |
||
636 | * @since 1.1.0 |
||
637 | */ |
||
638 | public static function fromConcat(BitArray $bits1, BitArray $bits2) |
||
647 | |||
648 | /** |
||
649 | * Complement the bit array |
||
650 | * |
||
651 | * @return BitArray This object for chaining |
||
652 | * |
||
653 | * @since 1.0.0 |
||
654 | */ |
||
655 | public function applyComplement() |
||
666 | |||
667 | /** |
||
668 | * Or with an another bit array |
||
669 | * |
||
670 | * @param BitArray $bits A bit array |
||
671 | * |
||
672 | * @return BitArray This object for chaining |
||
673 | * |
||
674 | * @throw \InvalidArgumentException Argument must be of equal size |
||
675 | * |
||
676 | * @since 1.0.0 |
||
677 | */ |
||
678 | View Code Duplication | public function applyOr(BitArray $bits) |
|
696 | |||
697 | /** |
||
698 | * And with an another bit array |
||
699 | * |
||
700 | * @param BitArray $bits A bit array |
||
701 | * |
||
702 | * @return BitArray This object for chaining |
||
703 | * |
||
704 | * @throw \InvalidArgumentException Argument must be of equal size |
||
705 | * |
||
706 | * @since 1.0.0 |
||
707 | */ |
||
708 | View Code Duplication | public function applyAnd(BitArray $bits) |
|
726 | |||
727 | /** |
||
728 | * Xor with an another bit array |
||
729 | * |
||
730 | * @param BitArray $bits A bit array |
||
731 | * |
||
732 | * @return BitArray This object for chaining |
||
733 | * |
||
734 | * @throw \InvalidArgumentException Argument must be of equal size |
||
735 | * |
||
736 | * @since 1.0.0 |
||
737 | */ |
||
738 | View Code Duplication | public function applyXor(BitArray $bits) |
|
756 | |||
757 | /** |
||
758 | * Shift a bit array. |
||
759 | * |
||
760 | * @param int $size Size to shift. |
||
761 | * Negative value means the shifting is done right to left while |
||
762 | * positive value means the shifting is done left to right. |
||
763 | * @param boolean $value Value to shift |
||
764 | * |
||
765 | * @return BitArray $this for chaining |
||
766 | * |
||
767 | * @since 1.2.0 |
||
768 | */ |
||
769 | public function shift($size = 1, $value = false) |
||
804 | } |
||
805 |
This check marks private properties in classes that are never used. Those properties can be removed.