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