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:
Complex classes like AuthnRequest 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 AuthnRequest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class AuthnRequest extends Request |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * The options for what type of name identifier should be returned. |
||
| 19 | * |
||
| 20 | * @var array |
||
| 21 | */ |
||
| 22 | private $nameIdPolicy; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Whether the Identity Provider must authenticate the user again. |
||
| 26 | * |
||
| 27 | * @var bool |
||
| 28 | */ |
||
| 29 | private $forceAuthn; |
||
| 30 | |||
| 31 | |||
| 32 | /** |
||
| 33 | * Optional ProviderID attribute |
||
| 34 | * |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | private $ProviderName; |
||
| 38 | |||
| 39 | |||
| 40 | /** |
||
| 41 | * Set to true if this request is passive. |
||
| 42 | * |
||
| 43 | * @var bool. |
||
| 44 | */ |
||
| 45 | private $isPassive; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * The list of providerIDs in this request's scoping element |
||
| 49 | * |
||
| 50 | * @var array |
||
| 51 | */ |
||
| 52 | private $IDPList = array(); |
||
| 53 | |||
| 54 | /** |
||
| 55 | * The ProxyCount in this request's scoping element |
||
| 56 | * |
||
| 57 | * @var int |
||
| 58 | */ |
||
| 59 | private $ProxyCount = null; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * The RequesterID list in this request's scoping element |
||
| 63 | * |
||
| 64 | * @var array |
||
| 65 | */ |
||
| 66 | |||
| 67 | private $RequesterID = array(); |
||
| 68 | |||
| 69 | /** |
||
| 70 | * The URL of the asertion consumer service where the response should be delivered. |
||
| 71 | * |
||
| 72 | * @var string|null |
||
| 73 | */ |
||
| 74 | private $assertionConsumerServiceURL; |
||
| 75 | |||
| 76 | |||
| 77 | /** |
||
| 78 | * What binding should be used when sending the response. |
||
| 79 | * |
||
| 80 | * @var string|null |
||
| 81 | */ |
||
| 82 | private $protocolBinding; |
||
| 83 | |||
| 84 | |||
| 85 | /** |
||
| 86 | * The index of the AttributeConsumingService. |
||
| 87 | * |
||
| 88 | * @var int|null |
||
| 89 | */ |
||
| 90 | private $attributeConsumingServiceIndex; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * The index of the AssertionConsumerService. |
||
| 94 | * |
||
| 95 | * @var int|null |
||
| 96 | */ |
||
| 97 | private $assertionConsumerServiceIndex; |
||
| 98 | |||
| 99 | |||
| 100 | /** |
||
| 101 | * What authentication context was requested. |
||
| 102 | * |
||
| 103 | * Array with the following elements. |
||
| 104 | * - AuthnContextClassRef (required) |
||
| 105 | * - Comparison (optinal) |
||
| 106 | * |
||
| 107 | * @var array |
||
| 108 | */ |
||
| 109 | private $requestedAuthnContext; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @var \SAML2\XML\saml\SubjectConfirmation[] |
||
| 113 | */ |
||
| 114 | private $subjectConfirmation = array(); |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @var string |
||
| 118 | */ |
||
| 119 | private $encryptedNameId; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @var string |
||
| 123 | */ |
||
| 124 | private $nameId; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Constructor for SAML 2 authentication request messages. |
||
| 128 | * |
||
| 129 | * @param \DOMElement|null $xml The input message. |
||
| 130 | * @throws \Exception |
||
| 131 | */ |
||
| 132 | public function __construct(\DOMElement $xml = null) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @param $xml |
||
| 171 | * |
||
| 172 | * @throws \Exception |
||
| 173 | */ |
||
| 174 | private function parseSubject(\DOMElement $xml) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @param \DOMElement $xml |
||
| 211 | * |
||
| 212 | * @throws \Exception |
||
| 213 | */ |
||
| 214 | protected function parseNameIdPolicy(\DOMElement $xml) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @param \DOMElement $xml |
||
| 235 | */ |
||
| 236 | protected function parseRequestedAuthnContext(\DOMElement $xml) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @param \DOMElement $xml |
||
| 264 | * |
||
| 265 | * @throws \Exception |
||
| 266 | */ |
||
| 267 | protected function parseScoping(\DOMElement $xml) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Retrieve the NameIdPolicy. |
||
| 296 | * |
||
| 297 | * @see \SAML2\AuthnRequest::setNameIdPolicy() |
||
| 298 | * @return array The NameIdPolicy. |
||
| 299 | */ |
||
| 300 | public function getNameIdPolicy() |
||
| 304 | |||
| 305 | |||
| 306 | /** |
||
| 307 | * Set the NameIDPolicy. |
||
| 308 | * |
||
| 309 | * This function accepts an array with the following options: |
||
| 310 | * - 'Format' (string) |
||
| 311 | * - 'SPNameQualifier' (string) |
||
| 312 | * - 'AllowCreate' (bool) |
||
| 313 | * |
||
| 314 | * @param array $nameIdPolicy The NameIDPolicy. |
||
| 315 | */ |
||
| 316 | public function setNameIdPolicy(array $nameIdPolicy) |
||
| 330 | |||
| 331 | |||
| 332 | /** |
||
| 333 | * Retrieve the value of the ForceAuthn attribute. |
||
| 334 | * |
||
| 335 | * @return bool The ForceAuthn attribute. |
||
| 336 | */ |
||
| 337 | public function getForceAuthn() |
||
| 341 | |||
| 342 | |||
| 343 | /** |
||
| 344 | * Set the value of the ForceAuthn attribute. |
||
| 345 | * |
||
| 346 | * @param bool $forceAuthn The ForceAuthn attribute. |
||
| 347 | */ |
||
| 348 | public function setForceAuthn($forceAuthn) |
||
| 354 | |||
| 355 | |||
| 356 | /** |
||
| 357 | * Retrieve the value of the ProviderName attribute. |
||
| 358 | * |
||
| 359 | * @return string The ProviderName attribute. |
||
| 360 | */ |
||
| 361 | public function getProviderName() |
||
| 365 | |||
| 366 | |||
| 367 | /** |
||
| 368 | * Set the value of the ProviderName attribute. |
||
| 369 | * |
||
| 370 | * @param string $ProviderName The ProviderName attribute. |
||
| 371 | */ |
||
| 372 | public function setProviderName($ProviderName) |
||
| 378 | |||
| 379 | |||
| 380 | /** |
||
| 381 | * Retrieve the value of the IsPassive attribute. |
||
| 382 | * |
||
| 383 | * @return bool The IsPassive attribute. |
||
| 384 | */ |
||
| 385 | public function getIsPassive() |
||
| 389 | |||
| 390 | |||
| 391 | /** |
||
| 392 | * Set the value of the IsPassive attribute. |
||
| 393 | * |
||
| 394 | * @param bool $isPassive The IsPassive attribute. |
||
| 395 | */ |
||
| 396 | public function setIsPassive($isPassive) |
||
| 402 | |||
| 403 | |||
| 404 | /** |
||
| 405 | * This function sets the scoping for the request. |
||
| 406 | * See Core 3.4.1.2 for the definition of scoping. |
||
| 407 | * Currently we support an IDPList of idpEntries. |
||
| 408 | * |
||
| 409 | * Each idpEntries consists of an array, containing |
||
| 410 | * keys (mapped to attributes) and corresponding values. |
||
| 411 | * Allowed attributes: Loc, Name, ProviderID. |
||
| 412 | * |
||
| 413 | * For backward compatibility, an idpEntries can also |
||
| 414 | * be a string instead of an array, where each string |
||
| 415 | * is mapped to the value of attribute ProviderID. |
||
| 416 | */ |
||
| 417 | public function setIDPList($IDPList) |
||
| 422 | |||
| 423 | |||
| 424 | /** |
||
| 425 | * This function retrieves the list of providerIDs from this authentication request. |
||
| 426 | * Currently we only support a list of ipd ientity id's. |
||
| 427 | * @return array List of idp EntityIDs from the request |
||
| 428 | */ |
||
| 429 | public function getIDPList() |
||
| 433 | |||
| 434 | /** |
||
| 435 | * @param int $ProxyCount |
||
| 436 | */ |
||
| 437 | public function setProxyCount($ProxyCount) |
||
| 442 | |||
| 443 | /** |
||
| 444 | * @return int |
||
| 445 | */ |
||
| 446 | public function getProxyCount() |
||
| 450 | |||
| 451 | /** |
||
| 452 | * @param array $RequesterID |
||
| 453 | */ |
||
| 454 | public function setRequesterID(array $RequesterID) |
||
| 458 | |||
| 459 | /** |
||
| 460 | * @return array |
||
| 461 | */ |
||
| 462 | public function getRequesterID() |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Retrieve the value of the AssertionConsumerServiceURL attribute. |
||
| 469 | * |
||
| 470 | * @return string|null The AssertionConsumerServiceURL attribute. |
||
| 471 | */ |
||
| 472 | public function getAssertionConsumerServiceURL() |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Set the value of the AssertionConsumerServiceURL attribute. |
||
| 479 | * |
||
| 480 | * @param string|null $assertionConsumerServiceURL The AssertionConsumerServiceURL attribute. |
||
| 481 | */ |
||
| 482 | public function setAssertionConsumerServiceURL($assertionConsumerServiceURL) |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Retrieve the value of the ProtocolBinding attribute. |
||
| 491 | * |
||
| 492 | * @return string|null The ProtocolBinding attribute. |
||
| 493 | */ |
||
| 494 | public function getProtocolBinding() |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Set the value of the ProtocolBinding attribute. |
||
| 501 | * |
||
| 502 | * @param string $protocolBinding The ProtocolBinding attribute. |
||
| 503 | */ |
||
| 504 | public function setProtocolBinding($protocolBinding) |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Retrieve the value of the AttributeConsumingServiceIndex attribute. |
||
| 513 | * |
||
| 514 | * @return int|null The AttributeConsumingServiceIndex attribute. |
||
| 515 | */ |
||
| 516 | public function getAttributeConsumingServiceIndex() |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Set the value of the AttributeConsumingServiceIndex attribute. |
||
| 523 | * |
||
| 524 | * @param int|null $attributeConsumingServiceIndex The AttributeConsumingServiceIndex attribute. |
||
| 525 | */ |
||
| 526 | public function setAttributeConsumingServiceIndex($attributeConsumingServiceIndex) |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Retrieve the value of the AssertionConsumerServiceIndex attribute. |
||
| 535 | * |
||
| 536 | * @return int|null The AssertionConsumerServiceIndex attribute. |
||
| 537 | */ |
||
| 538 | public function getAssertionConsumerServiceIndex() |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Set the value of the AssertionConsumerServiceIndex attribute. |
||
| 545 | * |
||
| 546 | * @param int|null $assertionConsumerServiceIndex The AssertionConsumerServiceIndex attribute. |
||
| 547 | */ |
||
| 548 | public function setAssertionConsumerServiceIndex($assertionConsumerServiceIndex) |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Retrieve the RequestedAuthnContext. |
||
| 557 | * |
||
| 558 | * @return array|null The RequestedAuthnContext. |
||
| 559 | */ |
||
| 560 | public function getRequestedAuthnContext() |
||
| 564 | |||
| 565 | /** |
||
| 566 | * Set the RequestedAuthnContext. |
||
| 567 | * |
||
| 568 | * @param array|null $requestedAuthnContext The RequestedAuthnContext. |
||
| 569 | */ |
||
| 570 | public function setRequestedAuthnContext($requestedAuthnContext) |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Retrieve the NameId of the subject in the assertion. |
||
| 579 | * |
||
| 580 | * The returned NameId is in the format used by \SAML2\Utils::addNameId(). |
||
| 581 | * |
||
| 582 | * @see \SAML2\Utils::addNameId() |
||
| 583 | * @return array|null The name identifier of the assertion. |
||
| 584 | * @throws \Exception |
||
| 585 | */ |
||
| 586 | public function getNameId() |
||
| 594 | |||
| 595 | /** |
||
| 596 | * Set the NameId of the subject in the assertion. |
||
| 597 | * |
||
| 598 | * The NameId must be in the format accepted by \SAML2\Utils::addNameId(). |
||
| 599 | * |
||
| 600 | * @see \SAML2\Utils::addNameId() |
||
| 601 | * |
||
| 602 | * @param array|null $nameId The name identifier of the assertion. |
||
| 603 | */ |
||
| 604 | public function setNameId($nameId) |
||
| 610 | |||
| 611 | /** |
||
| 612 | * Encrypt the NameID in the AuthnRequest. |
||
| 613 | * |
||
| 614 | * @param XMLSecurityKey $key The encryption key. |
||
| 615 | */ |
||
| 616 | View Code Duplication | public function encryptNameId(XMLSecurityKey $key) |
|
| 641 | |||
| 642 | /** |
||
| 643 | * Decrypt the NameId of the subject in the assertion. |
||
| 644 | * |
||
| 645 | * @param XMLSecurityKey $key The decryption key. |
||
| 646 | * @param array $blacklist Blacklisted decryption algorithms. |
||
| 647 | */ |
||
| 648 | View Code Duplication | public function decryptNameId(XMLSecurityKey $key, array $blacklist = array()) |
|
| 661 | |||
| 662 | /** |
||
| 663 | * Retrieve the SubjectConfirmation elements we have in our Subject element. |
||
| 664 | * |
||
| 665 | * @return \SAML2\XML\saml\SubjectConfirmation[] |
||
| 666 | */ |
||
| 667 | public function getSubjectConfirmation() |
||
| 671 | |||
| 672 | /** |
||
| 673 | * Set the SubjectConfirmation elements that should be included in the assertion. |
||
| 674 | * |
||
| 675 | * @param array \SAML2\XML\saml\SubjectConfirmation[] |
||
| 676 | */ |
||
| 677 | public function setSubjectConfirmation(array $subjectConfirmation) |
||
| 681 | |||
| 682 | /** |
||
| 683 | * Convert this authentication request to an XML element. |
||
| 684 | * |
||
| 685 | * @return \DOMElement This authentication request. |
||
| 686 | */ |
||
| 687 | public function toUnsignedXML() |
||
| 780 | |||
| 781 | /** |
||
| 782 | * Add a Subject-node to the assertion. |
||
| 783 | * |
||
| 784 | * @param \DOMElement $root The assertion element we should add the subject to. |
||
| 785 | */ |
||
| 786 | View Code Duplication | private function addSubject(\DOMElement $root) |
|
| 808 | } |
||
| 809 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..