| Conditions | 19 |
| Paths | 34 |
| Total Lines | 72 |
| Code Lines | 43 |
| 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 |
||
| 27 | public static function nodeToArray( |
||
| 28 | DOMElement $node, |
||
| 29 | $forceArrayKeys = ['hostAttr', 'hostObj', 'street', 'hostAddr'], |
||
| 30 | $ignoreAttributeKeys = ['hostAddr'] |
||
| 31 | ) |
||
| 32 | { |
||
| 33 | $tmp = []; |
||
| 34 | foreach ($node->childNodes as $each) { |
||
| 35 | if ($each->nodeType !== XML_ELEMENT_NODE) { |
||
| 36 | continue; |
||
| 37 | } |
||
| 38 | |||
| 39 | // if node only has a type attribute lets distinguish them directly |
||
| 40 | // and then ignore the attribtue |
||
| 41 | if ($each->hasAttribute('type')) { |
||
| 42 | $key = $each->localName . '@' . $each->getAttribute('type'); |
||
| 43 | $ignore_attributes = true; |
||
| 44 | } else { |
||
| 45 | $key = $each->localName; |
||
| 46 | $ignore_attributes = false; |
||
| 47 | } |
||
| 48 | |||
| 49 | // in case of special keys, always create array |
||
| 50 | if (in_array($key, $forceArrayKeys)) { |
||
| 51 | $current = &$tmp[$key][]; |
||
| 52 | end($tmp[$key]); |
||
| 53 | $insert_key = key($tmp[$key]); |
||
| 54 | } |
||
| 55 | // if key already exists, dynamically create an array |
||
| 56 | elseif (isset($tmp[$key])) { |
||
| 57 | if (!is_array($tmp[$key]) || !isset($tmp[$key][0])) { |
||
| 58 | $tmp[$key] = [$tmp[$key]]; |
||
| 59 | } |
||
| 60 | $current = &$tmp[$key][]; |
||
| 61 | end($tmp[$key]); |
||
| 62 | $insert_key = key($tmp[$key]); |
||
| 63 | } |
||
| 64 | // key was not yet set, so lets start off with a string |
||
| 65 | else { |
||
| 66 | $current = &$tmp[$key]; |
||
| 67 | $insert_key = null; |
||
| 68 | } |
||
| 69 | |||
| 70 | if ($each->hasChildNodes()) { |
||
| 71 | $current = static::nodeToArray($each, $forceArrayKeys, $ignoreAttributeKeys); |
||
| 72 | } else { |
||
| 73 | $current = $each->nodeValue; |
||
| 74 | |||
| 75 | if (!$ignore_attributes && !in_array($each->localName, $ignoreAttributeKeys) && $each->hasAttributes()) { |
||
| 76 | foreach ($each->attributes as $attr) { |
||
| 77 | |||
| 78 | // single attribute with empty node, use the attr-value directly |
||
| 79 | if ($each->localName === 'status' || ($each->attributes->length === 1 && $each->nodeValue === '')) { |
||
| 80 | $current = $attr->nodeValue; |
||
| 81 | break; |
||
| 82 | } |
||
| 83 | |||
| 84 | if ($insert_key) { |
||
| 85 | if (isset($tmp['@' . $key][$attr->nodeName]) && !is_array($tmp['@' . $key][$attr->nodeName])) { |
||
| 86 | $tmp['@' . $key][$attr->nodeName] = [$tmp['@' . $key][$attr->nodeName]]; |
||
| 87 | } |
||
| 88 | $tmp['@' . $key][$attr->nodeName][$insert_key] = $attr->nodeValue; |
||
| 89 | } else { |
||
| 90 | $tmp['@' . $key][$attr->nodeName] = $attr->nodeValue; |
||
| 91 | } |
||
| 92 | } |
||
| 93 | } |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | return $tmp; |
||
| 98 | } |
||
| 99 | } |
||
| 100 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: