| Conditions | 10 |
| Paths | 12 |
| Total Lines | 24 |
| Code Lines | 15 |
| 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 |
||
| 38 | public function xpath($element) |
||
| 39 | { |
||
| 40 | $dom = $this->parseXml($this->content); |
||
| 41 | $xpath = new \DOMXpath($dom); |
||
| 42 | $namespaces = $this->getNamespaces(); |
||
| 43 | $defaultNamespaceUri = $dom->lookupNamespaceURI(null); |
||
| 44 | $defaultNamespacePrefix = $defaultNamespaceUri ? $dom->lookupPrefix($defaultNamespaceUri) : null; |
||
| 45 | foreach ($namespaces as $prefix => $namespace) { |
||
| 46 | if (empty($prefix) && empty($defaultNamespacePrefix) && !empty($defaultNamespaceUri)) { |
||
| 47 | $prefix = 'rootns'; |
||
| 48 | } |
||
| 49 | $xpath->registerNamespace($prefix, $namespace); |
||
| 50 | } |
||
| 51 | |||
| 52 | // "fix" queries to the default namespace if any namespaces are defined |
||
| 53 | if (!empty($namespaces) && empty($defaultNamespacePrefix) && !empty($defaultNamespaceUri)) { |
||
| 54 | for ($i=0; $i < 2; ++$i) { |
||
| 55 | $element = preg_replace('/\/(\w+)(\[[^]]+\])?\//', '/rootns:$1$2/', $element); |
||
| 56 | } |
||
| 57 | $element = preg_replace('/\/(\w+)(\[[^]]+\])?$/', '/rootns:$1$2', $element); |
||
| 58 | } |
||
| 59 | |||
| 60 | return $xpath->query($element); |
||
| 61 | } |
||
| 62 | |||
| 82 |