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 | 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 | public static function nthByte($frame, int $byteNumber) : int |
||
106 | |||
107 | 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 | 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 | 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 | 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 | 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 | public static function binaryStringtoInt(string $frame) : int |
||
294 | |||
295 | /** |
||
296 | * Method that return frame as hex (more readable). |
||
297 | * Helpful for debug ! |
||
298 | * |
||
299 | * @param string $frame |
||
300 | * @return string |
||
301 | */ |
||
302 | public static function binaryStringToHex(string $frame) : string |
||
303 | { |
||
304 | return \unpack('H*', $frame)[1]; |
||
305 | } |
||
306 | |||
307 | /** |
||
308 | * Haters gonna hate. `strlen` cannot be trusted because of an option of mbstring extension, more info: |
||
309 | * http://php.net/manual/fr/mbstring.overload.php |
||
310 | * http://php.net/manual/fr/function.mb-strlen.php#77040 |
||
311 | * |
||
312 | * @param string $frame |
||
313 | * @return int |
||
314 | */ |
||
315 | public static function frameSize(string $frame) : int |
||
323 | } |
||
324 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: