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 | |||
| 17 | /** @var string */ |
||
| 18 | public $rawResponse; |
||
| 19 | |||
| 20 | /** @var string */ |
||
| 21 | protected $response; |
||
| 22 | |||
| 23 | /** @var array */ |
||
| 24 | protected $segments = array(); |
||
| 25 | |||
| 26 | /** @var string */ |
||
| 27 | protected $dialogId; |
||
| 28 | |||
| 29 | /** @var string */ |
||
| 30 | protected $systemId; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Response constructor. |
||
| 34 | * |
||
| 35 | * @param string $rawResponse |
||
| 36 | */ |
||
| 37 | 1 | public function __construct($rawResponse) |
|
| 38 | { |
||
| 39 | 1 | if ($rawResponse instanceof Initialization) { |
|
| 40 | $rawResponse = $rawResponse->rawResponse; |
||
| 41 | } |
||
| 42 | |||
| 43 | 1 | $this->rawResponse = $rawResponse; |
|
| 44 | 1 | $this->response = $this->unwrapEncryptedMsg($rawResponse); |
|
| 45 | 1 | $this->segments = preg_split("#'(?=[A-Z]{4,}:\d|')#", $rawResponse); |
|
| 46 | 1 | } |
|
| 47 | |||
| 48 | /** |
||
| 49 | * Extracts dialog ID from response. |
||
| 50 | * |
||
| 51 | * @return string|null |
||
| 52 | * @throws \Exception |
||
| 53 | */ |
||
| 54 | public function getDialogId() |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Extracts bank name from response. |
||
| 67 | * |
||
| 68 | * @return string|null |
||
| 69 | */ |
||
| 70 | public function getBankName() |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Some kind of HBCI pagination. |
||
| 86 | * |
||
| 87 | * @param AbstractMessage $message |
||
| 88 | * |
||
| 89 | * @return array |
||
| 90 | */ |
||
| 91 | public function getTouchDowns(AbstractMessage $message) |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Extracts supported TAN mechanisms from response. |
||
| 117 | * |
||
| 118 | * @return array|bool |
||
| 119 | */ |
||
| 120 | public function getSupportedTanMechanisms() |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @return string |
||
| 142 | */ |
||
| 143 | public function getHksalMaxVersion() |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @return string |
||
| 150 | */ |
||
| 151 | public function getHkkazMaxVersion() |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Checks if request / response was successful. |
||
| 158 | * |
||
| 159 | * @return bool |
||
| 160 | */ |
||
| 161 | public function isSuccess() |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @return array |
||
| 176 | * @throws \Exception |
||
| 177 | */ |
||
| 178 | public function getMessageSummary() |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @return array |
||
| 185 | * @throws \Exception |
||
| 186 | */ |
||
| 187 | public function getSegmentSummary() |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @param string $name |
||
| 194 | * |
||
| 195 | * @return array |
||
| 196 | * @throws \Exception |
||
| 197 | */ |
||
| 198 | protected function getSummaryBySegment($name) |
||
| 199 | { |
||
| 200 | if (!in_array($name, array('HIRMS', 'HIRMG'))) { |
||
| 201 | throw new \Exception('Invalid segment for message summary. Only HIRMS and HIRMG supported'); |
||
| 202 | } |
||
| 203 | |||
| 204 | $result = array(); |
||
| 205 | $segment = $this->findSegment($name); |
||
| 206 | $segment = $this->splitSegment($segment); |
||
| 207 | array_shift($segment); |
||
| 208 | foreach ($segment as $de) { |
||
| 209 | $de = $this->splitDeg($de); |
||
| 210 | $result[$de[0]] = $de[2]; |
||
| 211 | } |
||
| 212 | |||
| 213 | return $result; |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @param string $segmentName |
||
| 218 | * |
||
| 219 | * @return string |
||
| 220 | */ |
||
| 221 | public function getSegmentMaxVersion($segmentName) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @return string |
||
| 239 | * @throws \Exception |
||
| 240 | */ |
||
| 241 | public function getSystemId() |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @param bool $translateCodes |
||
| 254 | * |
||
| 255 | * @return string |
||
| 256 | */ |
||
| 257 | View Code Duplication | public function humanReadable($translateCodes = false) |
|
| 258 | { |
||
| 259 | return str_replace( |
||
| 260 | array("'", '+'), |
||
| 261 | array(PHP_EOL, PHP_EOL . " "), |
||
| 262 | $translateCodes |
||
| 263 | ? NameMapping::translateResponse($this->rawResponse) |
||
| 264 | : $this->rawResponse |
||
| 265 | ); |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @param string $name |
||
| 270 | * @param AbstractSegment $reference |
||
| 271 | * |
||
| 272 | * @return string|null |
||
| 273 | */ |
||
| 274 | protected function findSegmentForReference($name, AbstractSegment $reference) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param string $name |
||
| 292 | * |
||
| 293 | * @return array|null|string |
||
| 294 | */ |
||
| 295 | protected function findSegment($name) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * @param string $name |
||
| 302 | * @param bool $one |
||
| 303 | * |
||
| 304 | * @return array|null|string |
||
| 305 | */ |
||
| 306 | protected function findSegments($name, $one = false) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @param $segment |
||
| 326 | * |
||
| 327 | * @return array |
||
| 328 | */ |
||
| 329 | 1 | protected function splitSegment($segment) |
|
| 330 | { |
||
| 331 | 1 | $parts = preg_split('/\+(?<!\?\+)/', $segment); |
|
| 332 | |||
| 333 | 1 | foreach ($parts as &$part) { |
|
| 334 | 1 | $part = str_replace('?+', '+', $part); |
|
| 335 | 1 | } |
|
| 336 | |||
| 337 | 1 | return $parts; |
|
| 338 | } |
||
| 339 | |||
| 340 | /** |
||
| 341 | * @param $deg |
||
| 342 | * |
||
| 343 | * @return array |
||
| 344 | */ |
||
| 345 | protected function splitDeg($deg) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * @param int $idx |
||
| 352 | * @param $segment |
||
| 353 | * |
||
| 354 | * @return string|null |
||
| 355 | */ |
||
| 356 | protected function getSegmentIndex($idx, $segment) |
||
| 365 | |||
| 366 | /** |
||
| 367 | * @param string $response |
||
| 368 | * |
||
| 369 | * @return string |
||
| 370 | */ |
||
| 371 | 1 | protected function unwrapEncryptedMsg($response) |
|
| 379 | } |
||
| 380 |