Conditions | 33 |
Paths | 296 |
Total Lines | 191 |
Code Lines | 76 |
Lines | 16 |
Ratio | 8.38 % |
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 |
||
140 | public function stringToObject($data, array $options = array()) |
||
141 | { |
||
142 | $options = array_merge(self::$options, $options); |
||
143 | |||
144 | // Check the memory cache for already processed strings. |
||
145 | $hash = md5($data . ':' . (int) $options['processSections']); |
||
146 | |||
147 | if (isset(self::$cache[$hash])) |
||
148 | { |
||
149 | return self::$cache[$hash]; |
||
150 | } |
||
151 | |||
152 | // If no lines present just return the object. |
||
153 | if (empty($data)) |
||
154 | { |
||
155 | return new stdClass; |
||
156 | } |
||
157 | |||
158 | $obj = new stdClass; |
||
159 | $section = false; |
||
160 | $array = false; |
||
161 | $lines = explode("\n", $data); |
||
162 | |||
163 | // Process the lines. |
||
164 | foreach ($lines as $line) |
||
165 | { |
||
166 | // Trim any unnecessary whitespace. |
||
167 | $line = trim($line); |
||
168 | |||
169 | // Ignore empty lines and comments. |
||
170 | if (empty($line) || ($line{0} == ';')) |
||
171 | { |
||
172 | continue; |
||
173 | } |
||
174 | |||
175 | if ($options['processSections']) |
||
176 | { |
||
177 | $length = strlen($line); |
||
178 | |||
179 | // If we are processing sections and the line is a section add the object and continue. |
||
180 | if (($line[0] == '[') && ($line[$length - 1] == ']')) |
||
181 | { |
||
182 | $section = substr($line, 1, $length - 2); |
||
183 | $obj->$section = new stdClass; |
||
184 | continue; |
||
185 | } |
||
186 | } |
||
187 | elseif ($line{0} == '[') |
||
188 | { |
||
189 | continue; |
||
190 | } |
||
191 | |||
192 | // Check that an equal sign exists and is not the first character of the line. |
||
193 | if (!strpos($line, '=')) |
||
194 | { |
||
195 | // Maybe throw exception? |
||
196 | continue; |
||
197 | } |
||
198 | |||
199 | // Get the key and value for the line. |
||
200 | list ($key, $value) = explode('=', $line, 2); |
||
201 | |||
202 | // If we have an array item |
||
203 | if (substr($key, -1) == ']' && ($open_brace = strpos($key, '[', 1)) !== false) |
||
204 | { |
||
205 | if ($options['supportArrayValues']) |
||
206 | { |
||
207 | $array = true; |
||
208 | $array_key = substr($key, $open_brace + 1, -1); |
||
209 | |||
210 | // If we have a multi-dimensional array or malformed key |
||
211 | if (strpos($array_key, '[') !== false || strpos($array_key, ']') !== false) |
||
212 | { |
||
213 | // Maybe throw exception? |
||
214 | continue; |
||
215 | } |
||
216 | |||
217 | $key = substr($key, 0, $open_brace); |
||
218 | } |
||
219 | else |
||
220 | { |
||
221 | continue; |
||
222 | } |
||
223 | } |
||
224 | |||
225 | // Validate the key. |
||
226 | if (preg_match('/[^A-Z0-9_]/i', $key)) |
||
227 | { |
||
228 | // Maybe throw exception? |
||
229 | continue; |
||
230 | } |
||
231 | |||
232 | // If the value is quoted then we assume it is a string. |
||
233 | $length = strlen($value); |
||
234 | |||
235 | if ($length && ($value[0] == '"') && ($value[$length - 1] == '"')) |
||
236 | { |
||
237 | // Strip the quotes and Convert the new line characters. |
||
238 | $value = stripcslashes(substr($value, 1, ($length - 2))); |
||
239 | $value = str_replace('\n', "\n", $value); |
||
240 | } |
||
241 | else |
||
242 | { |
||
243 | // If the value is not quoted, we assume it is not a string. |
||
244 | |||
245 | // If the value is 'false' assume boolean false. |
||
246 | if ($value == 'false') |
||
247 | { |
||
248 | $value = false; |
||
249 | } |
||
250 | elseif ($value == 'true') |
||
251 | // If the value is 'true' assume boolean true. |
||
252 | { |
||
253 | $value = true; |
||
254 | } |
||
255 | elseif ($options['parseBooleanWords'] && in_array(strtolower($value), array('yes', 'no'))) |
||
256 | // If the value is 'yes' or 'no' and option is enabled assume appropriate boolean |
||
257 | { |
||
258 | $value = (strtolower($value) == 'yes'); |
||
259 | } |
||
260 | elseif (is_numeric($value)) |
||
261 | // If the value is numeric than it is either a float or int. |
||
262 | { |
||
263 | // If there is a period then we assume a float. |
||
264 | if (strpos($value, '.') !== false) |
||
265 | { |
||
266 | $value = (float) $value; |
||
267 | } |
||
268 | else |
||
269 | { |
||
270 | $value = (int) $value; |
||
271 | } |
||
272 | } |
||
273 | } |
||
274 | |||
275 | // If a section is set add the key/value to the section, otherwise top level. |
||
276 | if ($section) |
||
277 | { |
||
278 | if ($array) |
||
279 | { |
||
280 | if (!isset($obj->$section->$key)) |
||
281 | { |
||
282 | $obj->$section->$key = array(); |
||
283 | } |
||
284 | |||
285 | View Code Duplication | if (!empty($array_key)) |
|
286 | { |
||
287 | $obj->$section->{$key}[$array_key] = $value; |
||
288 | } |
||
289 | else |
||
290 | { |
||
291 | $obj->$section->{$key}[] = $value; |
||
292 | } |
||
293 | } |
||
294 | else |
||
295 | { |
||
296 | $obj->$section->$key = $value; |
||
297 | } |
||
298 | } |
||
299 | else |
||
300 | { |
||
301 | if ($array) |
||
302 | { |
||
303 | if (!isset($obj->$key)) |
||
304 | { |
||
305 | $obj->$key = array(); |
||
306 | } |
||
307 | |||
308 | View Code Duplication | if (!empty($array_key)) |
|
309 | { |
||
310 | $obj->{$key}[$array_key] = $value; |
||
311 | } |
||
312 | else |
||
313 | { |
||
314 | $obj->{$key}[] = $value; |
||
315 | } |
||
316 | } |
||
317 | else |
||
318 | { |
||
319 | $obj->$key = $value; |
||
320 | } |
||
321 | } |
||
322 | |||
323 | $array = false; |
||
324 | } |
||
325 | |||
326 | // Cache the string to save cpu cycles -- thus the world :) |
||
327 | self::$cache[$hash] = clone $obj; |
||
328 | |||
329 | return $obj; |
||
330 | } |
||
331 | |||
365 |
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.