| Conditions | 31 |
| Paths | 100 |
| Total Lines | 110 |
| Code Lines | 85 |
| Lines | 8 |
| Ratio | 7.27 % |
| 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 |
||
| 40 | function xml2array(string $xml, int $get_attributes = 1, string $priority = 'tag') : array |
||
| 41 | { |
||
| 42 | if (!function_exists('xml_parser_create')) { |
||
| 43 | return array(); |
||
| 44 | } |
||
| 45 | if (!$xml) { |
||
| 46 | return array(); |
||
| 47 | } |
||
| 48 | $contents = $xml; |
||
| 49 | $parser = xml_parser_create(''); |
||
| 50 | xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8"); |
||
| 51 | xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); |
||
| 52 | xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1); |
||
| 53 | xml_parse_into_struct($parser, trim($contents), $xml_values); |
||
| 54 | xml_parser_free($parser); |
||
| 55 | if (!$xml_values) { |
||
|
|
|||
| 56 | return array(); |
||
| 57 | } //Hmm... |
||
| 58 | $xml_array = array(); |
||
| 59 | $parents = array(); |
||
| 60 | $opened_tags = array(); |
||
| 61 | $arr = array(); |
||
| 62 | $current = &$xml_array; |
||
| 63 | $repeated_tag_index = array(); |
||
| 64 | foreach ($xml_values as $data) { |
||
| 65 | unset ($attributes, $value); |
||
| 66 | extract($data); |
||
| 67 | $result = array(); |
||
| 68 | $attributes_data = array(); |
||
| 69 | if (isset ($value)) { |
||
| 70 | if ($priority == 'tag') { |
||
| 71 | $result = $value; |
||
| 72 | } else { |
||
| 73 | $result['value'] = $value; |
||
| 74 | } |
||
| 75 | } |
||
| 76 | if (isset ($attributes) && $get_attributes) { |
||
| 77 | foreach ($attributes as $attr => $val) { |
||
| 78 | if ($priority == 'tag') { |
||
| 79 | $attributes_data[$attr] = $val; |
||
| 80 | } else { |
||
| 81 | $result['attr'][$attr] = $val; |
||
| 82 | } //Set all the attributes in a array called 'attr' |
||
| 83 | } |
||
| 84 | } |
||
| 85 | if ($type == "open") { |
||
| 86 | $parent[$level - 1] = &$current; |
||
| 87 | if (!is_array($current) or (!in_array($tag, array_keys($current)))) { |
||
| 88 | $current[$tag] = $result; |
||
| 89 | if ($attributes_data) { |
||
| 90 | $current[$tag . '_attr'] = $attributes_data; |
||
| 91 | } |
||
| 92 | $repeated_tag_index[$tag . '_' . $level] = 1; |
||
| 93 | $current = &$current[$tag]; |
||
| 94 | } else { |
||
| 95 | if (isset ($current[$tag][0])) { |
||
| 96 | $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result; |
||
| 97 | $repeated_tag_index[$tag . '_' . $level]++; |
||
| 98 | } else { |
||
| 99 | $current[$tag] = array( |
||
| 100 | $current[$tag], |
||
| 101 | $result |
||
| 102 | ); |
||
| 103 | $repeated_tag_index[$tag . '_' . $level] = 2; |
||
| 104 | View Code Duplication | if (isset ($current[$tag . '_attr'])) { |
|
|
1 ignored issue
–
show
|
|||
| 105 | $current[$tag]['0_attr'] = $current[$tag . '_attr']; |
||
| 106 | unset ($current[$tag . '_attr']); |
||
| 107 | } |
||
| 108 | } |
||
| 109 | $last_item_index = $repeated_tag_index[$tag . '_' . $level] - 1; |
||
| 110 | $current = &$current[$tag][$last_item_index]; |
||
| 111 | } |
||
| 112 | } elseif ($type == "complete") { |
||
| 113 | if (!isset ($current[$tag])) { |
||
| 114 | $current[$tag] = $result; |
||
| 115 | $repeated_tag_index[$tag . '_' . $level] = 1; |
||
| 116 | if ($priority == 'tag' && $attributes_data) { |
||
| 117 | $current[$tag . '_attr'] = $attributes_data; |
||
| 118 | } |
||
| 119 | } else { |
||
| 120 | if (is_array($current[$tag]) && isset ($current[$tag][0])) { |
||
| 121 | $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result; |
||
| 122 | if ($priority == 'tag' && $get_attributes && $attributes_data) { |
||
| 123 | $current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data; |
||
| 124 | } |
||
| 125 | $repeated_tag_index[$tag . '_' . $level]++; |
||
| 126 | } else { |
||
| 127 | $current[$tag] = array( |
||
| 128 | $current[$tag], |
||
| 129 | $result |
||
| 130 | ); |
||
| 131 | $repeated_tag_index[$tag . '_' . $level] = 1; |
||
| 132 | if ($priority == 'tag' && $get_attributes) { |
||
| 133 | View Code Duplication | if (isset ($current[$tag . '_attr'])) { |
|
|
1 ignored issue
–
show
|
|||
| 134 | $current[$tag]['0_attr'] = $current[$tag . '_attr']; |
||
| 135 | unset ($current[$tag . '_attr']); |
||
| 136 | } |
||
| 137 | if ($attributes_data) { |
||
| 138 | $current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data; |
||
| 139 | } |
||
| 140 | } |
||
| 141 | $repeated_tag_index[$tag . '_' . $level]++; //0 && 1 index is already taken |
||
| 142 | } |
||
| 143 | } |
||
| 144 | } elseif ($type == 'close') { |
||
| 145 | $current = &$parent[$level - 1]; |
||
| 146 | } |
||
| 147 | } |
||
| 148 | return $xml_array; |
||
| 149 | } |
||
| 150 | |||
| 183 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.