| Conditions | 3 |
| Paths | 4 |
| Total Lines | 73 |
| Code Lines | 49 |
| 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 |
||
| 127 | public static function fromXML(DOMElement $xml): static |
||
| 128 | { |
||
| 129 | Assert::same($xml->localName, 'LogoutRequest', InvalidDOMElementException::class); |
||
| 130 | Assert::same($xml->namespaceURI, LogoutRequest::NS, InvalidDOMElementException::class); |
||
| 131 | |||
| 132 | $version = self::getAttribute($xml, 'Version'); |
||
| 133 | Assert::true(version_compare('2.0', $version, '<='), RequestVersionTooLowException::class); |
||
| 134 | Assert::true(version_compare('2.0', $version, '>='), RequestVersionTooHighException::class); |
||
| 135 | |||
| 136 | $id = self::getAttribute($xml, 'ID'); |
||
| 137 | Assert::validNCName($id); // Covers the empty string |
||
| 138 | |||
| 139 | $issueInstant = self::getAttribute($xml, 'IssueInstant'); |
||
| 140 | // Strip sub-seconds - See paragraph 1.3.3 of SAML core specifications |
||
| 141 | $issueInstant = preg_replace('/([.][0-9]+Z)$/', 'Z', $issueInstant, 1); |
||
| 142 | |||
| 143 | Assert::validDateTime($issueInstant, ProtocolViolationException::class); |
||
| 144 | $issueInstant = new DateTimeImmutable($issueInstant); |
||
| 145 | |||
| 146 | $notOnOrAfter = self::getOptionalAttribute($xml, 'NotOnOrAfter', null); |
||
| 147 | if ($notOnOrAfter !== null) { |
||
| 148 | // Strip sub-seconds - See paragraph 1.3.3 of SAML core specifications |
||
| 149 | $notOnOrAfter = preg_replace('/([.][0-9]+Z)$/', 'Z', $notOnOrAfter, 1); |
||
| 150 | |||
| 151 | Assert::validDateTime($notOnOrAfter, ProtocolViolationException::class); |
||
| 152 | $notOnOrAfter = new DateTimeImmutable($notOnOrAfter); |
||
| 153 | } |
||
| 154 | |||
| 155 | $issuer = Issuer::getChildrenOfClass($xml); |
||
| 156 | Assert::countBetween($issuer, 0, 1); |
||
| 157 | |||
| 158 | $extensions = Extensions::getChildrenOfClass($xml); |
||
| 159 | Assert::maxCount( |
||
| 160 | $extensions, |
||
| 161 | 1, |
||
| 162 | 'Only one saml:Extensions element is allowed.', |
||
| 163 | TooManyElementsException::class, |
||
| 164 | ); |
||
| 165 | |||
| 166 | $identifier = self::getIdentifierFromXML($xml); |
||
| 167 | Assert::notNull( |
||
| 168 | $identifier, |
||
| 169 | 'Missing <saml:NameID>, <saml:BaseID> or <saml:EncryptedID> in <samlp:LogoutRequest>.', |
||
| 170 | MissingElementException::class, |
||
| 171 | ); |
||
| 172 | Assert::isInstanceOfAny($identifier, [AbstractBaseID::class, NameID::class, EncryptedID::class]); |
||
| 173 | |||
| 174 | $signature = Signature::getChildrenOfClass($xml); |
||
| 175 | Assert::maxCount($signature, 1, 'Only one ds:Signature element is allowed.'); |
||
| 176 | |||
| 177 | $sessionIndex = SessionIndex::getChildrenOfClass($xml); |
||
| 178 | |||
| 179 | $request = new static( |
||
| 180 | $identifier, |
||
| 181 | $issueInstant, |
||
| 182 | $notOnOrAfter, |
||
| 183 | self::getOptionalAttribute($xml, 'Reason', null), |
||
| 184 | $sessionIndex, |
||
| 185 | array_pop($issuer), |
||
| 186 | $id, |
||
| 187 | $version, |
||
| 188 | self::getOptionalAttribute($xml, 'Destination', null), |
||
| 189 | self::getOptionalAttribute($xml, 'Consent', null), |
||
| 190 | array_pop($extensions), |
||
| 191 | ); |
||
| 192 | |||
| 193 | if (!empty($signature)) { |
||
| 194 | $request->setSignature($signature[0]); |
||
| 195 | $request->messageContainedSignatureUponConstruction = true; |
||
| 196 | $request->setXML($xml); |
||
| 197 | } |
||
| 198 | |||
| 199 | return $request; |
||
| 200 | } |
||
| 232 |