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 |
||
| 36 | class HandlerCreateFormOfPayment extends StandardResponseHandler |
||
| 37 | { |
||
| 38 | //General error |
||
| 39 | const Q_G_ERR = "//m:transmissionError/m:errorOrWarningCodeDetails/m:errorDetails/m:errorCode"; |
||
| 40 | const Q_G_CAT = "//m:transmissionError/m:errorOrWarningCodeDetails/m:errorDetails/m:errorCategory"; |
||
| 41 | const Q_G_MSG = "//m:transmissionError/m:errorWarningDescription/m:freeText"; |
||
| 42 | //FP level error |
||
| 43 | const Q_F_ERR = "//m:fopDescription//m:fpElementError/m:errorOrWarningCodeDetails/m:errorDetails/m:errorCode"; |
||
| 44 | const Q_F_CAT = "//m:fopDescription//m:fpElementError/m:errorOrWarningCodeDetails/m:errorDetails/m:errorCategory"; |
||
| 45 | const Q_F_MSG = "//m:fopDescription//m:fpElementError/m:errorWarningDescription/m:freeText"; |
||
| 46 | //Deficient FOP level: |
||
| 47 | const Q_D_ERR = "//m:fopDescription/m:mopDescription/m:mopElementError/m:errorOrWarningCodeDetails//m:errorCode"; |
||
| 48 | const Q_D_CAT = "//m:fopDescription/m:mopDescription/m:mopElementError/m:errorOrWarningCodeDetails//m:errorCategory"; |
||
|
|
|||
| 49 | const Q_D_MSG = "//m:fopDescription/m:mopDescription/m:mopElementError/m:errorWarningDescription/m:freeText"; |
||
| 50 | //Authorization failure: |
||
| 51 | const Q_A_ERR = "//m:fopDescription/m:mopDescription/m:paymentModule//m:paymentStatusError/m:errorOrWarningCodeDetails//m:errorCode"; |
||
| 52 | const Q_A_CAT = "//m:fopDescription/m:mopDescription/m:paymentModule//m:paymentStatusError/m:errorOrWarningCodeDetails//m:errorCategory"; |
||
| 53 | const Q_A_MSG = "//m:fopDescription/m:mopDescription/m:paymentModule//m:paymentStatusError/m:errorWarningDescription/m:freeText"; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * FOP_CreateFormOfPayment Analyze the result from the message operation and check for any error messages |
||
| 57 | * |
||
| 58 | * @param SendResult $response |
||
| 59 | * @return Result |
||
| 60 | * @throws Exception |
||
| 61 | */ |
||
| 62 | 4 | public function analyze(SendResult $response) |
|
| 63 | { |
||
| 64 | //TODO |
||
| 65 | 4 | $analyzeResponse = new Result($response); |
|
| 66 | |||
| 67 | 4 | $domXpath = $this->makeDomXpath($response->responseXml); |
|
| 68 | |||
| 69 | //General error level in the transmissionError location: |
||
| 70 | 4 | $errorCodeNodeList = $domXpath->query(self::Q_G_ERR); |
|
| 71 | |||
| 72 | 4 | View Code Duplication | if ($errorCodeNodeList->length > 0) { |
| 73 | 1 | $errorCatNode = $domXpath->query(self::Q_G_CAT)->item(0); |
|
| 74 | 1 | $analyzeResponse->setStatus($this->makeStatusForPotentiallyNonExistent($errorCatNode)); |
|
| 75 | |||
| 76 | 1 | $code = $errorCodeNodeList->item(0)->nodeValue; |
|
| 77 | 1 | $errorTextNodeList = $domXpath->query(self::Q_G_MSG); |
|
| 78 | 1 | $message = $this->makeMessageFromMessagesNodeList($errorTextNodeList); |
|
| 79 | |||
| 80 | 1 | $analyzeResponse->messages[] = new Result\NotOk($code, trim($message), 'general'); |
|
| 81 | 1 | } |
|
| 82 | |||
| 83 | //FP level errors via the fopDescription/fpElementError data: |
||
| 84 | 4 | $errorCodeNodeList = $domXpath->query(self::Q_F_ERR); |
|
| 85 | |||
| 86 | 4 | View Code Duplication | if ($errorCodeNodeList->length > 0) { |
| 87 | 1 | $errorCatNode = $domXpath->query(self::Q_F_CAT)->item(0); |
|
| 88 | 1 | $analyzeResponse->setStatus($this->makeStatusForPotentiallyNonExistent($errorCatNode)); |
|
| 89 | |||
| 90 | 1 | $code = $errorCodeNodeList->item(0)->nodeValue; |
|
| 91 | 1 | $errorTextNodeList = $domXpath->query(self::Q_F_MSG); |
|
| 92 | 1 | $message = $this->makeMessageFromMessagesNodeList($errorTextNodeList); |
|
| 93 | |||
| 94 | 1 | $analyzeResponse->messages[] = new Result\NotOk($code, trim($message), 'fp'); |
|
| 95 | 1 | } |
|
| 96 | |||
| 97 | //Deficient FOP level errors: |
||
| 98 | 4 | $errorCodeNodeList = $domXpath->query(self::Q_D_ERR); |
|
| 99 | |||
| 100 | 4 | View Code Duplication | if ($errorCodeNodeList->length > 0) { |
| 101 | 1 | $errorCatNode = $domXpath->query(self::Q_D_CAT)->item(0); |
|
| 102 | 1 | $analyzeResponse->setStatus($this->makeStatusForPotentiallyNonExistent($errorCatNode)); |
|
| 103 | |||
| 104 | 1 | $code = $errorCodeNodeList->item(0)->nodeValue; |
|
| 105 | |||
| 106 | 1 | $errorTextNodeList = $domXpath->query(self::Q_D_MSG); |
|
| 107 | 1 | $message = $this->makeMessageFromMessagesNodeList($errorTextNodeList); |
|
| 108 | |||
| 109 | 1 | $analyzeResponse->messages[] = new Result\NotOk($code, trim($message), 'deficient_fop'); |
|
| 110 | 1 | } |
|
| 111 | |||
| 112 | //authorization failure: |
||
| 113 | 4 | $errorCodeNodeList = $domXpath->query(self::Q_A_ERR); |
|
| 114 | |||
| 115 | 4 | View Code Duplication | if ($errorCodeNodeList->length > 0) { |
| 116 | 1 | $errorCatNode = $domXpath->query(self::Q_A_CAT)->item(0); |
|
| 117 | 1 | $analyzeResponse->setStatus($this->makeStatusForPotentiallyNonExistent($errorCatNode)); |
|
| 118 | |||
| 119 | 1 | $code = $errorCodeNodeList->item(0)->nodeValue; |
|
| 120 | |||
| 121 | 1 | $errorTextNodeList = $domXpath->query(self::Q_A_MSG); |
|
| 122 | 1 | $message = $this->makeMessageFromMessagesNodeList($errorTextNodeList); |
|
| 123 | |||
| 124 | 1 | $analyzeResponse->messages[] = new Result\NotOk($code, trim($message), 'authorization_failure'); |
|
| 125 | 1 | } |
|
| 126 | |||
| 127 | 4 | return $analyzeResponse; |
|
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Make status from a category DOMNode or default status. |
||
| 132 | * |
||
| 133 | * @param \DOMNode|null $errorCatNode |
||
| 134 | * @return string |
||
| 135 | */ |
||
| 136 | 2 | protected function makeStatusForPotentiallyNonExistent($errorCatNode) |
|
| 146 | } |
||
| 147 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.