Conditions | 6 |
Paths | 6 |
Total Lines | 78 |
Code Lines | 50 |
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 |
||
97 | public static function fromXML(DOMElement $xml): static |
||
98 | { |
||
99 | Assert::same($xml->localName, 'ArtifactResponse', InvalidDOMElementException::class); |
||
100 | Assert::same($xml->namespaceURI, ArtifactResponse::NS, InvalidDOMElementException::class); |
||
101 | |||
102 | $version = self::getAttribute($xml, 'Version'); |
||
103 | Assert::true(version_compare('2.0', $version, '<='), RequestVersionTooLowException::class); |
||
104 | Assert::true(version_compare('2.0', $version, '>='), RequestVersionTooHighException::class); |
||
105 | |||
106 | $id = self::getAttribute($xml, 'ID'); |
||
107 | Assert::validNCName($id); // Covers the empty string |
||
108 | |||
109 | $inResponseTo = self::getOptionalAttribute($xml, 'InResponseTo', null); |
||
110 | $destination = self::getOptionalAttribute($xml, 'Destination', null); |
||
111 | $consent = self::getOptionalAttribute($xml, 'Consent', null); |
||
112 | |||
113 | $issueInstant = self::getAttribute($xml, 'IssueInstant'); |
||
114 | // Strip sub-seconds - See paragraph 1.3.3 of SAML core specifications |
||
115 | $issueInstant = preg_replace('/([.][0-9]+Z)$/', 'Z', $issueInstant, 1); |
||
116 | |||
117 | Assert::validDateTime($issueInstant, ProtocolViolationException::class); |
||
118 | $issueInstant = new DateTimeImmutable($issueInstant); |
||
119 | |||
120 | $issuer = Issuer::getChildrenOfClass($xml); |
||
121 | Assert::countBetween($issuer, 0, 1); |
||
122 | |||
123 | // find message; it should come last, after the Status-element |
||
124 | $status = XPath::xpQuery($xml, './saml_protocol:Status', XPath::getXPath($xml)); |
||
125 | $status = $status[0]; |
||
126 | $message = null; |
||
127 | |||
128 | /** @psalm-suppress RedundantCondition */ |
||
129 | for ($child = $status->nextSibling; $child !== null; $child = $child->nextSibling) { |
||
130 | if ($child instanceof DOMElement) { |
||
131 | $message = MessageFactory::fromXML($child); |
||
132 | break; |
||
133 | } |
||
134 | /* Ignore comments and text nodes. */ |
||
135 | } |
||
136 | |||
137 | $status = Status::getChildrenOfClass($xml); |
||
138 | Assert::count($status, 1); |
||
139 | |||
140 | $extensions = Extensions::getChildrenOfClass($xml); |
||
141 | Assert::maxCount( |
||
142 | $extensions, |
||
143 | 1, |
||
144 | 'Only one saml:Extensions element is allowed.', |
||
145 | TooManyElementsException::class, |
||
146 | ); |
||
147 | |||
148 | $signature = Signature::getChildrenOfClass($xml); |
||
149 | Assert::maxCount( |
||
150 | $signature, |
||
151 | 1, |
||
152 | 'Only one ds:Signature element is allowed.', |
||
153 | TooManyElementsException::class, |
||
154 | ); |
||
155 | |||
156 | $response = new static( |
||
157 | array_pop($status), |
||
158 | $issueInstant, |
||
159 | empty($issuer) ? null : array_pop($issuer), |
||
160 | $id, |
||
161 | $version, |
||
162 | $inResponseTo, |
||
163 | $destination, |
||
164 | $consent, |
||
165 | empty($extensions) ? null : array_pop($extensions), |
||
166 | $message, |
||
167 | ); |
||
168 | |||
169 | if (!empty($signature)) { |
||
170 | $response->setSignature($signature[0]); |
||
171 | $response->setXML($xml); |
||
172 | } |
||
173 | |||
174 | return $response; |
||
175 | } |
||
193 |