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 Response 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 Response, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class Response |
||
15 | { |
||
16 | /** @var string */ |
||
17 | public $rawResponse; |
||
18 | |||
19 | /** @var string */ |
||
20 | protected $response; |
||
21 | |||
22 | /** @var array */ |
||
23 | protected $segments = array(); |
||
24 | |||
25 | /** @var string */ |
||
26 | protected $dialogId; |
||
27 | |||
28 | /** @var string */ |
||
29 | protected $systemId; |
||
30 | |||
31 | /** |
||
32 | * Response constructor. |
||
33 | * |
||
34 | * @param string $rawResponse |
||
35 | */ |
||
36 | 1 | public function __construct($rawResponse) |
|
46 | |||
47 | /** |
||
48 | * Extracts dialog ID from response. |
||
49 | * |
||
50 | * @return string|null |
||
51 | * @throws \Exception |
||
52 | */ |
||
53 | public function getDialogId() |
||
63 | |||
64 | /** |
||
65 | * Extracts bank name from response. |
||
66 | * |
||
67 | * @return string|null |
||
68 | */ |
||
69 | public function getBankName() |
||
82 | |||
83 | /** |
||
84 | * Some kind of HBCI pagination. |
||
85 | * |
||
86 | * @param AbstractMessage $message |
||
87 | * |
||
88 | * @return array |
||
89 | */ |
||
90 | public function getTouchDowns(AbstractMessage $message) |
||
113 | |||
114 | /** |
||
115 | * Extracts supported TAN mechanisms from response. |
||
116 | * |
||
117 | * @return array |
||
118 | */ |
||
119 | public function getSupportedTanMechanisms() |
||
138 | |||
139 | /** |
||
140 | * @return int |
||
141 | */ |
||
142 | public function getHksalMaxVersion() |
||
146 | |||
147 | /** |
||
148 | * @return int |
||
149 | */ |
||
150 | public function getHkkazMaxVersion() |
||
154 | |||
155 | /** |
||
156 | * Checks if request / response was successful. |
||
157 | * |
||
158 | * @return bool |
||
159 | */ |
||
160 | public function isSuccess() |
||
172 | |||
173 | /** |
||
174 | * @return array |
||
175 | * @throws \Exception |
||
176 | */ |
||
177 | public function getMessageSummary() |
||
181 | |||
182 | /** |
||
183 | * @return array |
||
184 | * @throws \Exception |
||
185 | */ |
||
186 | public function getSegmentSummary() |
||
190 | |||
191 | /** |
||
192 | * @param string $name |
||
193 | * |
||
194 | * @return array |
||
195 | * @throws \Exception |
||
196 | */ |
||
197 | protected function getSummaryBySegment($name) |
||
214 | |||
215 | /** |
||
216 | * @param string $segmentName |
||
217 | * |
||
218 | * @return int |
||
219 | */ |
||
220 | public function getSegmentMaxVersion($segmentName) |
||
235 | |||
236 | /** |
||
237 | * @return string |
||
238 | * @throws \Exception |
||
239 | */ |
||
240 | public function getSystemId() |
||
250 | |||
251 | /** |
||
252 | * @param bool $translateCodes |
||
253 | * |
||
254 | * @return string |
||
255 | */ |
||
256 | View Code Duplication | public function humanReadable($translateCodes = false) |
|
266 | |||
267 | /** |
||
268 | * @param string $name |
||
269 | * @param AbstractSegment $reference |
||
270 | * |
||
271 | * @return string|null |
||
272 | */ |
||
273 | protected function findSegmentForReference($name, AbstractSegment $reference) |
||
287 | |||
288 | /** |
||
289 | * @param string $name |
||
290 | * |
||
291 | * @return string|null |
||
292 | */ |
||
293 | protected function findSegment($name) |
||
297 | |||
298 | /** |
||
299 | * @param string $name |
||
300 | * @param bool $one |
||
301 | * |
||
302 | * @return array|null|string |
||
303 | */ |
||
304 | protected function findSegments($name, $one = false) |
||
321 | |||
322 | /** |
||
323 | * @param $segment |
||
324 | * |
||
325 | * @return array |
||
326 | */ |
||
327 | 1 | protected function splitSegment($segment) |
|
337 | |||
338 | /** |
||
339 | * @param $deg |
||
340 | * |
||
341 | * @return array |
||
342 | */ |
||
343 | protected function splitDeg($deg) |
||
347 | |||
348 | /** |
||
349 | * @param int $idx |
||
350 | * @param $segment |
||
351 | * |
||
352 | * @return string|null |
||
353 | */ |
||
354 | protected function getSegmentIndex($idx, $segment) |
||
363 | |||
364 | /** |
||
365 | * @param string $response |
||
366 | * |
||
367 | * @return string |
||
368 | */ |
||
369 | 1 | protected function unwrapEncryptedMsg($response) |
|
377 | } |
||
378 |