| Conditions | 8 |
| Paths | 18 |
| Total Lines | 57 |
| Code Lines | 31 |
| 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 |
||
| 168 | public static function fromXML(DOMElement $xml): object |
||
| 169 | { |
||
| 170 | Assert::same($xml->localName, 'Signature', InvalidDOMElementException::class); |
||
| 171 | Assert::same($xml->namespaceURI, Signature::NS, InvalidDOMElementException::class); |
||
| 172 | |||
| 173 | $parent = $xml->parentNode; |
||
| 174 | |||
| 175 | $sigMethod = XMLUtils::xpQuery($xml, './ds:SignedInfo/ds:SignatureMethod'); |
||
| 176 | Assert::notEmpty($sigMethod, 'Missing ds:SignatureMethod element.'); |
||
| 177 | /** @var \DOMElement $sigMethod */ |
||
| 178 | $sigMethod = $sigMethod[0]; |
||
| 179 | Assert::true( |
||
| 180 | $sigMethod->hasAttribute('Algorithm'), |
||
| 181 | 'Missing "Algorithm" attribute on ds:SignatureMethod element.' |
||
| 182 | ); |
||
| 183 | |||
| 184 | // now we extract all available X509 certificates in the signature element |
||
| 185 | $certificates = []; |
||
| 186 | foreach (XMLUtils::xpQuery($xml, './ds:KeyInfo/ds:X509Data/ds:X509Certificate') as $certNode) { |
||
| 187 | $certificates[] = Certificate::convertToCertificate( |
||
| 188 | str_replace(["\r", "\n", "\t", ' '], '', trim($certNode->textContent)) |
||
| 189 | ); |
||
| 190 | } |
||
| 191 | |||
| 192 | $value = SignatureValue::getChildrenOfClass($xml); |
||
| 193 | Assert::count($value, 1, 'ds:Signature needs exactly one ds:SignatureValue'); |
||
| 194 | |||
| 195 | $signature = new self(self::getAttribute($sigMethod, 'Algorithm'), $value[0], $certificates); |
||
| 196 | |||
| 197 | $signature->signer->sigNode = $xml; |
||
| 198 | |||
| 199 | // canonicalize the XMLDSig SignedInfo element in the message |
||
| 200 | $signature->signer->canonicalizeSignedInfo(); |
||
| 201 | |||
| 202 | // validate referenced xml nodes |
||
| 203 | if (!$signature->signer->validateReference()) { |
||
| 204 | throw new Exception('Digest validation failed.'); |
||
| 205 | } |
||
| 206 | |||
| 207 | // check that $root is one of the signed nodes |
||
| 208 | $rootSigned = false; |
||
| 209 | /** @var \DOMNode $signedNode */ |
||
| 210 | foreach ($signature->signer->getValidatedNodes() as $signedNode) { |
||
| 211 | if ($signedNode->isSameNode($parent)) { |
||
| 212 | $rootSigned = true; |
||
| 213 | break; |
||
| 214 | } elseif ($parent->parentNode instanceof \DOMDocument && $signedNode->isSameNode($parent->ownerDocument)) { |
||
| 215 | // $parent is the root element of a signed document |
||
| 216 | $rootSigned = true; |
||
| 217 | break; |
||
| 218 | } |
||
| 219 | } |
||
| 220 | if (!$rootSigned) { |
||
| 221 | throw new Exception('The parent element is not signed.'); |
||
| 222 | } |
||
| 223 | |||
| 224 | return $signature; |
||
| 225 | } |
||
| 253 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.