| Conditions | 4 |
| Paths | 2 |
| Total Lines | 61 |
| Code Lines | 42 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 104 | public static function fromXML(DOMElement $xml): static |
||
| 105 | { |
||
| 106 | Assert::same($xml->localName, 'Response', InvalidDOMElementException::class); |
||
| 107 | Assert::same($xml->namespaceURI, Response::NS, InvalidDOMElementException::class); |
||
| 108 | |||
| 109 | $version = self::getAttribute($xml, 'Version'); |
||
| 110 | Assert::true(version_compare('2.0', $version, '<='), RequestVersionTooLowException::class); |
||
| 111 | Assert::true(version_compare('2.0', $version, '>='), RequestVersionTooHighException::class); |
||
| 112 | |||
| 113 | $signature = Signature::getChildrenOfClass($xml); |
||
| 114 | Assert::maxCount($signature, 1, 'Only one ds:Signature element is allowed.', TooManyElementsException::class); |
||
| 115 | |||
| 116 | $id = self::getAttribute($xml, 'ID'); |
||
| 117 | Assert::validNCName($id); // Covers the empty string |
||
| 118 | |||
| 119 | $inResponseTo = self::getOptionalAttribute($xml, 'InResponseTo', null); |
||
| 120 | $destination = self::getOptionalAttribute($xml, 'Destination', null); |
||
| 121 | $consent = self::getOptionalAttribute($xml, 'Consent', null); |
||
| 122 | |||
| 123 | $issueInstant = self::getAttribute($xml, 'IssueInstant'); |
||
| 124 | // Strip sub-seconds - See paragraph 1.3.3 of SAML core specifications |
||
| 125 | $issueInstant = preg_replace('/([.][0-9]+Z)$/', 'Z', $issueInstant, 1); |
||
| 126 | |||
| 127 | Assert::validDateTime($issueInstant, ProtocolViolationException::class); |
||
| 128 | $issueInstant = new DateTimeImmutable($issueInstant); |
||
| 129 | |||
| 130 | $issuer = Issuer::getChildrenOfClass($xml); |
||
| 131 | Assert::countBetween($issuer, 0, 1); |
||
| 132 | |||
| 133 | $status = Status::getChildrenOfClass($xml); |
||
| 134 | Assert::minCount($status, 1, MissingElementException::class); |
||
| 135 | Assert::maxCount($status, 1, TooManyElementsException::class); |
||
| 136 | |||
| 137 | $extensions = Extensions::getChildrenOfClass($xml); |
||
| 138 | Assert::maxCount( |
||
| 139 | $extensions, |
||
| 140 | 1, |
||
| 141 | 'Only one saml:Extensions element is allowed.', |
||
| 142 | TooManyElementsException::class, |
||
| 143 | ); |
||
| 144 | |||
| 145 | $response = new static( |
||
| 146 | array_pop($status), |
||
| 147 | $issueInstant, |
||
| 148 | empty($issuer) ? null : array_pop($issuer), |
||
| 149 | $id, |
||
| 150 | $version, |
||
| 151 | $inResponseTo, |
||
| 152 | $destination, |
||
| 153 | $consent, |
||
| 154 | empty($extensions) ? null : array_pop($extensions), |
||
| 155 | array_merge(Assertion::getChildrenOfClass($xml), EncryptedAssertion::getChildrenOfClass($xml)), |
||
| 156 | ); |
||
| 157 | |||
| 158 | if (!empty($signature)) { |
||
| 159 | $response->setSignature($signature[0]); |
||
| 160 | $response->messageContainedSignatureUponConstruction = true; |
||
| 161 | $response->setXML($xml); |
||
| 162 | } |
||
| 163 | |||
| 164 | return $response; |
||
| 165 | } |
||
| 184 |