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 |
||
13 | class PublishMessageResponse extends BaseResponse |
||
14 | { |
||
15 | use MessageIdAndMD5; |
||
16 | |||
17 | public function __construct() |
||
18 | { |
||
19 | } |
||
20 | |||
21 | public function parseResponse($statusCode, $content) |
||
22 | { |
||
23 | $this->statusCode = $statusCode; |
||
24 | if ($statusCode == 201) { |
||
25 | $this->succeed = TRUE; |
||
26 | } else { |
||
27 | $this->parseErrorResponse($statusCode, $content); |
||
28 | } |
||
29 | |||
30 | $xmlReader = new \XMLReader(); |
||
31 | try { |
||
32 | $xmlReader->XML($content); |
||
33 | $this->readMessageIdAndMD5XML($xmlReader); |
||
34 | } catch (\Exception $e) { |
||
35 | throw new MnsException($statusCode, $e->getMessage(), $e); |
||
36 | } catch (\Throwable $t) { |
||
37 | throw new MnsException($statusCode, $t->getMessage()); |
||
38 | } |
||
39 | |||
40 | } |
||
41 | |||
42 | public function parseErrorResponse($statusCode, $content, MnsException $exception = NULL) |
||
43 | { |
||
44 | $this->succeed = FALSE; |
||
45 | $xmlReader = new \XMLReader(); |
||
46 | try { |
||
47 | $xmlReader->XML($content); |
||
48 | $result = XMLParser::parseNormalError($xmlReader); |
||
49 | if ($result['Code'] == Constants::TOPIC_NOT_EXIST) |
||
50 | { |
||
51 | throw new TopicNotExistException($statusCode, $result['Message'], $exception, $result['Code'], $result['RequestId'], $result['HostId']); |
||
52 | } |
||
53 | if ($result['Code'] == Constants::INVALID_ARGUMENT) |
||
54 | { |
||
55 | throw new InvalidArgumentException($statusCode, $result['Message'], $exception, $result['Code'], $result['RequestId'], $result['HostId']); |
||
56 | } |
||
57 | if ($result['Code'] == Constants::MALFORMED_XML) |
||
58 | { |
||
59 | throw new MalformedXMLException($statusCode, $result['Message'], $exception, $result['Code'], $result['RequestId'], $result['HostId']); |
||
60 | } |
||
61 | throw new MnsException($statusCode, $result['Message'], $exception, $result['Code'], $result['RequestId'], $result['HostId']); |
||
62 | } catch (\Exception $e) { |
||
63 | if ($exception != NULL) { |
||
64 | throw $exception; |
||
65 | } elseif($e instanceof MnsException) { |
||
66 | throw $e; |
||
67 | } else { |
||
68 | throw new MnsException($statusCode, $e->getMessage()); |
||
69 | } |
||
70 | } catch (\Throwable $t) { |
||
71 | throw new MnsException($statusCode, $t->getMessage()); |
||
72 | } |
||
73 | } |
||
74 | } |
||
75 | |||
77 |