| Conditions | 4 |
| Paths | 2 |
| Total Lines | 61 |
| Code Lines | 42 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 100 | public static function fromXML(DOMElement $xml): static |
||
| 101 | { |
||
| 102 | Assert::same($xml->localName, 'Response', InvalidDOMElementException::class); |
||
| 103 | Assert::same($xml->namespaceURI, Response::NS, InvalidDOMElementException::class); |
||
| 104 | |||
| 105 | $version = self::getAttribute($xml, 'Version'); |
||
| 106 | Assert::true(version_compare('2.0', $version, '<='), RequestVersionTooLowException::class); |
||
| 107 | Assert::true(version_compare('2.0', $version, '>='), RequestVersionTooHighException::class); |
||
| 108 | |||
| 109 | $signature = Signature::getChildrenOfClass($xml); |
||
| 110 | Assert::maxCount($signature, 1, 'Only one ds:Signature element is allowed.', TooManyElementsException::class); |
||
| 111 | |||
| 112 | $id = self::getAttribute($xml, 'ID'); |
||
| 113 | Assert::validNCName($id); // Covers the empty string |
||
| 114 | |||
| 115 | $inResponseTo = self::getOptionalAttribute($xml, 'InResponseTo', null); |
||
| 116 | $destination = self::getOptionalAttribute($xml, 'Destination', null); |
||
| 117 | $consent = self::getOptionalAttribute($xml, 'Consent', null); |
||
| 118 | |||
| 119 | $issueInstant = self::getAttribute($xml, 'IssueInstant'); |
||
| 120 | // Strip sub-seconds - See paragraph 1.3.3 of SAML core specifications |
||
| 121 | $issueInstant = preg_replace('/([.][0-9]+Z)$/', 'Z', $issueInstant, 1); |
||
| 122 | |||
| 123 | SAMLAssert::validDateTime($issueInstant, ProtocolViolationException::class); |
||
| 124 | $issueInstant = new DateTimeImmutable($issueInstant); |
||
| 125 | |||
| 126 | $issuer = Issuer::getChildrenOfClass($xml); |
||
| 127 | Assert::countBetween($issuer, 0, 1); |
||
| 128 | |||
| 129 | $status = Status::getChildrenOfClass($xml); |
||
| 130 | Assert::minCount($status, 1, MissingElementException::class); |
||
| 131 | Assert::maxCount($status, 1, TooManyElementsException::class); |
||
| 132 | |||
| 133 | $extensions = Extensions::getChildrenOfClass($xml); |
||
| 134 | Assert::maxCount( |
||
| 135 | $extensions, |
||
| 136 | 1, |
||
| 137 | 'Only one saml:Extensions element is allowed.', |
||
| 138 | TooManyElementsException::class, |
||
| 139 | ); |
||
| 140 | |||
| 141 | $response = new static( |
||
| 142 | array_pop($status), |
||
| 143 | $issueInstant, |
||
| 144 | empty($issuer) ? null : array_pop($issuer), |
||
| 145 | $id, |
||
| 146 | $version, |
||
| 147 | $inResponseTo, |
||
| 148 | $destination, |
||
| 149 | $consent, |
||
| 150 | empty($extensions) ? null : array_pop($extensions), |
||
| 151 | array_merge(Assertion::getChildrenOfClass($xml), EncryptedAssertion::getChildrenOfClass($xml)), |
||
| 152 | ); |
||
| 153 | |||
| 154 | if (!empty($signature)) { |
||
| 155 | $response->setSignature($signature[0]); |
||
| 156 | $response->messageContainedSignatureUponConstruction = true; |
||
| 157 | $response->setXML($xml); |
||
| 158 | } |
||
| 159 | |||
| 160 | return $response; |
||
| 161 | } |
||
| 180 |