Complex classes like BinaryReader 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 BinaryReader, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class BinaryReader |
||
15 | { |
||
16 | /** |
||
17 | * @var int |
||
18 | */ |
||
19 | private $machineByteOrder = Endian::ENDIAN_LITTLE; |
||
20 | |||
21 | /** |
||
22 | * @var resource |
||
23 | */ |
||
24 | private $inputHandle; |
||
25 | |||
26 | /** |
||
27 | * @var int |
||
28 | */ |
||
29 | private $currentBit; |
||
30 | |||
31 | /** |
||
32 | * @var mixed |
||
33 | */ |
||
34 | private $nextByte; |
||
35 | |||
36 | /** |
||
37 | * @var int |
||
38 | */ |
||
39 | private $position; |
||
40 | |||
41 | /** |
||
42 | * @var int |
||
43 | */ |
||
44 | private $eofPosition; |
||
45 | |||
46 | /** |
||
47 | * @var string |
||
48 | */ |
||
49 | private $endian; |
||
50 | |||
51 | /** |
||
52 | * @var \PhpBinaryReader\Type\Byte |
||
53 | */ |
||
54 | private $byteReader; |
||
55 | |||
56 | /** |
||
57 | * @var \PhpBinaryReader\Type\Bit |
||
58 | */ |
||
59 | private $bitReader; |
||
60 | |||
61 | /** |
||
62 | * @var \PhpBinaryReader\Type\Str |
||
63 | */ |
||
64 | private $stringReader; |
||
65 | |||
66 | /** |
||
67 | * @var \PhpBinaryReader\Type\Int8 |
||
68 | */ |
||
69 | private $int8Reader; |
||
70 | |||
71 | /** |
||
72 | * @var \PhpBinaryReader\Type\Int16 |
||
73 | */ |
||
74 | private $int16Reader; |
||
75 | |||
76 | /** |
||
77 | * @var \PhpBinaryReader\Type\Int32 |
||
78 | */ |
||
79 | private $int32Reader; |
||
80 | |||
81 | /** |
||
82 | * @var \PhpBinaryReader\Type\Single |
||
83 | */ |
||
84 | private $singleReader; |
||
85 | |||
86 | /** |
||
87 | * @param string|resource $input |
||
88 | * @param int|string $endian |
||
89 | * @throws \InvalidArgumentException |
||
90 | */ |
||
91 | 62 | public function __construct($input, $endian = Endian::ENDIAN_LITTLE) |
|
114 | |||
115 | /** |
||
116 | * @return bool |
||
117 | */ |
||
118 | 2 | public function isEof() |
|
122 | |||
123 | /** |
||
124 | * @param int $length |
||
125 | * @return bool |
||
126 | */ |
||
127 | 114 | public function canReadBytes($length = 0) |
|
131 | |||
132 | /** |
||
133 | * @return void |
||
134 | */ |
||
135 | 14 | public function align() |
|
140 | |||
141 | /** |
||
142 | * @param int $count |
||
143 | * @return int |
||
144 | */ |
||
145 | 66 | public function readBits($count) |
|
149 | |||
150 | /** |
||
151 | * @param int $count |
||
152 | * @return int |
||
153 | */ |
||
154 | 4 | public function readUBits($count) |
|
158 | |||
159 | /** |
||
160 | * @param int $count |
||
161 | * @return int |
||
162 | */ |
||
163 | 2 | public function readBytes($count) |
|
167 | |||
168 | /** |
||
169 | * @return int |
||
170 | */ |
||
171 | 32 | public function readInt8() |
|
175 | |||
176 | /** |
||
177 | * @return int |
||
178 | */ |
||
179 | 4 | public function readUInt8() |
|
183 | |||
184 | /** |
||
185 | * @return int |
||
186 | */ |
||
187 | 4 | public function readInt16() |
|
191 | |||
192 | /** |
||
193 | * @return string |
||
194 | */ |
||
195 | 6 | public function readUInt16() |
|
199 | |||
200 | /** |
||
201 | * @return int |
||
202 | */ |
||
203 | 6 | public function readInt32() |
|
207 | |||
208 | /** |
||
209 | * @return int |
||
210 | */ |
||
211 | 6 | public function readUInt32() |
|
215 | |||
216 | /** |
||
217 | * @return float |
||
218 | */ |
||
219 | 2 | public function readSingle() |
|
223 | |||
224 | /** |
||
225 | * @param int $length |
||
226 | * @return string |
||
227 | */ |
||
228 | 2 | public function readString($length) |
|
232 | |||
233 | /** |
||
234 | * @param int $length |
||
235 | * @return string |
||
236 | */ |
||
237 | 2 | public function readAlignedString($length) |
|
241 | |||
242 | /** |
||
243 | * @param int $machineByteOrder |
||
244 | * @return $this |
||
245 | */ |
||
246 | 6 | public function setMachineByteOrder($machineByteOrder) |
|
252 | |||
253 | /** |
||
254 | * @return int |
||
255 | */ |
||
256 | 32 | public function getMachineByteOrder() |
|
260 | |||
261 | /** |
||
262 | * @param resource $inputHandle |
||
263 | * @return $this |
||
264 | */ |
||
265 | 43 | public function setInputHandle($inputHandle) |
|
271 | |||
272 | /** |
||
273 | * @return resource |
||
274 | */ |
||
275 | 104 | public function getInputHandle() |
|
279 | |||
280 | /** |
||
281 | * @param string $inputString |
||
282 | * @return $this |
||
283 | */ |
||
284 | 21 | public function setInputString($inputString) |
|
293 | |||
294 | /** |
||
295 | * @return string |
||
296 | */ |
||
297 | 2 | public function getInputString() |
|
305 | |||
306 | /** |
||
307 | * @param mixed $nextByte |
||
308 | * @return $this |
||
309 | */ |
||
310 | 106 | public function setNextByte($nextByte) |
|
316 | |||
317 | /** |
||
318 | * @return mixed |
||
319 | */ |
||
320 | 58 | public function getNextByte() |
|
324 | |||
325 | /** |
||
326 | * @param int $position |
||
327 | * @return $this |
||
328 | */ |
||
329 | 104 | public function setPosition($position) |
|
336 | |||
337 | /** |
||
338 | * @return int |
||
339 | */ |
||
340 | 4 | public function getPosition() |
|
344 | |||
345 | /** |
||
346 | * @return int |
||
347 | */ |
||
348 | 2 | public function getEofPosition() |
|
352 | |||
353 | /** |
||
354 | * @param string $endian |
||
355 | * @return $this |
||
356 | * @throws InvalidDataException |
||
357 | */ |
||
358 | 64 | public function setEndian($endian) |
|
370 | |||
371 | /** |
||
372 | * @return string |
||
373 | */ |
||
374 | 52 | public function getEndian() |
|
378 | |||
379 | /** |
||
380 | * @param int $currentBit |
||
381 | * @return $this |
||
382 | */ |
||
383 | 106 | public function setCurrentBit($currentBit) |
|
389 | |||
390 | /** |
||
391 | * @return int |
||
392 | */ |
||
393 | 104 | public function getCurrentBit() |
|
397 | |||
398 | /** |
||
399 | * @return \PhpBinaryReader\Type\Bit |
||
400 | */ |
||
401 | 1 | public function getBitReader() |
|
405 | |||
406 | /** |
||
407 | * @return \PhpBinaryReader\Type\Byte |
||
408 | */ |
||
409 | 1 | public function getByteReader() |
|
413 | |||
414 | /** |
||
415 | * @return \PhpBinaryReader\Type\Int8 |
||
416 | */ |
||
417 | 1 | public function getInt8Reader() |
|
421 | |||
422 | /** |
||
423 | * @return \PhpBinaryReader\Type\Int16 |
||
424 | */ |
||
425 | 1 | public function getInt16Reader() |
|
429 | |||
430 | /** |
||
431 | * @return \PhpBinaryReader\Type\Int32 |
||
432 | */ |
||
433 | 1 | public function getInt32Reader() |
|
437 | |||
438 | /** |
||
439 | * @return \PhpBinaryReader\Type\Single |
||
440 | */ |
||
441 | 1 | public function getSingleReader() |
|
445 | |||
446 | /** |
||
447 | * @return \PhpBinaryReader\Type\Str |
||
448 | */ |
||
449 | 1 | public function getStringReader() |
|
453 | |||
454 | /** |
||
455 | * Read a length of characters from the input handle, updating the |
||
456 | * internal position marker. |
||
457 | * |
||
458 | * @return string |
||
459 | */ |
||
460 | 110 | public function readFromHandle($length) |
|
465 | } |
||
466 |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.