| Conditions | 11 |
| Paths | 7 |
| Total Lines | 51 |
| Code Lines | 33 |
| 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 |
||
| 105 | public static function fromXML(DOMElement $xml): static |
||
| 106 | { |
||
| 107 | Assert::same($xml->localName, 'AttributeValue', InvalidDOMElementException::class); |
||
| 108 | Assert::same($xml->namespaceURI, AttributeValue::NS, InvalidDOMElementException::class); |
||
| 109 | |||
| 110 | if ($xml->childElementCount > 0) { |
||
| 111 | $node = $xml->firstElementChild; |
||
| 112 | |||
| 113 | if (str_contains($node->tagName, ':')) { |
||
| 114 | list($prefix, $eltName) = explode(':', $node->tagName); |
||
| 115 | $className = sprintf('\SimpleSAML\SAML2\XML\%s\%s', $prefix, $eltName); |
||
| 116 | |||
| 117 | if (class_exists($className)) { |
||
| 118 | $value = $className::fromXML($node); |
||
| 119 | } else { |
||
| 120 | $value = Chunk::fromXML($node); |
||
|
|
|||
| 121 | } |
||
| 122 | } else { |
||
| 123 | $value = Chunk::fromXML($node); |
||
| 124 | } |
||
| 125 | } elseif ( |
||
| 126 | $xml->hasAttributeNS(C_XSI::NS_XSI, "type") && |
||
| 127 | $xml->getAttributeNS(C_XSI::NS_XSI, "type") === "xs:integer" |
||
| 128 | ) { |
||
| 129 | Assert::numeric($xml->textContent); |
||
| 130 | |||
| 131 | // we have an integer as value |
||
| 132 | $value = intval($xml->textContent); |
||
| 133 | } elseif ( |
||
| 134 | $xml->hasAttributeNS(C_XSI::NS_XSI, "type") && |
||
| 135 | $xml->getAttributeNS(C_XSI::NS_XSI, "type") === "xs:dateTime" |
||
| 136 | ) { |
||
| 137 | Assert::validDateTime($xml->textContent); |
||
| 138 | |||
| 139 | // we have a dateTime as value |
||
| 140 | $value = new DateTimeImmutable($xml->textContent); |
||
| 141 | } elseif ( |
||
| 142 | // null value |
||
| 143 | $xml->hasAttributeNS(C_XSI::NS_XSI, "nil") && |
||
| 144 | ($xml->getAttributeNS(C_XSI::NS_XSI, "nil") === "1" || |
||
| 145 | $xml->getAttributeNS(C_XSI::NS_XSI, "nil") === "true") |
||
| 146 | ) { |
||
| 147 | Assert::isEmpty($xml->nodeValue); |
||
| 148 | Assert::isEmpty($xml->textContent); |
||
| 149 | |||
| 150 | $value = null; |
||
| 151 | } else { |
||
| 152 | $value = $xml->textContent; |
||
| 153 | } |
||
| 154 | |||
| 155 | return new static($value); |
||
| 156 | } |
||
| 203 |