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 |
||
| 35 | abstract class StandardResponseHandler implements MessageResponseHandler |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * Default namespace prefix we'll be using for xpath queries |
||
| 39 | * |
||
| 40 | * Why not "m"? It's as good as any other letter. |
||
| 41 | */ |
||
| 42 | const XMLNS_PREFIX = "m"; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Analyze response by looking for error, category and message with the provided XPATH queries |
||
| 46 | * |
||
| 47 | * xpath queries must be prefixed with the namespace self::XMLNS_PREFIX |
||
| 48 | * |
||
| 49 | * @param SendResult $response |
||
| 50 | * @param string $qErr XPATH query for fetching error code (first node is used) |
||
| 51 | * @param string $qCat XPATH query for fetching error category (first node is used) |
||
| 52 | * @param string $qMsg XPATH query for fetching error messages (all nodes are used) |
||
| 53 | * @param string|null $errLevel Optional custom error level string. |
||
| 54 | * @return Result |
||
| 55 | */ |
||
| 56 | 9 | protected function analyzeWithErrCodeCategoryMsgQuery(SendResult $response, $qErr, $qCat, $qMsg, $errLevel = null) |
|
| 83 | |||
| 84 | /** |
||
| 85 | * Analyze response by looking for error, category and message in nodes specified by name |
||
| 86 | * |
||
| 87 | * @param SendResult $response |
||
| 88 | * @param string $nodeErr Node name of the node containing the error code (first node is used) |
||
| 89 | * @param string $nodeCat Node name of the node containing the error category (first node is used) |
||
| 90 | * @param string $nodeMsg Node name of the node containing the error messages (all nodes are used) |
||
| 91 | * @return Result |
||
| 92 | */ |
||
| 93 | 30 | protected function analyzeWithErrCodeCategoryMsgNodeName(SendResult $response, $nodeErr, $nodeCat, $nodeMsg) |
|
| 120 | |||
| 121 | |||
| 122 | /** |
||
| 123 | * @param SendResult $response WebService message Send Result |
||
| 124 | * @return Result |
||
| 125 | * @throws Exception |
||
| 126 | */ |
||
| 127 | 26 | protected function analyzeSimpleResponseErrorCodeAndMessage($response) |
|
| 136 | |||
| 137 | /** |
||
| 138 | * @param SendResult $response WebService message Send Result |
||
| 139 | * @return Result |
||
| 140 | * @throws Exception |
||
| 141 | */ |
||
| 142 | 4 | protected function analyzeSimpleResponseErrorCodeAndMessageStatusCode($response) |
|
| 151 | |||
| 152 | /** |
||
| 153 | * Make a Xpath-queryable object for an XML string |
||
| 154 | * |
||
| 155 | * registers TNS namespace with prefix self::XMLNS_PREFIX |
||
| 156 | * |
||
| 157 | * @param string $response |
||
| 158 | * @return \DOMXPath |
||
| 159 | * @throws Exception when there's a problem loading the message |
||
| 160 | */ |
||
| 161 | 46 | protected function makeDomXpath($response) |
|
| 173 | |||
| 174 | /** |
||
| 175 | * @param string $response |
||
| 176 | * @return \DOMDocument |
||
| 177 | * @throws Exception when there's a problem loading the message |
||
| 178 | */ |
||
| 179 | 84 | protected function loadDomDocument($response) |
|
| 190 | |||
| 191 | /** |
||
| 192 | * Converts a status code found in an error message to the appropriate status level |
||
| 193 | * |
||
| 194 | * if no node found (= $qualifier is a null), $defaultStatus will be used |
||
| 195 | * |
||
| 196 | * @param string|null $qualifier |
||
| 197 | * @param string $defaultStatus the default status to fall back to if no qualifier is present |
||
| 198 | * @return string Result::STATUS_* |
||
| 199 | */ |
||
| 200 | 39 | protected function makeStatusFromErrorQualifier($qualifier, $defaultStatus = Result::STATUS_ERROR) |
|
| 225 | |||
| 226 | /** |
||
| 227 | * Convert a DomNodeList of nodes containing a (potentially partial) error message into a string. |
||
| 228 | * |
||
| 229 | * @param \DOMNodeList $errorTextNodeList |
||
| 230 | * @return string|null |
||
| 231 | */ |
||
| 232 | 41 | protected function makeMessageFromMessagesNodeList($errorTextNodeList) |
|
| 244 | } |
||
| 245 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.