| Conditions | 9 |
| Paths | 12 |
| Total Lines | 63 |
| Code Lines | 44 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 112 | public static function fromXML(DOMElement $xml): object |
||
| 113 | { |
||
| 114 | Assert::same($xml->localName, 'Response', InvalidDOMElementException::class); |
||
| 115 | Assert::same($xml->namespaceURI, Response::NS, InvalidDOMElementException::class); |
||
| 116 | Assert::same('2.0', self::getAttribute($xml, 'Version')); |
||
| 117 | |||
| 118 | $signature = Signature::getChildrenOfClass($xml); |
||
| 119 | Assert::maxCount($signature, 1, 'Only one ds:Signature element is allowed.', TooManyElementsException::class); |
||
| 120 | |||
| 121 | $id = self::getAttribute($xml, 'ID'); |
||
| 122 | $inResponseTo = self::getAttribute($xml, 'InResponseTo', null); |
||
| 123 | $destination = self::getAttribute($xml, 'Destination', null); |
||
| 124 | $consent = self::getAttribute($xml, 'Consent', null); |
||
| 125 | |||
| 126 | /** @psalm-suppress PossiblyNullArgument */ |
||
| 127 | $issueInstant = self::getAttribute($xml, 'IssueInstant'); |
||
| 128 | Assert::validDateTimeZulu($issueInstant, ProtocolViolationException::class); |
||
| 129 | $issueInstant = XMLUtils::xsDateTimeToTimestamp($issueInstant); |
||
| 130 | |||
| 131 | $issuer = Issuer::getChildrenOfClass($xml); |
||
| 132 | Assert::countBetween($issuer, 0, 1); |
||
| 133 | |||
| 134 | $status = Status::getChildrenOfClass($xml); |
||
| 135 | Assert::minCount($status, 1, MissingElementException::class); |
||
| 136 | Assert::maxCount($status, 1, TooManyElementsException::class); |
||
| 137 | |||
| 138 | $extensions = Extensions::getChildrenOfClass($xml); |
||
| 139 | Assert::maxCount($extensions, 1, 'Only one saml:Extensions element is allowed.', TooManyElementsException::class); |
||
| 140 | |||
| 141 | $assertions = []; |
||
| 142 | foreach ($xml->childNodes as $node) { |
||
| 143 | if ($node->namespaceURI !== Constants::NS_SAML) { |
||
| 144 | continue; |
||
| 145 | } elseif (!($node instanceof DOMElement)) { |
||
| 146 | continue; |
||
| 147 | } |
||
| 148 | |||
| 149 | if ($node->localName === 'Assertion') { |
||
| 150 | $assertions[] = Assertion::fromXML($node); |
||
| 151 | } elseif ($node->localName === 'EncryptedAssertion') { |
||
| 152 | $assertions[] = EncryptedAssertion::fromXML($node); |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | $response = new self( |
||
| 157 | array_pop($status), |
||
| 158 | empty($issuer) ? null : array_pop($issuer), |
||
| 159 | $id, |
||
| 160 | $issueInstant, |
||
| 161 | $inResponseTo, |
||
| 162 | $destination, |
||
| 163 | $consent, |
||
| 164 | empty($extensions) ? null : array_pop($extensions), |
||
| 165 | $assertions |
||
| 166 | ); |
||
| 167 | |||
| 168 | if (!empty($signature)) { |
||
| 169 | $response->setSignature($signature[0]); |
||
| 170 | $response->messageContainedSignatureUponConstruction = true; |
||
| 171 | } |
||
| 172 | |||
| 173 | $response->setXML($xml); |
||
| 174 | return $response; |
||
| 175 | } |
||
| 194 |