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 |
||
33 | class SoapHeader4 extends Base |
||
34 | { |
||
35 | /** |
||
36 | * XPATH query to retrieve the SOAPAction from the WSDL for a given message. |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | const XPATH_OPERATION_ACTION = 'string(//wsdl:operation[./@name="%s"]/soap:operation/@soapAction)'; |
||
41 | /** |
||
42 | * XPATH query to retrieve the server endpoint from the WSDL. |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | const XPATH_ENDPOINT = 'string(/wsdl:definitions/wsdl:service/wsdl:port/soap:address/@location)'; |
||
47 | |||
48 | /** |
||
49 | * SoapHeader - Session |
||
50 | * TransactionStatusCode for starting new sessions. |
||
51 | */ |
||
52 | const TRANSACTION_STATUS_CODE_START = 'Start'; |
||
53 | |||
54 | /** |
||
55 | * SoapHeader - Session |
||
56 | * TransactionStatusCode for active stateful sessions. |
||
57 | */ |
||
58 | const TRANSACTION_STATUS_CODE_INSERIES = 'InSeries'; |
||
59 | |||
60 | /** |
||
61 | * SoapHeader - Session |
||
62 | * TransactionStatusCode for ending sessions. |
||
63 | */ |
||
64 | const TRANSACTION_STATUS_CODE_END = 'End'; |
||
65 | |||
66 | /** |
||
67 | * Switch between stateful & stateless sessions. Default: stateful |
||
68 | * |
||
69 | * @var bool |
||
70 | */ |
||
71 | protected $isStateful = true; |
||
72 | |||
73 | protected $enableTransactionFlowLink = false; |
||
74 | |||
75 | /** |
||
76 | * TransactionFlowLink Consumer ID |
||
77 | * |
||
78 | * @var string|null |
||
79 | */ |
||
80 | protected $consumerId; |
||
81 | |||
82 | /** |
||
83 | * @param bool $stateful |
||
84 | */ |
||
85 | 192 | public function setStateful($stateful) |
|
89 | |||
90 | /** |
||
91 | * Check whether we are running in stateful mode (true) or in stateless mode (false) |
||
92 | * |
||
93 | * @return bool |
||
94 | */ |
||
95 | 84 | public function isStateful() |
|
99 | |||
100 | /** |
||
101 | * Is the TransactionFlowLink header enabled? |
||
102 | * |
||
103 | * @return bool |
||
104 | */ |
||
105 | 60 | public function isTransactionFlowLinkEnabled() |
|
109 | |||
110 | /** |
||
111 | * Enable or disable TransactionFlowLink header |
||
112 | * |
||
113 | * @param bool $enabled |
||
114 | */ |
||
115 | 192 | public function setTransactionFlowLink($enabled) |
|
119 | |||
120 | /** |
||
121 | * Get the TransactionFlowLink Consumer ID |
||
122 | * |
||
123 | * @param bool $generate Whether to generate a consumer ID |
||
124 | * @return string|null |
||
125 | */ |
||
126 | 12 | public function getConsumerId($generate = false) |
|
134 | |||
135 | /** |
||
136 | * Set the TransactionFlowLink Consumer ID |
||
137 | * |
||
138 | * @param string $id |
||
139 | * @return void |
||
140 | */ |
||
141 | 192 | public function setConsumerId($id) |
|
145 | |||
146 | /** |
||
147 | * Handles authentication & sessions |
||
148 | * |
||
149 | * If authenticated, increment sequence number for next message and set session info to soapheader |
||
150 | * If not, set auth info to soapheader |
||
151 | * |
||
152 | * @uses $this->isAuthenticated |
||
153 | * @uses $this->sessionData |
||
154 | * @param string $messageName |
||
155 | * @param array $messageOptions |
||
156 | */ |
||
157 | 32 | protected function prepareForNextMessage($messageName, $messageOptions) |
|
168 | |||
169 | |||
170 | /** |
||
171 | * Handles post message actions |
||
172 | * |
||
173 | * - look for session info and set status variables |
||
174 | * - checks for message errors? |
||
175 | * - ends terminated sessions |
||
176 | * |
||
177 | * @param string $messageName |
||
178 | * @param string $lastResponse |
||
179 | * @param array $messageOptions |
||
180 | * @param mixed $result |
||
181 | * @return void |
||
182 | */ |
||
183 | 32 | protected function handlePostMessage($messageName, $lastResponse, $messageOptions, $result) |
|
196 | |||
197 | /** |
||
198 | * @param string $responseMsg the full response XML received. |
||
199 | * @return array |
||
200 | */ |
||
201 | 20 | protected function getSessionDataFromHeader($responseMsg) |
|
235 | |||
236 | /** |
||
237 | * Create the Soap Headers to be used on the subsequent request. |
||
238 | * |
||
239 | * This depends on the current Session Data (if there is an active session) and |
||
240 | * the Session Handler parameters (to create a new or stateless session) |
||
241 | * |
||
242 | * You can also terminate the session with $doEndSession = true |
||
243 | * |
||
244 | * @param array $sessionData |
||
245 | * @param Client\Params\SessionHandlerParams $params |
||
246 | * @param string $messageName |
||
247 | * @param array $messageOptions |
||
248 | * @return \SoapHeader[]|null |
||
|
|||
249 | */ |
||
250 | 60 | protected function createSoapHeaders($sessionData, $params, $messageName, $messageOptions) |
|
379 | |||
380 | /** |
||
381 | * Get the Web Services server Endpoint from the WSDL. |
||
382 | * |
||
383 | * @param string $wsdlFilePath |
||
384 | * @param string $messageName |
||
385 | * @return string|null |
||
386 | */ |
||
387 | 60 | protected function getEndpointFromWsdl($wsdlFilePath, $messageName) |
|
397 | |||
398 | /** |
||
399 | * Get the SOAPAction for a given message from the WSDL contents. |
||
400 | * |
||
401 | * @param string $wsdlFilePath |
||
402 | * @param string $messageName |
||
403 | * @return string|null |
||
404 | */ |
||
405 | 60 | protected function getActionFromWsdl($wsdlFilePath, $messageName) |
|
415 | |||
416 | /** |
||
417 | * Generate a GUID |
||
418 | * |
||
419 | * @return string |
||
420 | */ |
||
421 | 60 | protected function generateGuid() |
|
435 | |||
436 | /** |
||
437 | * @param string $originator |
||
438 | * @param string $nonce |
||
439 | * @param string $pwDigest |
||
440 | * @param string $creationTimeString |
||
441 | * @return string |
||
442 | */ |
||
443 | 48 | protected function generateSecurityHeaderRawXml($originator, $nonce, $pwDigest, $creationTimeString) |
|
454 | |||
455 | |||
456 | /** |
||
457 | * @param string $nonceBase |
||
458 | * @param string $creationString |
||
459 | * @return string |
||
460 | */ |
||
461 | 44 | protected function generateUniqueNonce($nonceBase, $creationString) |
|
472 | |||
473 | /** |
||
474 | * Generates a Password Digest following this algorithm: |
||
475 | * HashedPassword = Base64(SHA-1( nonce + created + SHA-1 ( password ))) |
||
476 | * as defined in |
||
477 | * https://webservices.amadeus.com/extranet/kdbViewDocument.do?externalId=wikidoc_web_services_embedded_security_implementation_guide_header_entries_ws-security_usernametoken&docStatus=Published&mpId=fla__1__technical |
||
478 | * |
||
479 | * EXAMPLE: with: |
||
480 | * Nonce in Base 64 = 'PZgFvh5439plJpKpIyf5ucmXhNU=' |
||
481 | * Timestamp = '2013-01-11T09:41:03Z' |
||
482 | * Clear Password = 'WBSPassword' |
||
483 | * The digest algorithm returns the Encrypted Password in Base 64: |
||
484 | * HshPwd = 'ic3AOJElVpvkz9ZBKd105Siry28=' |
||
485 | * |
||
486 | * @param string $password CLEARTEXT password (NOT the base64 encoded password used in Security_Authenticate) |
||
487 | * @param string $creationString message creation datetime |
||
488 | * UTC Format: yyyy-mm-ddTHH:MM:SSZ or yyyy-mm-ddTHH:MM:SS.sssZ |
||
489 | * @param string $messageNonce Random unique string |
||
490 | * @return string The generated Password Digest |
||
491 | */ |
||
492 | 68 | protected function generatePasswordDigest($password, $creationString, $messageNonce) |
|
496 | |||
497 | /** |
||
498 | * @param \DateTime $creationDateTime |
||
499 | * @param string $micro |
||
500 | * @return string |
||
501 | */ |
||
502 | 44 | protected function createDateTimeStringForAuth($creationDateTime, $micro) |
|
508 | |||
509 | /** |
||
510 | * Make SoapClient options for Soap Header 4 handler |
||
511 | * |
||
512 | * @return array |
||
513 | */ |
||
514 | 8 | View Code Duplication | protected function makeSoapClientOptions() |
525 | |||
526 | /** |
||
527 | * Check is called message is not Security_Authenticate. |
||
528 | * |
||
529 | * @param $messageName |
||
530 | * @return bool |
||
531 | */ |
||
532 | 48 | protected function isNotSecurityAuthenticateMessage($messageName) |
|
536 | |||
537 | /** |
||
538 | * Return transaction code for stateful requests. |
||
539 | * |
||
540 | * @param string $messageName name of request message (e.g. Security_Authenticate) |
||
541 | * @param array $messageOptions |
||
542 | * @return string |
||
543 | */ |
||
544 | 12 | private function getStatefulStatusCode($messageName, array $messageOptions) |
|
559 | } |
||
560 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.