Conditions | 31 |
Paths | 100 |
Total Lines | 107 |
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 |
||
30 | function xml2array(string $xml, int $get_attributes = 1, string $priority = 'tag') : array |
||
31 | { |
||
32 | if (!function_exists('xml_parser_create')) { |
||
33 | return array(); |
||
34 | } |
||
35 | if (!$xml) { |
||
36 | return array(); |
||
37 | } |
||
38 | $contents = $xml; |
||
39 | $parser = xml_parser_create(''); |
||
40 | xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8"); |
||
41 | xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); |
||
42 | xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1); |
||
43 | xml_parse_into_struct($parser, trim($contents), $xml_values); |
||
44 | xml_parser_free($parser); |
||
45 | if (empty($xml_values)) { |
||
46 | return array(); |
||
47 | } |
||
48 | $xml_array = array(); |
||
49 | $current = &$xml_array; |
||
50 | $repeated_tag_index = array(); |
||
51 | foreach ($xml_values as $data) { |
||
52 | unset ($attributes, $value); |
||
53 | extract($data); |
||
54 | $result = array(); |
||
55 | $attributes_data = array(); |
||
56 | if (isset ($value)) { |
||
57 | if ($priority == 'tag') { |
||
58 | $result = $value; |
||
59 | } else { |
||
60 | $result['value'] = $value; |
||
61 | } |
||
62 | } |
||
63 | if (isset ($attributes) && $get_attributes) { |
||
64 | foreach ($attributes as $attr => $val) { |
||
65 | if ($priority == 'tag') { |
||
66 | $attributes_data[$attr] = $val; |
||
67 | } else { |
||
68 | $result['attr'][$attr] = $val; |
||
69 | } //Set all the attributes in a array called 'attr' |
||
70 | } |
||
71 | } |
||
72 | if ($type == "open") { |
||
73 | $parent[$level - 1] = &$current; |
||
74 | if (!is_array($current) || (!in_array($tag, array_keys($current)))) { |
||
75 | $current[$tag] = $result; |
||
76 | if (!empty($attributes_data)) { |
||
77 | $current[$tag . '_attr'] = $attributes_data; |
||
78 | } |
||
79 | $repeated_tag_index[$tag . '_' . $level] = 1; |
||
80 | $current = &$current[$tag]; |
||
81 | } else { |
||
82 | if (isset ($current[$tag][0])) { |
||
83 | $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result; |
||
84 | $repeated_tag_index[$tag . '_' . $level]++; |
||
85 | } else { |
||
86 | $current[$tag] = array( |
||
87 | $current[$tag], |
||
88 | $result |
||
89 | ); |
||
90 | $repeated_tag_index[$tag . '_' . $level] = 2; |
||
91 | if (isset ($current[$tag . '_attr'])) { |
||
92 | $current[$tag]['0_attr'] = $current[$tag . '_attr']; |
||
93 | unset ($current[$tag . '_attr']); |
||
94 | } |
||
95 | } |
||
96 | $last_item_index = $repeated_tag_index[$tag . '_' . $level] - 1; |
||
97 | $current = &$current[$tag][$last_item_index]; |
||
98 | } |
||
99 | } elseif ($type == "complete") { |
||
100 | if (!isset ($current[$tag])) { |
||
101 | $current[$tag] = $result; |
||
102 | $repeated_tag_index[$tag . '_' . $level] = 1; |
||
103 | if ($priority == 'tag' && !empty($attributes_data)) { |
||
104 | $current[$tag . '_attr'] = $attributes_data; |
||
105 | } |
||
106 | } else { |
||
107 | if (is_array($current[$tag]) && isset ($current[$tag][0])) { |
||
108 | $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result; |
||
109 | if ($priority == 'tag' && $get_attributes && !empty($attributes_data)) { |
||
110 | $current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data; |
||
111 | } |
||
112 | $repeated_tag_index[$tag . '_' . $level]++; |
||
113 | } else { |
||
114 | $current[$tag] = array( |
||
115 | $current[$tag], |
||
116 | $result |
||
117 | ); |
||
118 | $repeated_tag_index[$tag . '_' . $level] = 1; |
||
119 | if ($priority == 'tag' && $get_attributes) { |
||
120 | if (isset ($current[$tag . '_attr'])) { |
||
121 | $current[$tag]['0_attr'] = $current[$tag . '_attr']; |
||
122 | unset ($current[$tag . '_attr']); |
||
123 | } |
||
124 | if (!empty($attributes_data)) { |
||
125 | $current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data; |
||
126 | } |
||
127 | } |
||
128 | $repeated_tag_index[$tag . '_' . $level]++; //0 && 1 index is already taken |
||
129 | } |
||
130 | } |
||
131 | } elseif ($type == 'close') { |
||
132 | $current = &$parent[$level - 1]; |
||
133 | } |
||
134 | } |
||
135 | return $xml_array; |
||
136 | } |
||
137 | |||
170 |