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 |
||
| 11 | class SendMessageResponseItem |
||
| 12 | { |
||
| 13 | use MessageIdAndMD5; |
||
| 14 | |||
| 15 | protected $isSucceed; |
||
| 16 | protected $errorCode; |
||
| 17 | protected $errorMessage; |
||
| 18 | |||
| 19 | public function __construct($isSucceed, $param1, $param2) |
||
| 20 | { |
||
| 21 | $this->isSucceed = $isSucceed; |
||
| 22 | if ($isSucceed == TRUE) |
||
| 23 | { |
||
| 24 | $this->messageId = $param1; |
||
| 25 | $this->messageBodyMD5 = $param2; |
||
| 26 | } |
||
| 27 | else |
||
| 28 | { |
||
| 29 | $this->errorCode = $param1; |
||
| 30 | $this->errorMessage = $param2; |
||
| 31 | } |
||
| 32 | } |
||
| 33 | |||
| 34 | public function isSucceed() |
||
| 35 | { |
||
| 36 | return $this->isSucceed; |
||
| 37 | } |
||
| 38 | |||
| 39 | public function getErrorCode() |
||
| 40 | { |
||
| 41 | return $this->errorCode; |
||
| 42 | } |
||
| 43 | |||
| 44 | public function getErrorMessage() |
||
| 45 | { |
||
| 46 | return $this->errorMessage; |
||
| 47 | } |
||
| 48 | |||
| 49 | static public function fromXML($xmlReader) |
||
| 50 | { |
||
| 51 | $messageId = NULL; |
||
| 52 | $messageBodyMD5 = NULL; |
||
| 53 | $errorCode = NULL; |
||
| 54 | $errorMessage = NULL; |
||
| 55 | |||
| 56 | while ($xmlReader->read()) |
||
| 57 | { |
||
| 58 | switch ($xmlReader->nodeType) |
||
| 59 | { |
||
| 60 | case \XMLReader::ELEMENT: |
||
| 61 | switch ($xmlReader->name) { |
||
| 62 | case Constants::MESSAGE_ID: |
||
| 63 | $xmlReader->read(); |
||
| 64 | if ($xmlReader->nodeType == \XMLReader::TEXT) |
||
| 65 | { |
||
| 66 | $messageId = $xmlReader->value; |
||
| 67 | } |
||
| 68 | break; |
||
| 69 | case Constants::MESSAGE_BODY_MD5: |
||
| 70 | $xmlReader->read(); |
||
| 71 | if ($xmlReader->nodeType == \XMLReader::TEXT) |
||
| 72 | { |
||
| 73 | $messageBodyMD5 = $xmlReader->value; |
||
| 74 | } |
||
| 75 | break; |
||
| 76 | case Constants::ERROR_CODE: |
||
| 77 | $xmlReader->read(); |
||
| 78 | if ($xmlReader->nodeType == \XMLReader::TEXT) |
||
| 79 | { |
||
| 80 | $errorCode = $xmlReader->value; |
||
| 81 | } |
||
| 82 | break; |
||
| 83 | case Constants::ERROR_MESSAGE: |
||
| 84 | $xmlReader->read(); |
||
| 85 | if ($xmlReader->nodeType == \XMLReader::TEXT) |
||
| 86 | { |
||
| 87 | $errorMessage = $xmlReader->value; |
||
| 88 | } |
||
| 89 | break; |
||
| 90 | } |
||
| 91 | break; |
||
| 92 | case \XMLReader::END_ELEMENT: |
||
| 93 | if ($xmlReader->name == 'Message') |
||
| 94 | { |
||
| 95 | if ($messageId != NULL) |
||
| 96 | { |
||
| 97 | return new SendMessageResponseItem(TRUE, $messageId, $messageBodyMD5); |
||
| 98 | } |
||
| 99 | else |
||
| 100 | { |
||
| 101 | return new SendMessageResponseItem(FALSE, $errorCode, $errorMessage); |
||
| 102 | } |
||
| 103 | } |
||
| 104 | break; |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | if ($messageId != NULL) |
||
| 109 | { |
||
| 110 | return new SendMessageResponseItem(TRUE, $messageId, $messageBodyMD5); |
||
| 111 | } |
||
| 112 | else |
||
| 113 | { |
||
| 114 | return new SendMessageResponseItem(FALSE, $errorCode, $errorMessage); |
||
| 115 | } |
||
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 120 |