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 |
||
12 | class BatchReceiveMessageResponse extends BaseResponse |
||
13 | { |
||
14 | protected $messages; |
||
15 | |||
16 | // boolean, whether the message body will be decoded as base64 |
||
17 | protected $base64; |
||
18 | |||
19 | public function __construct($base64 = TRUE) |
||
20 | { |
||
21 | $this->messages = array(); |
||
22 | $this->base64 = $base64; |
||
23 | } |
||
24 | |||
25 | public function setBase64($base64) |
||
26 | { |
||
27 | $this->base64 = $base64; |
||
28 | } |
||
29 | |||
30 | public function isBase64() |
||
31 | { |
||
32 | return ($this->base64 == TRUE); |
||
33 | } |
||
34 | |||
35 | public function getMessages() |
||
36 | { |
||
37 | return $this->messages; |
||
38 | } |
||
39 | |||
40 | public function parseResponse($statusCode, $content) |
||
41 | { |
||
42 | $this->statusCode = $statusCode; |
||
43 | if ($statusCode == 200) { |
||
44 | $this->succeed = TRUE; |
||
45 | } else { |
||
46 | $this->parseErrorResponse($statusCode, $content); |
||
47 | } |
||
48 | |||
49 | $xmlReader = new \XMLReader(); |
||
50 | try { |
||
51 | $xmlReader->XML($content); |
||
52 | while ($xmlReader->read()) |
||
53 | { |
||
54 | if ($xmlReader->nodeType == \XMLReader::ELEMENT |
||
55 | && $xmlReader->name == 'Message') |
||
56 | { |
||
57 | $this->messages[] = Message::fromXML($xmlReader, $this->base64); |
||
58 | } |
||
59 | } |
||
60 | } catch (\Exception $e) { |
||
61 | throw new MnsException($statusCode, $e->getMessage(), $e); |
||
62 | } catch (\Throwable $t) { |
||
63 | throw new MnsException($statusCode, $t->getMessage()); |
||
64 | } |
||
65 | } |
||
66 | |||
67 | public function parseErrorResponse($statusCode, $content, MnsException $exception = NULL) |
||
68 | { |
||
69 | $this->succeed = FALSE; |
||
70 | $xmlReader = new \XMLReader(); |
||
71 | try { |
||
72 | $xmlReader->XML($content); |
||
73 | $result = XMLParser::parseNormalError($xmlReader); |
||
74 | if ($result['Code'] == Constants::QUEUE_NOT_EXIST) |
||
75 | { |
||
76 | throw new QueueNotExistException($statusCode, $result['Message'], $exception, $result['Code'], $result['RequestId'], $result['HostId']); |
||
77 | } |
||
78 | if ($result['Code'] == Constants::MESSAGE_NOT_EXIST) |
||
79 | { |
||
80 | throw new MessageNotExistException($statusCode, $result['Message'], $exception, $result['Code'], $result['RequestId'], $result['HostId']); |
||
81 | } |
||
82 | throw new MnsException($statusCode, $result['Message'], $exception, $result['Code'], $result['RequestId'], $result['HostId']); |
||
83 | } catch (\Exception $e) { |
||
84 | if ($exception != NULL) { |
||
85 | throw $exception; |
||
86 | } elseif($e instanceof MnsException) { |
||
87 | throw $e; |
||
88 | } else { |
||
89 | throw new MnsException($statusCode, $e->getMessage()); |
||
90 | } |
||
91 | } catch (\Throwable $t) { |
||
92 | throw new MnsException($statusCode, $t->getMessage()); |
||
93 | } |
||
94 | |||
95 | } |
||
96 | } |
||
97 | |||
99 |