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