| Conditions | 8 |
| Paths | 18 |
| Total Lines | 54 |
| Code Lines | 29 |
| 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 |
||
| 135 | public static function fromXML(DOMElement $xml): object |
||
| 136 | { |
||
| 137 | Assert::same($xml->localName, 'Signature'); |
||
| 138 | Assert::same($xml->namespaceURI, Signature::NS); |
||
| 139 | |||
| 140 | $parent = $xml->parentNode; |
||
| 141 | |||
| 142 | $sigMethod = Utils::xpQuery($xml, './ds:SignedInfo/ds:SignatureMethod'); |
||
| 143 | Assert::notEmpty($sigMethod, 'Missing ds:SignatureMethod element.'); |
||
| 144 | /** @var \DOMElement $sigMethod */ |
||
| 145 | $sigMethod = $sigMethod[0]; |
||
| 146 | Assert::true( |
||
| 147 | $sigMethod->hasAttribute('Algorithm'), |
||
| 148 | 'Missing "Algorithm" attribute on ds:SignatureMethod element.' |
||
| 149 | ); |
||
| 150 | |||
| 151 | // now we extract all available X509 certificates in the signature element |
||
| 152 | $certificates = []; |
||
| 153 | foreach (Utils::xpQuery($xml, './ds:KeyInfo/ds:X509Data/ds:X509Certificate') as $certNode) { |
||
| 154 | $certificates[] = Certificate::convertToCertificate( |
||
| 155 | str_replace(["\r", "\n", "\t", ' '], '', trim($certNode->textContent)) |
||
| 156 | ); |
||
| 157 | } |
||
| 158 | |||
| 159 | $signature = new self($sigMethod->getAttribute('Algorithm'), $certificates); |
||
| 160 | |||
| 161 | $signature->signer->sigNode = $xml; |
||
| 162 | |||
| 163 | // canonicalize the XMLDSig SignedInfo element in the message |
||
| 164 | $signature->signer->canonicalizeSignedInfo(); |
||
| 165 | |||
| 166 | // validate referenced xml nodes |
||
| 167 | if (!$signature->signer->validateReference()) { |
||
| 168 | throw new Exception('Digest validation failed.'); |
||
| 169 | } |
||
| 170 | |||
| 171 | // check that $root is one of the signed nodes |
||
| 172 | $rootSigned = false; |
||
| 173 | /** @var \DOMNode $signedNode */ |
||
| 174 | foreach ($signature->signer->getValidatedNodes() as $signedNode) { |
||
| 175 | if ($signedNode->isSameNode($parent)) { |
||
| 176 | $rootSigned = true; |
||
| 177 | break; |
||
| 178 | } elseif ($parent->parentNode instanceof \DOMDocument && $signedNode->isSameNode($parent->ownerDocument)) { |
||
| 179 | // $parent is the root element of a signed document |
||
| 180 | $rootSigned = true; |
||
| 181 | break; |
||
| 182 | } |
||
| 183 | } |
||
| 184 | if (!$rootSigned) { |
||
| 185 | throw new Exception('The parent element is not signed.'); |
||
| 186 | } |
||
| 187 | |||
| 188 | return $signature; |
||
| 189 | } |
||
| 213 |