Conditions | 48 |
Paths | 261 |
Total Lines | 174 |
Code Lines | 97 |
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 |
||
63 | public function Decode(&$decoder) { |
||
64 | WBXMLDecoder::ResetInWhile("decodeMain"); |
||
65 | while (WBXMLDecoder::InWhile("decodeMain")) { |
||
66 | $entity = $decoder->getElement(); |
||
67 | |||
68 | if ($entity[EN_TYPE] == EN_TYPE_STARTTAG) { |
||
69 | if (!($entity[EN_FLAGS] & EN_FLAGS_CONTENT)) { |
||
70 | $map = $this->mapping[$entity[EN_TAG]]; |
||
71 | if (isset($map[self::STREAMER_ARRAY])) { |
||
72 | $this->{$map[self::STREAMER_VAR]} = []; |
||
73 | } |
||
74 | elseif (isset($map[self::STREAMER_PROP]) && $map[self::STREAMER_PROP] == self::STREAMER_TYPE_SEND_EMPTY) { |
||
75 | $this->{$map[self::STREAMER_VAR]} = "1"; |
||
76 | } |
||
77 | elseif (!isset($map[self::STREAMER_TYPE])) { |
||
78 | $this->{$map[self::STREAMER_VAR]} = ""; |
||
79 | } |
||
80 | elseif ($map[self::STREAMER_TYPE] == self::STREAMER_TYPE_DATE || $map[self::STREAMER_TYPE] == self::STREAMER_TYPE_DATE_DASHES) { |
||
81 | $this->{$map[self::STREAMER_VAR]} = ""; |
||
82 | } |
||
83 | |||
84 | continue; |
||
85 | } |
||
86 | // Found a start tag |
||
87 | if (!isset($this->mapping[$entity[EN_TAG]])) { |
||
88 | // This tag shouldn't be here, abort |
||
89 | SLog::Write(LOGLEVEL_WBXMLSTACK, sprintf("Tag '%s' unexpected in type XML type '%s'", $entity[EN_TAG], get_class($this))); |
||
90 | |||
91 | return false; |
||
92 | } |
||
93 | |||
94 | $map = $this->mapping[$entity[EN_TAG]]; |
||
95 | |||
96 | // Handle an array |
||
97 | if (isset($map[self::STREAMER_ARRAY])) { |
||
98 | WBXMLDecoder::ResetInWhile("decodeArray"); |
||
99 | while (WBXMLDecoder::InWhile("decodeArray")) { |
||
100 | $streamertype = $map[self::STREAMER_TYPE] ?? false; |
||
101 | // do not get start tag for an array without a container |
||
102 | if (!(isset($map[self::STREAMER_PROP]) && $map[self::STREAMER_PROP] == self::STREAMER_TYPE_NO_CONTAINER)) { |
||
103 | // are there multiple possibilities for element encapsulation tags? |
||
104 | if (is_array($map[self::STREAMER_ARRAY])) { |
||
105 | $encapTagsTypes = $map[self::STREAMER_ARRAY]; |
||
106 | } |
||
107 | else { |
||
108 | // set $streamertype to null if the element is a single string (e.g. category) |
||
109 | $encapTagsTypes = [$map[self::STREAMER_ARRAY] => isset($map[self::STREAMER_TYPE]) ? $map[self::STREAMER_TYPE] : null]; |
||
110 | } |
||
111 | |||
112 | // Identify the used tag |
||
113 | $streamertype = false; |
||
114 | foreach ($encapTagsTypes as $tag => $type) { |
||
115 | if ($decoder->getElementStartTag($tag)) { |
||
116 | $streamertype = $type; |
||
117 | } |
||
118 | } |
||
119 | if ($streamertype === false) { |
||
120 | break; |
||
121 | } |
||
122 | } |
||
123 | if ($streamertype) { |
||
124 | $decoded = new $streamertype(); |
||
125 | $decoded->Decode($decoder); |
||
126 | } |
||
127 | else { |
||
128 | $decoded = $decoder->getElementContent(); |
||
129 | } |
||
130 | |||
131 | if (!isset($this->{$map[self::STREAMER_VAR]})) { |
||
132 | $this->{$map[self::STREAMER_VAR]} = [$decoded]; |
||
133 | } |
||
134 | else { |
||
135 | array_push($this->{$map[self::STREAMER_VAR]}, $decoded); |
||
136 | } |
||
137 | |||
138 | if (!$decoder->getElementEndTag()) { // end tag of a container element |
||
139 | return false; |
||
140 | } |
||
141 | |||
142 | if (isset($map[self::STREAMER_PROP]) && $map[self::STREAMER_PROP] == self::STREAMER_TYPE_NO_CONTAINER) { |
||
143 | $e = $decoder->peek(); |
||
144 | // go back to the initial while if another block of no container elements is found |
||
145 | if ($e[EN_TYPE] == EN_TYPE_STARTTAG) { |
||
146 | continue 2; |
||
147 | } |
||
148 | // break on end tag because no container elements block end is reached |
||
149 | if ($e[EN_TYPE] == EN_TYPE_ENDTAG) { |
||
150 | break; |
||
151 | } |
||
152 | if (empty($e)) { |
||
153 | break; |
||
154 | } |
||
155 | } |
||
156 | } |
||
157 | // do not get end tag for an array without a container |
||
158 | if (!(isset($map[self::STREAMER_PROP]) && $map[self::STREAMER_PROP] == self::STREAMER_TYPE_NO_CONTAINER)) { |
||
159 | if (!$decoder->getElementEndTag()) { // end tag of container |
||
160 | return false; |
||
161 | } |
||
162 | } |
||
163 | } |
||
164 | else { // Handle single value |
||
165 | if (isset($map[self::STREAMER_TYPE])) { |
||
166 | // Complex type, decode recursively |
||
167 | if ($map[self::STREAMER_TYPE] == self::STREAMER_TYPE_DATE || $map[self::STREAMER_TYPE] == self::STREAMER_TYPE_DATE_DASHES) { |
||
168 | $decoded = Utils::ParseDate($decoder->getElementContent()); |
||
169 | if (!$decoder->getElementEndTag()) { |
||
170 | return false; |
||
171 | } |
||
172 | } |
||
173 | elseif ($map[self::STREAMER_TYPE] == self::STREAMER_TYPE_HEX) { |
||
174 | $decoded = hex2bin($decoder->getElementContent()); |
||
|
|||
175 | if (!$decoder->getElementEndTag()) { |
||
176 | return false; |
||
177 | } |
||
178 | } |
||
179 | // explode comma or semicolon strings into arrays |
||
180 | elseif ($map[self::STREAMER_TYPE] == self::STREAMER_TYPE_COMMA_SEPARATED || $map[self::STREAMER_TYPE] == self::STREAMER_TYPE_SEMICOLON_SEPARATED) { |
||
181 | $glue = ($map[self::STREAMER_TYPE] == self::STREAMER_TYPE_COMMA_SEPARATED) ? ", " : "; "; |
||
182 | $decoded = explode($glue, $decoder->getElementContent()); |
||
183 | if (!$decoder->getElementEndTag()) { |
||
184 | return false; |
||
185 | } |
||
186 | } |
||
187 | elseif ($map[self::STREAMER_TYPE] == self::STREAMER_TYPE_STREAM_ASPLAIN) { |
||
188 | $decoded = StringStreamWrapper::Open($decoder->getElementContent()); |
||
189 | if (!$decoder->getElementEndTag()) { |
||
190 | return false; |
||
191 | } |
||
192 | } |
||
193 | else { |
||
194 | $subdecoder = new $map[self::STREAMER_TYPE](); |
||
195 | if ($subdecoder->Decode($decoder) === false) { |
||
196 | return false; |
||
197 | } |
||
198 | |||
199 | $decoded = $subdecoder; |
||
200 | |||
201 | if (!$decoder->getElementEndTag()) { |
||
202 | SLog::Write(LOGLEVEL_WBXMLSTACK, sprintf("No end tag for '%s'", $entity[EN_TAG])); |
||
203 | |||
204 | return false; |
||
205 | } |
||
206 | } |
||
207 | } |
||
208 | else { |
||
209 | // Simple type, just get content |
||
210 | $decoded = $decoder->getElementContent(); |
||
211 | |||
212 | if ($decoded === false) { |
||
213 | // the tag is declared to have content, but no content is available. |
||
214 | // set an empty content |
||
215 | $decoded = ""; |
||
216 | } |
||
217 | |||
218 | if (!$decoder->getElementEndTag()) { |
||
219 | SLog::Write(LOGLEVEL_WBXMLSTACK, sprintf("Unable to get end tag for '%s'", $entity[EN_TAG])); |
||
220 | |||
221 | return false; |
||
222 | } |
||
223 | } |
||
224 | // $decoded now contains data object (or string) |
||
225 | $this->{$map[self::STREAMER_VAR]} = $decoded; |
||
226 | } |
||
227 | } |
||
228 | elseif ($entity[EN_TYPE] == EN_TYPE_ENDTAG) { |
||
229 | $decoder->ungetElement($entity); |
||
230 | |||
231 | break; |
||
232 | } |
||
233 | else { |
||
234 | SLog::Write(LOGLEVEL_WBXMLSTACK, "Unexpected content in type"); |
||
235 | |||
236 | break; |
||
237 | } |
||
466 |