Complex classes like WsSecurityFilterRequest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use WsSecurityFilterRequest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class WsSecurityFilterRequest extends AbstractWsSecurityFilter |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * Web Services Security: SOAP Message Security 1.0 (WS-Security 2004) |
||
| 14 | */ |
||
| 15 | const NAME_WSS_SMS = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0'; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Web Services Security UsernameToken Profile 1.0 |
||
| 19 | */ |
||
| 20 | const NAME_WSS_UTP = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0'; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Web Services Security: SOAP Message Security 1.1 (WS-Security 2004) |
||
| 24 | */ |
||
| 25 | const NAME_WSS_SMS_1_1 = 'http://docs.oasis-open.org/wss/oasis-wss-soap-message-security-1.1'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Web Services Security X.509 Certificate Token Profile |
||
| 29 | */ |
||
| 30 | const NAME_WSS_X509 = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * The date format to be used with {@link \DateTime} |
||
| 34 | */ |
||
| 35 | const DATETIME_FORMAT = 'Y-m-d\TH:i:s.000\Z'; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * (X509 3.2.1) Reference to a Subject Key Identifier |
||
| 39 | */ |
||
| 40 | const TOKEN_REFERENCE_SUBJECT_KEY_IDENTIFIER = 0; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * (X509 3.2.1) Reference to a Security Token |
||
| 44 | */ |
||
| 45 | const TOKEN_REFERENCE_SECURITY_TOKEN = 1; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * (SMS_1.1 7.3) Key Identifiers |
||
| 49 | */ |
||
| 50 | const TOKEN_REFERENCE_THUMBPRINT_SHA1 = 2; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * (SMS 10) Add security timestamp. |
||
| 54 | * |
||
| 55 | * @var boolean |
||
| 56 | */ |
||
| 57 | private $addTimestamp = true; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Encrypt the signature? |
||
| 61 | * |
||
| 62 | * @var boolean |
||
| 63 | */ |
||
| 64 | private $encryptSignature = false; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * (SMS 10) Security timestamp expires time in seconds. |
||
| 68 | * |
||
| 69 | * @var int |
||
| 70 | */ |
||
| 71 | private $expires = 300; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Sign all headers. |
||
| 75 | * |
||
| 76 | * @var boolean |
||
| 77 | */ |
||
| 78 | private $signAllHeaders = false; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var \DateTime |
||
| 82 | */ |
||
| 83 | private $initialTimestamp; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * (X509 3.2) Token reference type for encryption. |
||
| 87 | * |
||
| 88 | * @var int |
||
| 89 | */ |
||
| 90 | private $tokenReferenceEncryption = null; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * (X509 3.2) Token reference type for signature. |
||
| 94 | * |
||
| 95 | * @var int |
||
| 96 | */ |
||
| 97 | private $tokenReferenceSignature = null; |
||
| 98 | |||
| 99 | |||
| 100 | public function setTimestampOptions($addTimestamp = true, $expires = 300) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @param \DateTime $initialTimestamp |
||
| 108 | */ |
||
| 109 | public function __construct(\DateTime $initialTimestamp = null) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Set security options. |
||
| 116 | * |
||
| 117 | * @param int $tokenReference self::TOKEN_REFERENCE_SUBJECT_KEY_IDENTIFIER | self::TOKEN_REFERENCE_SECURITY_TOKEN | self::TOKEN_REFERENCE_THUMBPRINT_SHA1 |
||
| 118 | * @param boolean $encryptSignature Encrypt signature |
||
| 119 | * |
||
| 120 | * @return void |
||
| 121 | */ |
||
| 122 | public function setSecurityOptionsEncryption($tokenReference, $encryptSignature = false) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Set security options. |
||
| 130 | * |
||
| 131 | * @param int $tokenReference self::TOKEN_REFERENCE_SUBJECT_KEY_IDENTIFIER | self::TOKEN_REFERENCE_SECURITY_TOKEN | self::TOKEN_REFERENCE_THUMBPRINT_SHA1 |
||
| 132 | * @param boolean $signAllHeaders Sign all headers? |
||
| 133 | * |
||
| 134 | * @return void |
||
| 135 | */ |
||
| 136 | public function setSecurityOptionsSignature($tokenReference, $signAllHeaders = false) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Adds the configured KeyInfo to the parentNode. |
||
| 144 | * |
||
| 145 | * @param \DOMDocument $dom |
||
| 146 | * @param int $tokenReference Token reference type |
||
| 147 | * @param string $guid Unique ID |
||
| 148 | * @param XmlSecurityKey $xmlSecurityKey XML security key |
||
| 149 | * |
||
| 150 | * @return \DOMElement |
||
| 151 | */ |
||
| 152 | private function createKeyInfo(\DOMDocument $dom, $tokenReference, $guid, XmlSecurityKey $xmlSecurityKey = null) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Create a list of \DOMNodes that should be encrypted. |
||
| 190 | * |
||
| 191 | * @param \DOMDocument $dom DOMDocument to query |
||
| 192 | * |
||
| 193 | * @return \DOMNodeList |
||
| 194 | */ |
||
| 195 | private function createNodeListForEncryption(\DOMDocument $dom) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Create a list of \DOMNodes that should be signed. |
||
| 211 | * |
||
| 212 | * @param \DOMDocument $dom DOMDocument to query |
||
| 213 | * @param \DOMElement $security Security element |
||
| 214 | * |
||
| 215 | * @return array(\DOMNode) |
||
| 216 | */ |
||
| 217 | private function createNodeListForSigning(\DOMDocument $dom, \DOMElement $security) |
||
| 240 | |||
| 241 | |||
| 242 | /** |
||
| 243 | * Modify the given request XML. |
||
| 244 | * |
||
| 245 | * @param \DOMElement $currentNode, |
||
| 246 | * @param Security $securityData |
||
| 247 | * |
||
| 248 | * @return \DOMElement |
||
| 249 | */ |
||
| 250 | public function filterDom(\DOMElement $currentNode, Security $securityData) |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Generate a pseudo-random version 4 UUID. |
||
| 297 | * |
||
| 298 | * @see http://de.php.net/manual/en/function.uniqid.php#94959 |
||
| 299 | * |
||
| 300 | * @return string |
||
| 301 | */ |
||
| 302 | private static function generateUUID() |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @param \DOMElement $security |
||
| 324 | * @param \DateTime $dt |
||
| 325 | */ |
||
| 326 | private function handleTimestamp(\DOMElement $security, \DateTime $dt) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * @param \DOMElement $security |
||
| 344 | * @param $dt |
||
| 345 | * @param Security $securityData |
||
| 346 | */ |
||
| 347 | private function handleUsername(\DOMElement $security, $dt, Security $securityData) |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @param \DOMElement $security |
||
| 386 | * @return \DOMElement |
||
| 387 | */ |
||
| 388 | private function handleSignature(\DOMElement $security) |
||
| 432 | |||
| 433 | /** |
||
| 434 | * @param \DOMElement $security |
||
| 435 | * @param \DOMElement $signature |
||
| 436 | */ |
||
| 437 | private function handleEncryption(\DOMElement $security, \DOMElement $signature) |
||
| 459 | } |
||
| 460 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: