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 WsdlAnalyser |
||
| 37 | { |
||
| 38 | /** |
||
| 39 | * XPATH query to retrieve all operations from a WSDL |
||
| 40 | * |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | const XPATH_ALL_OPERATIONS = '/wsdl:definitions/wsdl:portType/wsdl:operation/@name'; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * XPATH query to retrieve WSDL imports from a WSDL. |
||
| 47 | */ |
||
| 48 | const XPATH_IMPORTS = '/wsdl:definitions/wsdl:import/@location'; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * XPATH query to retrieve the full operation name + version from a WSDL for a given operation. |
||
| 52 | * |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | const XPATH_VERSION_FOR_OPERATION = "string(/wsdl:definitions/wsdl:message[contains(./@name, '%s_')]/@name)"; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Alternate XPATH query to retrieve the full operation name + version from a WSDL for a given operation |
||
| 59 | * |
||
| 60 | * Necessary for "interface" wsdls |
||
| 61 | * |
||
| 62 | * @var string |
||
| 63 | */ |
||
| 64 | const XPATH_ALT_VERSION_FOR_OPERATION = "string(//wsdl:operation[contains(./@name, '%s')]/wsdl:input/@message)"; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * List of WSDL ID's and their path |
||
| 68 | * |
||
| 69 | * format: |
||
| 70 | * [ |
||
| 71 | * '7d36c7b8' => '/path/to/wsdl.wsdl' |
||
| 72 | * ] |
||
| 73 | * |
||
| 74 | * @var array |
||
| 75 | */ |
||
| 76 | public static $wsdlIds = []; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Dom Document array where the WSDL's contents will be loaded |
||
| 80 | * |
||
| 81 | * format: |
||
| 82 | * [ |
||
| 83 | * '7d36c7b8' => \DOMDocument, |
||
| 84 | * '7e84f2537' => \DOMDocument |
||
| 85 | * ] |
||
| 86 | * |
||
| 87 | * @var array |
||
| 88 | */ |
||
| 89 | protected static $wsdlDomDoc; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * To query the WSDL contents |
||
| 93 | * |
||
| 94 | * format: |
||
| 95 | * [ |
||
| 96 | * '7d36c7b8' => \DOMXpath, |
||
| 97 | * '7e84f2537' => \DOMXpath |
||
| 98 | * ] |
||
| 99 | * |
||
| 100 | * @var array |
||
| 101 | */ |
||
| 102 | protected static $wsdlDomXpath; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Loads messages & versions from WSDL. |
||
| 106 | * |
||
| 107 | * @return array |
||
| 108 | */ |
||
| 109 | 31 | public static function loadMessagesAndVersions($wsdls) |
|
| 159 | |||
| 160 | /** |
||
| 161 | * Get Messages & Versions from an imported WSDL file |
||
| 162 | * |
||
| 163 | * Imported wsdl's are a little different, they require a different query |
||
| 164 | * to extract the version nrs. |
||
| 165 | * |
||
| 166 | * @param string $import |
||
| 167 | * @param string $wsdlPath |
||
| 168 | * @param string $wsdlIdentifier |
||
| 169 | * @return array |
||
| 170 | * @throws InvalidWsdlFileException when the WSDL import could not be loaded. |
||
| 171 | */ |
||
| 172 | 2 | protected static function getMessagesAndVersionsFromImportedWsdl($import, $wsdlPath, $wsdlIdentifier) |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Load the WSDL contents to a queryable DOMXpath. |
||
| 224 | * |
||
| 225 | * @param string $wsdlFilePath |
||
| 226 | * @param string $wsdlId |
||
| 227 | * @uses $this->wsdlDomDoc |
||
| 228 | * @uses $this->wsdlDomXpath |
||
| 229 | * @throws InvalidWsdlFileException when WSDL cannot be found. |
||
| 230 | */ |
||
| 231 | 31 | public static function loadWsdlXpath($wsdlFilePath, $wsdlId) |
|
| 253 | |||
| 254 | /** |
||
| 255 | * extractMessageVersion |
||
| 256 | * |
||
| 257 | * extracts "4.1" from a string like "Security_SignOut_4_1" |
||
| 258 | * or "1.00" from a string like "tns:AMA_MediaGetMediaRQ_1.000" |
||
| 259 | * |
||
| 260 | * @param string $fullVersionString |
||
| 261 | * @return string |
||
| 262 | */ |
||
| 263 | 31 | protected static function extractMessageVersion($fullVersionString) |
|
| 271 | |||
| 272 | /** |
||
| 273 | * Generates a unique identifier for a wsdl based on its path. |
||
| 274 | * |
||
| 275 | * @param string $wsdlPath |
||
| 276 | * |
||
| 277 | * @return string |
||
| 278 | */ |
||
| 279 | 31 | protected static function makeWsdlIdentifier($wsdlPath) |
|
| 283 | |||
| 284 | /** |
||
| 285 | * Evaluate an XPATH query on a given WSDL |
||
| 286 | * |
||
| 287 | * @param string $wsdlId |
||
| 288 | * @param string $wsdlFilePath |
||
| 289 | * @param string $xpath XPATH query |
||
| 290 | * @return string|null |
||
| 291 | */ |
||
| 292 | 11 | public static function exaluateXpathQueryOnWsdl($wsdlId, $wsdlFilePath, $xpath) |
|
| 298 | } |
||
| 299 |
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.