Complex classes like BitManipulation 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 BitManipulation, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class BitManipulation |
||
23 | { |
||
24 | /** |
||
25 | * Mode from to is the default mode of inspection of frames. But PHP usually uses from and length to inspect frames. |
||
26 | */ |
||
27 | const MODE_FROM_TO = 0; |
||
28 | const MODE_PHP = 1; |
||
29 | |||
30 | /** |
||
31 | * Get a specific bit from a byte. |
||
32 | * |
||
33 | * @param int $byte |
||
34 | * @param int $bitNumber |
||
35 | * @return int |
||
36 | */ |
||
37 | 81 | public static function nthBit(int $byte, int $bitNumber) : int |
|
55 | |||
56 | /** |
||
57 | * Get a specific byte inside a frame represented by an int or a string. |
||
58 | * |
||
59 | * @param string|int $frame Non utf8 string (this should be more precisely a bytes-string). |
||
60 | * @param int $byteNumber Starting at 0. |
||
61 | * @return int |
||
62 | */ |
||
63 | 88 | public static function nthByte($frame, int $byteNumber) : int |
|
106 | |||
107 | 74 | public static function partOfByte(int $byte, int $part) : int |
|
123 | |||
124 | /** |
||
125 | * Because strings are the best way to store many bytes in PHP it can |
||
126 | * be useful to make the conversion between hex (which are strings) |
||
127 | * array to string. |
||
128 | * |
||
129 | * @param array $hexArray |
||
130 | * @return string |
||
131 | */ |
||
132 | 55 | public static function hexArrayToString(...$hexArray) : string |
|
145 | |||
146 | /** |
||
147 | * @param string|int $frame |
||
148 | * @param int $from Byte where to start (should be inferior to $to). |
||
149 | * @param int $to Byte where to stop (considering it starts at 0). The `to` value include the target |
||
150 | * byte. |
||
151 | * @param bool $force8bytes By default PHP have a wrong behavior with 8 bytes variables. If you have 8 bytes |
||
152 | * the returned int will be negative (because unsigned integers does not exists in PHP) |
||
153 | * @return int |
||
154 | */ |
||
155 | 39 | public static function bytesFromTo($frame, int $from, int $to, bool $force8bytes = false) : int |
|
202 | |||
203 | /** |
||
204 | * Proxy to the substr to be sure to be use the right method (mb_substr) |
||
205 | * |
||
206 | * @param string $frame |
||
207 | * @param int $from |
||
208 | * @param int $to |
||
209 | * @return string |
||
210 | */ |
||
211 | 37 | public static function bytesFromToString(string $frame, int $from, int $to, int $mode = BitManipulation::MODE_FROM_TO) : string |
|
219 | |||
220 | /** |
||
221 | * Take a frame represented by a decimal int to transform it in a string. |
||
222 | * Notice that any int is a frame and cannot be more than 8 bytes |
||
223 | * |
||
224 | * @param int $frame |
||
225 | * @param int|null $size In bytes. This value should always be precise. Be careful if you don't ! |
||
226 | * @return string |
||
227 | */ |
||
228 | 23 | public static function intToBinaryString(int $frame, int $size = null) : string |
|
254 | |||
255 | /** |
||
256 | * Take an string frame and transform it to a decimal frame (inside an int). |
||
257 | * |
||
258 | * @param string $frame |
||
259 | * @return int |
||
260 | */ |
||
261 | 2 | public static function binaryStringtoInt(string $frame) : int |
|
292 | |||
293 | /** |
||
294 | * Method that return frame as hex (more readable). |
||
295 | * Helpful for debug ! |
||
296 | * |
||
297 | * @param string $frame |
||
298 | * @return string |
||
299 | */ |
||
300 | 3 | public static function binaryStringToHex(string $frame) : string |
|
304 | |||
305 | /** |
||
306 | * Haters gonna hate. `strlen` cannot be trusted because of an option of mbstring extension, more info: |
||
307 | * http://php.net/manual/fr/mbstring.overload.php |
||
308 | * http://php.net/manual/fr/function.mb-strlen.php#77040 |
||
309 | * |
||
310 | * @param string $frame |
||
311 | * @return int |
||
312 | */ |
||
313 | 101 | public static function frameSize(string $frame) : int |
|
321 | } |
||
322 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.