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) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @param $xml |
||
| 175 | * |
||
| 176 | * @throws \Exception |
||
| 177 | */ |
||
| 178 | private function parseSubject(\DOMElement $xml) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param \DOMElement $xml |
||
| 215 | * |
||
| 216 | * @throws \Exception |
||
| 217 | */ |
||
| 218 | protected function parseNameIdPolicy(\DOMElement $xml) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @param \DOMElement $xml |
||
| 239 | */ |
||
| 240 | protected function parseRequestedAuthnContext(\DOMElement $xml) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @param \DOMElement $xml |
||
| 268 | * |
||
| 269 | * @throws \Exception |
||
| 270 | */ |
||
| 271 | protected function parseScoping(\DOMElement $xml) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Retrieve the NameIdPolicy. |
||
| 300 | * |
||
| 301 | * @see \SAML2\AuthnRequest::setNameIdPolicy() |
||
| 302 | * @return array The NameIdPolicy. |
||
| 303 | */ |
||
| 304 | public function getNameIdPolicy() |
||
| 308 | |||
| 309 | |||
| 310 | /** |
||
| 311 | * Set the NameIDPolicy. |
||
| 312 | * |
||
| 313 | * This function accepts an array with the following options: |
||
| 314 | * - 'Format' (string) |
||
| 315 | * - 'SPNameQualifier' (string) |
||
| 316 | * - 'AllowCreate' (bool) |
||
| 317 | * |
||
| 318 | * @param array $nameIdPolicy The NameIDPolicy. |
||
| 319 | */ |
||
| 320 | public function setNameIdPolicy(array $nameIdPolicy) |
||
| 334 | |||
| 335 | |||
| 336 | /** |
||
| 337 | * Retrieve the value of the ForceAuthn attribute. |
||
| 338 | * |
||
| 339 | * @return bool The ForceAuthn attribute. |
||
| 340 | */ |
||
| 341 | public function getForceAuthn() |
||
| 345 | |||
| 346 | |||
| 347 | /** |
||
| 348 | * Set the value of the ForceAuthn attribute. |
||
| 349 | * |
||
| 350 | * @param bool $forceAuthn The ForceAuthn attribute. |
||
| 351 | */ |
||
| 352 | public function setForceAuthn($forceAuthn) |
||
| 358 | |||
| 359 | |||
| 360 | /** |
||
| 361 | * Retrieve the value of the ProviderName attribute. |
||
| 362 | * |
||
| 363 | * @return string The ProviderName attribute. |
||
| 364 | */ |
||
| 365 | public function getProviderName() |
||
| 369 | |||
| 370 | |||
| 371 | /** |
||
| 372 | * Set the value of the ProviderName attribute. |
||
| 373 | * |
||
| 374 | * @param string $ProviderName The ProviderName attribute. |
||
| 375 | */ |
||
| 376 | public function setProviderName($ProviderName) |
||
| 382 | |||
| 383 | |||
| 384 | /** |
||
| 385 | * Retrieve the value of the IsPassive attribute. |
||
| 386 | * |
||
| 387 | * @return bool The IsPassive attribute. |
||
| 388 | */ |
||
| 389 | public function getIsPassive() |
||
| 393 | |||
| 394 | |||
| 395 | /** |
||
| 396 | * Set the value of the IsPassive attribute. |
||
| 397 | * |
||
| 398 | * @param bool $isPassive The IsPassive attribute. |
||
| 399 | */ |
||
| 400 | public function setIsPassive($isPassive) |
||
| 406 | |||
| 407 | |||
| 408 | /** |
||
| 409 | * This function sets the scoping for the request. |
||
| 410 | * See Core 3.4.1.2 for the definition of scoping. |
||
| 411 | * Currently we support an IDPList of idpEntries. |
||
| 412 | * |
||
| 413 | * Each idpEntries consists of an array, containing |
||
| 414 | * keys (mapped to attributes) and corresponding values. |
||
| 415 | * Allowed attributes: Loc, Name, ProviderID. |
||
| 416 | * |
||
| 417 | * For backward compatibility, an idpEntries can also |
||
| 418 | * be a string instead of an array, where each string |
||
| 419 | * is mapped to the value of attribute ProviderID. |
||
| 420 | */ |
||
| 421 | public function setIDPList($IDPList) |
||
| 426 | |||
| 427 | |||
| 428 | /** |
||
| 429 | * This function retrieves the list of providerIDs from this authentication request. |
||
| 430 | * Currently we only support a list of ipd ientity id's. |
||
| 431 | * @return array List of idp EntityIDs from the request |
||
| 432 | */ |
||
| 433 | public function getIDPList() |
||
| 437 | |||
| 438 | /** |
||
| 439 | * @param int $ProxyCount |
||
| 440 | */ |
||
| 441 | public function setProxyCount($ProxyCount) |
||
| 446 | |||
| 447 | /** |
||
| 448 | * @return int |
||
| 449 | */ |
||
| 450 | public function getProxyCount() |
||
| 454 | |||
| 455 | /** |
||
| 456 | * @param array $RequesterID |
||
| 457 | */ |
||
| 458 | public function setRequesterID(array $RequesterID) |
||
| 462 | |||
| 463 | /** |
||
| 464 | * @return array |
||
| 465 | */ |
||
| 466 | public function getRequesterID() |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Retrieve the value of the AssertionConsumerServiceURL attribute. |
||
| 473 | * |
||
| 474 | * @return string|null The AssertionConsumerServiceURL attribute. |
||
| 475 | */ |
||
| 476 | public function getAssertionConsumerServiceURL() |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Set the value of the AssertionConsumerServiceURL attribute. |
||
| 483 | * |
||
| 484 | * @param string|null $assertionConsumerServiceURL The AssertionConsumerServiceURL attribute. |
||
| 485 | */ |
||
| 486 | public function setAssertionConsumerServiceURL($assertionConsumerServiceURL) |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Retrieve the value of the ProtocolBinding attribute. |
||
| 495 | * |
||
| 496 | * @return string|null The ProtocolBinding attribute. |
||
| 497 | */ |
||
| 498 | public function getProtocolBinding() |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Set the value of the ProtocolBinding attribute. |
||
| 505 | * |
||
| 506 | * @param string $protocolBinding The ProtocolBinding attribute. |
||
| 507 | */ |
||
| 508 | public function setProtocolBinding($protocolBinding) |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Retrieve the value of the AttributeConsumingServiceIndex attribute. |
||
| 517 | * |
||
| 518 | * @return int|null The AttributeConsumingServiceIndex attribute. |
||
| 519 | */ |
||
| 520 | public function getAttributeConsumingServiceIndex() |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Set the value of the AttributeConsumingServiceIndex attribute. |
||
| 527 | * |
||
| 528 | * @param int|null $attributeConsumingServiceIndex The AttributeConsumingServiceIndex attribute. |
||
| 529 | */ |
||
| 530 | public function setAttributeConsumingServiceIndex($attributeConsumingServiceIndex) |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Retrieve the value of the AssertionConsumerServiceIndex attribute. |
||
| 539 | * |
||
| 540 | * @return int|null The AssertionConsumerServiceIndex attribute. |
||
| 541 | */ |
||
| 542 | public function getAssertionConsumerServiceIndex() |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Set the value of the AssertionConsumerServiceIndex attribute. |
||
| 549 | * |
||
| 550 | * @param int|null $assertionConsumerServiceIndex The AssertionConsumerServiceIndex attribute. |
||
| 551 | */ |
||
| 552 | public function setAssertionConsumerServiceIndex($assertionConsumerServiceIndex) |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Retrieve the RequestedAuthnContext. |
||
| 561 | * |
||
| 562 | * @return array|null The RequestedAuthnContext. |
||
| 563 | */ |
||
| 564 | public function getRequestedAuthnContext() |
||
| 568 | |||
| 569 | /** |
||
| 570 | * Set the RequestedAuthnContext. |
||
| 571 | * |
||
| 572 | * @param array|null $requestedAuthnContext The RequestedAuthnContext. |
||
| 573 | */ |
||
| 574 | public function setRequestedAuthnContext($requestedAuthnContext) |
||
| 580 | |||
| 581 | /** |
||
| 582 | * Retrieve the NameId of the subject in the assertion. |
||
| 583 | * |
||
| 584 | * The returned NameId is in the format used by \SAML2\Utils::addNameId(). |
||
| 585 | * |
||
| 586 | * @see \SAML2\Utils::addNameId() |
||
| 587 | * @return array|null The name identifier of the assertion. |
||
| 588 | * @throws \Exception |
||
| 589 | */ |
||
| 590 | public function getNameId() |
||
| 598 | |||
| 599 | /** |
||
| 600 | * Set the NameId of the subject in the assertion. |
||
| 601 | * |
||
| 602 | * The NameId must be in the format accepted by \SAML2\Utils::addNameId(). |
||
| 603 | * |
||
| 604 | * @see \SAML2\Utils::addNameId() |
||
| 605 | * |
||
| 606 | * @param array|null $nameId The name identifier of the assertion. |
||
| 607 | */ |
||
| 608 | public function setNameId($nameId) |
||
| 614 | |||
| 615 | /** |
||
| 616 | * Encrypt the NameID in the AuthnRequest. |
||
| 617 | * |
||
| 618 | * @param XMLSecurityKey $key The encryption key. |
||
| 619 | */ |
||
| 620 | View Code Duplication | public function encryptNameId(XMLSecurityKey $key) |
|
| 645 | |||
| 646 | /** |
||
| 647 | * Decrypt the NameId of the subject in the assertion. |
||
| 648 | * |
||
| 649 | * @param XMLSecurityKey $key The decryption key. |
||
| 650 | * @param array $blacklist Blacklisted decryption algorithms. |
||
| 651 | */ |
||
| 652 | View Code Duplication | public function decryptNameId(XMLSecurityKey $key, array $blacklist = array()) |
|
| 665 | |||
| 666 | /** |
||
| 667 | * Retrieve the SubjectConfirmation elements we have in our Subject element. |
||
| 668 | * |
||
| 669 | * @return \SAML2\XML\saml\SubjectConfirmation[] |
||
| 670 | */ |
||
| 671 | public function getSubjectConfirmation() |
||
| 675 | |||
| 676 | /** |
||
| 677 | * Set the SubjectConfirmation elements that should be included in the assertion. |
||
| 678 | * |
||
| 679 | * @param array \SAML2\XML\saml\SubjectConfirmation[] |
||
| 680 | */ |
||
| 681 | public function setSubjectConfirmation(array $subjectConfirmation) |
||
| 685 | |||
| 686 | /** |
||
| 687 | * Convert this authentication request to an XML element. |
||
| 688 | * |
||
| 689 | * @return \DOMElement This authentication request. |
||
| 690 | */ |
||
| 691 | public function toUnsignedXML() |
||
| 784 | |||
| 785 | /** |
||
| 786 | * Add a Subject-node to the assertion. |
||
| 787 | * |
||
| 788 | * @param \DOMElement $root The assertion element we should add the subject to. |
||
| 789 | */ |
||
| 790 | View Code Duplication | private function addSubject(\DOMElement $root) |
|
| 812 | } |
||
| 813 |
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..