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 |
||
21 | class BitManipulation |
||
22 | { |
||
23 | /** |
||
24 | * Mode from to is the default mode of inspection of frames. But PHP usually uses from and length to inspect frames. |
||
25 | */ |
||
26 | const MODE_FROM_TO = 0; |
||
27 | const MODE_PHP = 1; |
||
28 | |||
29 | /** |
||
30 | * Get a specific bit from a byte. |
||
31 | * |
||
32 | * @param int $byte |
||
33 | * @param int $bitNumber |
||
34 | * @return int |
||
35 | */ |
||
36 | public static function nthBit(int $byte, int $bitNumber) : int |
||
54 | |||
55 | /** |
||
56 | * Get a specific byte inside a frame represented by an int or a string. |
||
57 | * |
||
58 | * @param string|int $frame Non utf8 string (this should be more precisely a bytes-string). |
||
59 | * @param int $byteNumber Starting at 0. |
||
60 | * @return int |
||
61 | */ |
||
62 | public static function nthByte($frame, int $byteNumber) : int |
||
105 | |||
106 | public static function partOfByte(int $byte, int $part) : int |
||
122 | |||
123 | /** |
||
124 | * Because strings are the best way to store many bytes in PHP it can |
||
125 | * be useful to make the conversion between hex (which are strings) |
||
126 | * array to string. |
||
127 | * |
||
128 | * @param array $hexArray |
||
129 | * @return string |
||
130 | */ |
||
131 | public static function hexArrayToString(...$hexArray) : string |
||
144 | |||
145 | /** |
||
146 | * @param string|int $frame |
||
147 | * @param int $from Byte where to start (should be inferior to $to). |
||
148 | * @param int $to Byte where to stop (considering it starts at 0). The `to` value include the target |
||
149 | * byte. |
||
150 | * @param bool $force8bytes By default PHP have a wrong behavior with 8 bytes variables. If you have 8 bytes |
||
151 | * the returned int will be negative (because unsigned integers does not exists in PHP) |
||
152 | * @return int |
||
153 | */ |
||
154 | public static function bytesFromTo($frame, int $from, int $to, bool $force8bytes = false) : int |
||
201 | |||
202 | /** |
||
203 | * Proxy to the substr to be sure to be use the right method (mb_substr) |
||
204 | * |
||
205 | * @param string $frame |
||
206 | * @param int $from |
||
207 | * @param int $to |
||
208 | * @return string |
||
209 | */ |
||
210 | public static function bytesFromToString(string $frame, int $from, int $to, int $mode = BitManipulation::MODE_FROM_TO) : string |
||
218 | |||
219 | /** |
||
220 | * Take a frame represented by a decimal int to transform it in a string. |
||
221 | * Notice that any int is a frame and cannot be more than 8 bytes |
||
222 | * |
||
223 | * @param int $frame |
||
224 | * @param int|null $size In bytes. This value should always be precise. Be careful if you don't ! |
||
225 | * @return string |
||
226 | */ |
||
227 | public static function intToBinaryString(int $frame, int $size = 0) : string |
||
245 | |||
246 | /** |
||
247 | * Take an string frame and transform it to a decimal frame (inside an int). |
||
248 | * |
||
249 | * @param string $frame |
||
250 | * @return int |
||
251 | */ |
||
252 | public static function binaryToInt(string $frame) : int |
||
275 | |||
276 | /** |
||
277 | * Method that return frame as hex (more readable). |
||
278 | * Helpful for debug ! |
||
279 | * |
||
280 | * @param string $frame |
||
281 | * @return string |
||
282 | */ |
||
283 | public static function binaryToHex(string $frame) : string |
||
287 | |||
288 | /** |
||
289 | * Haters gonna hate. `strlen` cannot be trusted because of an option of mbstring extension, more info: |
||
290 | * http://php.net/manual/fr/mbstring.overload.php |
||
291 | * http://php.net/manual/fr/function.mb-strlen.php#77040 |
||
292 | * |
||
293 | * @param string $frame |
||
294 | * @return int |
||
295 | */ |
||
296 | public static function frameSize(string $frame) : int |
||
304 | } |
||
305 |
As per the PSR-2 coding standard, the
break
(or other terminating) statement must be on a line of its own.To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.