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:
1 | <?php |
||
8 | class DeleteTopicResponse extends BaseResponse |
||
9 | { |
||
10 | public function __construct() |
||
11 | { |
||
12 | } |
||
13 | |||
14 | public function parseResponse($statusCode, $content) |
||
15 | { |
||
16 | $this->statusCode = $statusCode; |
||
17 | if ($statusCode == 204) { |
||
18 | $this->succeed = TRUE; |
||
19 | } else { |
||
20 | $this->parseErrorResponse($statusCode, $content); |
||
21 | } |
||
22 | } |
||
23 | |||
24 | public function parseErrorResponse($statusCode, $content, MnsException $exception = NULL) |
||
25 | { |
||
26 | $this->succeed = FALSE; |
||
27 | $xmlReader = new \XMLReader(); |
||
28 | try { |
||
29 | $xmlReader->XML($content); |
||
30 | $result = XMLParser::parseNormalError($xmlReader); |
||
31 | throw new MnsException($statusCode, $result['Message'], $exception, $result['Code'], $result['RequestId'], $result['HostId']); |
||
32 | } catch (\Exception $e) { |
||
33 | if ($exception != NULL) { |
||
34 | throw $exception; |
||
35 | } elseif($e instanceof MnsException) { |
||
36 | throw $e; |
||
37 | } else { |
||
38 | throw new MnsException($statusCode, $e->getMessage()); |
||
39 | } |
||
40 | } catch (\Throwable $t) { |
||
41 | throw new MnsException($statusCode, $t->getMessage()); |
||
42 | } |
||
43 | } |
||
44 | } |
||
45 | |||
47 |