Conditions | 45 |
Paths | 73 |
Total Lines | 265 |
Code Lines | 130 |
Lines | 61 |
Ratio | 23.02 % |
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 |
||
250 | private function readLine() { |
||
251 | // Read a line and set the stream finished indicator if it was not |
||
252 | // possible anymore. |
||
253 | $line = fgets($this->_fd); |
||
254 | $this->_finished = ($line === FALSE); |
||
255 | |||
256 | if (!$this->_finished) { |
||
257 | |||
258 | if ($this->_line_number == 0) { |
||
259 | // The first line might come with a UTF-8 BOM, which should be removed. |
||
260 | $line = str_replace("\xEF\xBB\xBF", '', $line); |
||
261 | // Current plurality for 'msgstr[]'. |
||
262 | $this->_current_plural_index = 0; |
||
263 | } |
||
264 | |||
265 | // Track the line number for error reporting. |
||
266 | $this->_line_number++; |
||
267 | |||
268 | // Initialize common values for error logging. |
||
269 | $log_vars = array( |
||
270 | '%uri' => $this->getURI(), |
||
271 | '%line' => $this->_line_number, |
||
272 | ); |
||
273 | |||
274 | // Trim away the linefeed. \\n might appear at the end of the string if |
||
275 | // another line continuing the same string follows. We can remove that. |
||
276 | $line = trim(strtr($line, array("\\\n" => ""))); |
||
277 | |||
278 | if (!strncmp('#', $line, 1)) { |
||
279 | // Lines starting with '#' are comments. |
||
280 | |||
281 | if ($this->_context == 'COMMENT') { |
||
282 | // Already in comment context, add to current comment. |
||
283 | $this->_current_item['#'][] = substr($line, 1); |
||
284 | } |
||
285 | elseif (($this->_context == 'MSGSTR') || ($this->_context == 'MSGSTR_ARR')) { |
||
286 | // We are currently in string context, save current item. |
||
287 | $this->setItemFromArray($this->_current_item); |
||
288 | |||
289 | // Start a new entry for the comment. |
||
290 | $this->_current_item = array(); |
||
291 | $this->_current_item['#'][] = substr($line, 1); |
||
292 | |||
293 | $this->_context = 'COMMENT'; |
||
294 | return; |
||
295 | } |
||
296 | else { |
||
297 | // A comment following any other context is a syntax error. |
||
298 | $this->_errors[] = SafeMarkup::format('The translation stream %uri contains an error: "msgstr" was expected but not found on line %line.', $log_vars); |
||
|
|||
299 | return FALSE; |
||
300 | } |
||
301 | return; |
||
302 | } |
||
303 | elseif (!strncmp('msgid_plural', $line, 12)) { |
||
304 | // A plural form for the current source string. |
||
305 | |||
306 | if ($this->_context != 'MSGID') { |
||
307 | // A plural form can only be added to an msgid directly. |
||
308 | $this->_errors[] = SafeMarkup::format('The translation stream %uri contains an error: "msgid_plural" was expected but not found on line %line.', $log_vars); |
||
309 | return FALSE; |
||
310 | } |
||
311 | |||
312 | // Remove 'msgid_plural' and trim away whitespace. |
||
313 | $line = trim(substr($line, 12)); |
||
314 | |||
315 | // Only the plural source string is left, parse it. |
||
316 | $quoted = $this->parseQuoted($line); |
||
317 | if ($quoted === FALSE) { |
||
318 | // The plural form must be wrapped in quotes. |
||
319 | $this->_errors[] = SafeMarkup::format('The translation stream %uri contains a syntax error on line %line.', $log_vars); |
||
320 | return FALSE; |
||
321 | } |
||
322 | |||
323 | // Append the plural source to the current entry. |
||
324 | if (is_string($this->_current_item['msgid'])) { |
||
325 | // The first value was stored as string. Now we know the context is |
||
326 | // plural, it is converted to array. |
||
327 | $this->_current_item['msgid'] = array($this->_current_item['msgid']); |
||
328 | } |
||
329 | $this->_current_item['msgid'][] = $quoted; |
||
330 | |||
331 | $this->_context = 'MSGID_PLURAL'; |
||
332 | return; |
||
333 | } |
||
334 | View Code Duplication | elseif (!strncmp('msgid', $line, 5)) { |
|
335 | // Starting a new message. |
||
336 | |||
337 | if (($this->_context == 'MSGSTR') || ($this->_context == 'MSGSTR_ARR')) { |
||
338 | // We are currently in string context, save current item. |
||
339 | $this->setItemFromArray($this->_current_item); |
||
340 | |||
341 | // Start a new context for the msgid. |
||
342 | $this->_current_item = array(); |
||
343 | } |
||
344 | elseif ($this->_context == 'MSGID') { |
||
345 | // We are currently already in the context, meaning we passed an id with no data. |
||
346 | $this->_errors[] = SafeMarkup::format('The translation stream %uri contains an error: "msgid" is unexpected on line %line.', $log_vars); |
||
347 | return FALSE; |
||
348 | } |
||
349 | |||
350 | // Remove 'msgid' and trim away whitespace. |
||
351 | $line = trim(substr($line, 5)); |
||
352 | |||
353 | // Only the message id string is left, parse it. |
||
354 | $quoted = $this->parseQuoted($line); |
||
355 | if ($quoted === FALSE) { |
||
356 | // The message id must be wrapped in quotes. |
||
357 | $this->_errors[] = SafeMarkup::format('The translation stream %uri contains an error: invalid format for "msgid" on line %line.', $log_vars, $log_vars); |
||
358 | return FALSE; |
||
359 | } |
||
360 | |||
361 | $this->_current_item['msgid'] = $quoted; |
||
362 | $this->_context = 'MSGID'; |
||
363 | return; |
||
364 | } |
||
365 | View Code Duplication | elseif (!strncmp('msgctxt', $line, 7)) { |
|
366 | // Starting a new context. |
||
367 | |||
368 | if (($this->_context == 'MSGSTR') || ($this->_context == 'MSGSTR_ARR')) { |
||
369 | // We are currently in string context, save current item. |
||
370 | $this->setItemFromArray($this->_current_item); |
||
371 | $this->_current_item = array(); |
||
372 | } |
||
373 | elseif (!empty($this->_current_item['msgctxt'])) { |
||
374 | // A context cannot apply to another context. |
||
375 | $this->_errors[] = SafeMarkup::format('The translation stream %uri contains an error: "msgctxt" is unexpected on line %line.', $log_vars); |
||
376 | return FALSE; |
||
377 | } |
||
378 | |||
379 | // Remove 'msgctxt' and trim away whitespaces. |
||
380 | $line = trim(substr($line, 7)); |
||
381 | |||
382 | // Only the msgctxt string is left, parse it. |
||
383 | $quoted = $this->parseQuoted($line); |
||
384 | if ($quoted === FALSE) { |
||
385 | // The context string must be quoted. |
||
386 | $this->_errors[] = SafeMarkup::format('The translation stream %uri contains an error: invalid format for "msgctxt" on line %line.', $log_vars); |
||
387 | return FALSE; |
||
388 | } |
||
389 | |||
390 | $this->_current_item['msgctxt'] = $quoted; |
||
391 | |||
392 | $this->_context = 'MSGCTXT'; |
||
393 | return; |
||
394 | } |
||
395 | elseif (!strncmp('msgstr[', $line, 7)) { |
||
396 | // A message string for a specific plurality. |
||
397 | |||
398 | if (($this->_context != 'MSGID') && |
||
399 | ($this->_context != 'MSGCTXT') && |
||
400 | ($this->_context != 'MSGID_PLURAL') && |
||
401 | ($this->_context != 'MSGSTR_ARR')) { |
||
402 | // Plural message strings must come after msgid, msgxtxt, |
||
403 | // msgid_plural, or other msgstr[] entries. |
||
404 | $this->_errors[] = SafeMarkup::format('The translation stream %uri contains an error: "msgstr[]" is unexpected on line %line.', $log_vars); |
||
405 | return FALSE; |
||
406 | } |
||
407 | |||
408 | // Ensure the plurality is terminated. |
||
409 | if (strpos($line, ']') === FALSE) { |
||
410 | $this->_errors[] = SafeMarkup::format('The translation stream %uri contains an error: invalid format for "msgstr[]" on line %line.', $log_vars); |
||
411 | return FALSE; |
||
412 | } |
||
413 | |||
414 | // Extract the plurality. |
||
415 | $frombracket = strstr($line, '['); |
||
416 | $this->_current_plural_index = substr($frombracket, 1, strpos($frombracket, ']') - 1); |
||
417 | |||
418 | // Skip to the next whitespace and trim away any further whitespace, |
||
419 | // bringing $line to the message text only. |
||
420 | $line = trim(strstr($line, " ")); |
||
421 | |||
422 | $quoted = $this->parseQuoted($line); |
||
423 | if ($quoted === FALSE) { |
||
424 | // The string must be quoted. |
||
425 | $this->_errors[] = SafeMarkup::format('The translation stream %uri contains an error: invalid format for "msgstr[]" on line %line.', $log_vars); |
||
426 | return FALSE; |
||
427 | } |
||
428 | if (!isset($this->_current_item['msgstr']) || !is_array($this->_current_item['msgstr'])) { |
||
429 | $this->_current_item['msgstr'] = array(); |
||
430 | } |
||
431 | |||
432 | $this->_current_item['msgstr'][$this->_current_plural_index] = $quoted; |
||
433 | |||
434 | $this->_context = 'MSGSTR_ARR'; |
||
435 | return; |
||
436 | } |
||
437 | elseif (!strncmp("msgstr", $line, 6)) { |
||
438 | // A string pair for an msgid (with optional context). |
||
439 | |||
440 | if (($this->_context != 'MSGID') && ($this->_context != 'MSGCTXT')) { |
||
441 | // Strings are only valid within an id or context scope. |
||
442 | $this->_errors[] = SafeMarkup::format('The translation stream %uri contains an error: "msgstr" is unexpected on line %line.', $log_vars); |
||
443 | return FALSE; |
||
444 | } |
||
445 | |||
446 | // Remove 'msgstr' and trim away away whitespaces. |
||
447 | $line = trim(substr($line, 6)); |
||
448 | |||
449 | // Only the msgstr string is left, parse it. |
||
450 | $quoted = $this->parseQuoted($line); |
||
451 | if ($quoted === FALSE) { |
||
452 | // The string must be quoted. |
||
453 | $this->_errors[] = SafeMarkup::format('The translation stream %uri contains an error: invalid format for "msgstr" on line %line.', $log_vars); |
||
454 | return FALSE; |
||
455 | } |
||
456 | |||
457 | $this->_current_item['msgstr'] = $quoted; |
||
458 | |||
459 | $this->_context = 'MSGSTR'; |
||
460 | return; |
||
461 | } |
||
462 | elseif ($line != '') { |
||
463 | // Anything that is not a token may be a continuation of a previous token. |
||
464 | |||
465 | $quoted = $this->parseQuoted($line); |
||
466 | if ($quoted === FALSE) { |
||
467 | // This string must be quoted. |
||
468 | $this->_errors[] = SafeMarkup::format('The translation stream %uri contains an error: string continuation expected on line %line.', $log_vars); |
||
469 | return FALSE; |
||
470 | } |
||
471 | |||
472 | // Append the string to the current item. |
||
473 | if (($this->_context == 'MSGID') || ($this->_context == 'MSGID_PLURAL')) { |
||
474 | if (is_array($this->_current_item['msgid'])) { |
||
475 | // Add string to last array element for plural sources. |
||
476 | $last_index = count($this->_current_item['msgid']) - 1; |
||
477 | $this->_current_item['msgid'][$last_index] .= $quoted; |
||
478 | } |
||
479 | else { |
||
480 | // Singular source, just append the string. |
||
481 | $this->_current_item['msgid'] .= $quoted; |
||
482 | } |
||
483 | } |
||
484 | elseif ($this->_context == 'MSGCTXT') { |
||
485 | // Multiline context name. |
||
486 | $this->_current_item['msgctxt'] .= $quoted; |
||
487 | } |
||
488 | elseif ($this->_context == 'MSGSTR') { |
||
489 | // Multiline translation string. |
||
490 | $this->_current_item['msgstr'] .= $quoted; |
||
491 | } |
||
492 | elseif ($this->_context == 'MSGSTR_ARR') { |
||
493 | // Multiline plural translation string. |
||
494 | $this->_current_item['msgstr'][$this->_current_plural_index] .= $quoted; |
||
495 | } |
||
496 | else { |
||
497 | // No valid context to append to. |
||
498 | $this->_errors[] = SafeMarkup::format('The translation stream %uri contains an error: unexpected string on line %line.', $log_vars); |
||
499 | return FALSE; |
||
500 | } |
||
501 | return; |
||
502 | } |
||
503 | } |
||
504 | |||
505 | // Empty line read or EOF of PO stream, close out the last entry. |
||
506 | if (($this->_context == 'MSGSTR') || ($this->_context == 'MSGSTR_ARR')) { |
||
507 | $this->setItemFromArray($this->_current_item); |
||
508 | $this->_current_item = array(); |
||
509 | } |
||
510 | elseif ($this->_context != 'COMMENT') { |
||
511 | $this->_errors[] = SafeMarkup::format('The translation stream %uri ended unexpectedly at line %line.', $log_vars); |
||
512 | return FALSE; |
||
513 | } |
||
514 | } |
||
515 | |||
600 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.