| Conditions | 22 |
| Paths | 208 |
| Total Lines | 96 |
| Code Lines | 56 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 73 | function phpmo_parse_po_file($in) { |
||
| 74 | // read .po file |
||
| 75 | $fc = file_get_contents($in); |
||
| 76 | // normalize newlines |
||
| 77 | $fc = str_replace( |
||
| 78 | array( |
||
| 79 | "\r\n", |
||
| 80 | "\r" |
||
| 81 | ), |
||
| 82 | array( |
||
| 83 | "\n", |
||
| 84 | "\n" |
||
| 85 | ), |
||
| 86 | $fc |
||
| 87 | ); |
||
| 88 | |||
| 89 | // results array |
||
| 90 | $hash = array(); |
||
| 91 | // temporary array |
||
| 92 | $temp = array(); |
||
| 93 | // state |
||
| 94 | $state = null; |
||
| 95 | $fuzzy = false; |
||
| 96 | |||
| 97 | // iterate over lines |
||
| 98 | foreach (explode("\n", $fc) as $line) { |
||
| 99 | $line = trim($line); |
||
| 100 | if ($line === '') { |
||
| 101 | continue; |
||
| 102 | } |
||
| 103 | |||
| 104 | list ($key, $data) = explode(' ', $line, 2); |
||
| 105 | |||
| 106 | switch ($key) { |
||
| 107 | case '#,' : // flag... |
||
| 108 | $fuzzy = in_array('fuzzy', preg_split('/,\s*/', $data)); |
||
| 109 | case '#' : // translator-comments |
||
| 110 | case '#.' : // extracted-comments |
||
| 111 | case '#:' : // reference... |
||
| 112 | case '#~' : // reference... |
||
| 113 | case '#|' : // msgid previous-untranslated-string |
||
| 114 | // start a new entry |
||
| 115 | if (sizeof($temp) && array_key_exists('msgid', $temp) && array_key_exists('msgstr', $temp)) { |
||
| 116 | if (!$fuzzy) { |
||
| 117 | $hash[] = $temp; |
||
| 118 | } |
||
| 119 | $temp = array(); |
||
| 120 | $state = null; |
||
| 121 | $fuzzy = false; |
||
| 122 | } |
||
| 123 | break; |
||
| 124 | case 'msgctxt' : |
||
| 125 | // context |
||
| 126 | case 'msgid' : |
||
| 127 | // untranslated-string |
||
| 128 | case 'msgid_plural' : |
||
| 129 | // untranslated-string-plural |
||
| 130 | $state = $key; |
||
| 131 | $temp[$state] = $data; |
||
| 132 | break; |
||
| 133 | case 'msgstr' : |
||
| 134 | // translated-string |
||
| 135 | $state = 'msgstr'; |
||
| 136 | if ($data) { |
||
| 137 | $temp[$state][] = $data; |
||
| 138 | } |
||
| 139 | |||
| 140 | break; |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | // add final entry |
||
| 145 | if ($state == 'msgstr') { |
||
| 146 | $hash[] = $temp; |
||
| 147 | } |
||
| 148 | |||
| 149 | // Cleanup data, merge multiline entries, reindex hash for ksort |
||
| 150 | $temp = $hash; |
||
| 151 | $hash = array(); |
||
| 152 | foreach ($temp as $entry) { |
||
| 153 | foreach ($entry as & $v) { |
||
| 154 | $v = phpmo_clean_helper($v); |
||
| 155 | // var_dumps($v); |
||
| 156 | if ($v === FALSE) { |
||
| 157 | // parse error |
||
| 158 | continue; |
||
| 159 | // return FALSE; |
||
| 160 | } |
||
| 161 | } |
||
| 162 | |||
| 163 | $hash[$entry['msgid']] = $entry; |
||
| 164 | } |
||
| 165 | // var_dumps($hash); |
||
| 166 | //exit; |
||
| 167 | return $hash; |
||
| 168 | } |
||
| 169 | |||
| 275 | } |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.