| Conditions | 48 |
| Paths | 15874 |
| Total Lines | 238 |
| Code Lines | 143 |
| 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 |
||
| 93 | public function parse() |
||
| 94 | { |
||
| 95 | $catalog = new Catalog(); |
||
| 96 | $entry = array(); |
||
| 97 | |||
| 98 | // A new entry has been just inserted. |
||
| 99 | $justNewEntry = false; |
||
| 100 | $firstLine = true; |
||
| 101 | |||
| 102 | // Used to remember last key in a multiline previous entry. |
||
| 103 | $lastPreviousKey = null; |
||
| 104 | $state = null; |
||
| 105 | $lineNumber = 0; |
||
| 106 | |||
| 107 | while (!$this->sourceHandler->ended()) { |
||
| 108 | $line = trim($this->sourceHandler->getNextLine()); |
||
| 109 | $split = preg_split('/\s+/ ', $line, 2); |
||
| 110 | $key = $split[0]; |
||
| 111 | |||
| 112 | // If a blank line is found, or a new msgid when already got one |
||
| 113 | if ($line === '' || ($key === 'msgid' && isset($entry['msgid']))) { |
||
| 114 | // Two consecutive blank lines |
||
| 115 | if ($justNewEntry) { |
||
| 116 | $lineNumber++; |
||
| 117 | continue; |
||
| 118 | } |
||
| 119 | |||
| 120 | if ($firstLine) { |
||
| 121 | $firstLine = false; |
||
| 122 | if (self::isHeader($entry)) { |
||
| 123 | $catalog->addHeaders(array_filter(explode('\\n', $entry['msgstr']))); |
||
| 124 | } else { |
||
| 125 | $catalog->addEntry(EntryFactory::createFromArray($entry)); |
||
| 126 | } |
||
| 127 | } else { |
||
| 128 | // A new entry is found! |
||
| 129 | $catalog->addEntry(EntryFactory::createFromArray($entry)); |
||
| 130 | } |
||
| 131 | |||
| 132 | $entry = array(); |
||
| 133 | $state = null; |
||
| 134 | $justNewEntry = true; |
||
| 135 | $lastPreviousKey = null; |
||
| 136 | if ($line === '') { |
||
| 137 | $lineNumber++; |
||
| 138 | continue; |
||
| 139 | } |
||
| 140 | } |
||
| 141 | |||
| 142 | $justNewEntry = false; // ? |
||
| 143 | $data = isset($split[1]) ? $split[1] : null; |
||
| 144 | |||
| 145 | switch ($key) { |
||
| 146 | // Flagged translation |
||
| 147 | case '#,': |
||
| 148 | $entry['flags'] = preg_split('/,\s*/', $data); |
||
| 149 | break; |
||
| 150 | |||
| 151 | // # Translator comments |
||
| 152 | case '#': |
||
| 153 | $entry['tcomment'] = !isset($entry['tcomment']) ? array() : $entry['tcomment']; |
||
| 154 | $entry['tcomment'][] = $data; |
||
| 155 | break; |
||
| 156 | |||
| 157 | // #. Comments extracted from source code |
||
| 158 | case '#.': |
||
| 159 | $entry['ccomment'] = !isset($entry['ccomment']) ? array() : $entry['ccomment']; |
||
| 160 | $entry['ccomment'][] = $data; |
||
| 161 | break; |
||
| 162 | |||
| 163 | // Reference |
||
| 164 | case '#:': |
||
| 165 | $entry['reference'][] = addslashes($data); |
||
| 166 | break; |
||
| 167 | |||
| 168 | |||
| 169 | case '#|': // #| Previous untranslated string |
||
| 170 | case '#~': // #~ Old entry |
||
| 171 | case '#~|': // #~| Previous-Old untranslated string. Reported by @Cellard |
||
| 172 | switch ($key) { |
||
| 173 | case '#|': |
||
| 174 | $key = 'previous'; |
||
| 175 | break; |
||
| 176 | case '#~': |
||
| 177 | $key = 'obsolete'; |
||
| 178 | break; |
||
| 179 | case '#~|': |
||
| 180 | $key = 'previous-obsolete'; |
||
| 181 | break; |
||
| 182 | } |
||
| 183 | |||
| 184 | $tmpParts = explode(' ', $data); |
||
| 185 | $tmpKey = $tmpParts[0]; |
||
| 186 | |||
| 187 | if (!in_array($tmpKey, array('msgid', 'msgid_plural', 'msgstr', 'msgctxt'), true)) { |
||
| 188 | // If there is a multi-line previous string we must remember |
||
| 189 | // what key was first line. |
||
| 190 | $tmpKey = $lastPreviousKey; |
||
| 191 | $str = $data; |
||
| 192 | } else { |
||
| 193 | $str = implode(' ', array_slice($tmpParts, 1)); |
||
| 194 | } |
||
| 195 | |||
| 196 | //array('obsolete' => true, 'msgid' => '', 'msgstr' => ''); |
||
| 197 | |||
| 198 | if (strpos($key, 'obsolete') !== false) { |
||
| 199 | $entry['obsolete'] = true; |
||
| 200 | switch ($tmpKey) { |
||
| 201 | case 'msgid': |
||
| 202 | if (!isset($entry['msgid'])) { |
||
| 203 | $entry['msgid'] = ''; |
||
| 204 | } |
||
| 205 | $entry['msgid'].= trim($str, '"'); |
||
| 206 | $lastPreviousKey = $tmpKey; |
||
| 207 | break; |
||
| 208 | |||
| 209 | case 'msgstr': |
||
| 210 | if (!isset($entry['msgstr'])) { |
||
| 211 | $entry['msgstr'] = ''; |
||
| 212 | } |
||
| 213 | $entry['msgstr'].= trim($str, '"'); |
||
| 214 | $lastPreviousKey = $tmpKey; |
||
| 215 | break; |
||
| 216 | |||
| 217 | case 'msgctxt': |
||
| 218 | $entry['msgctxt'] = trim($str, '"'); |
||
| 219 | $lastPreviousKey = $tmpKey; |
||
| 220 | break; |
||
| 221 | |||
| 222 | default: |
||
| 223 | break; |
||
| 224 | } |
||
| 225 | } else { |
||
| 226 | $entry[$key] = isset($entry[$key]) ? $entry[$key] : array('msgid' => '', 'msgstr' => ''); |
||
| 227 | } |
||
| 228 | |||
| 229 | if ($key !== 'obsolete') { |
||
| 230 | switch ($tmpKey) { |
||
| 231 | case 'msgid': |
||
| 232 | case 'msgid_plural': |
||
| 233 | case 'msgstr': |
||
| 234 | $entry[$key][$tmpKey] = trim($str, '"'); |
||
| 235 | $lastPreviousKey = $tmpKey; |
||
| 236 | break; |
||
| 237 | |||
| 238 | default: |
||
| 239 | $entry[$key][$tmpKey] = $str; |
||
| 240 | break; |
||
| 241 | } |
||
| 242 | } |
||
| 243 | break; |
||
| 244 | |||
| 245 | |||
| 246 | // context |
||
| 247 | // Allows disambiguations of different messages that have same msgid. |
||
| 248 | // Example: |
||
| 249 | // |
||
| 250 | // #: tools/observinglist.cpp:700 |
||
| 251 | // msgctxt "First letter in 'Scope'" |
||
| 252 | // msgid "S" |
||
| 253 | // msgstr "" |
||
| 254 | // |
||
| 255 | // #: skycomponents/horizoncomponent.cpp:429 |
||
| 256 | // msgctxt "South" |
||
| 257 | // msgid "S" |
||
| 258 | // msgstr "" |
||
| 259 | case 'msgctxt': |
||
| 260 | // untranslated-string |
||
| 261 | case 'msgid': |
||
| 262 | // untranslated-string-plural |
||
| 263 | case 'msgid_plural': |
||
| 264 | $state = $key; |
||
| 265 | if (!isset($entry[$state])) { |
||
| 266 | $entry[$state] = ''; |
||
| 267 | } |
||
| 268 | |||
| 269 | $entry[$state] .= trim($data, '"'); |
||
| 270 | break; |
||
| 271 | // translated-string |
||
| 272 | case 'msgstr': |
||
| 273 | $state = 'msgstr'; |
||
| 274 | $entry[$state] = trim($data, '"'); |
||
| 275 | break; |
||
| 276 | |||
| 277 | default: |
||
| 278 | if (strpos($key, 'msgstr[') !== false) { |
||
| 279 | // translated-string-case-n |
||
| 280 | $state = $key; |
||
| 281 | $entry[$state] = trim($data, '"'); |
||
| 282 | } else { |
||
| 283 | // "multiline" lines |
||
| 284 | switch ($state) { |
||
| 285 | case 'msgctxt': |
||
| 286 | case 'msgid': |
||
| 287 | case 'msgid_plural': |
||
| 288 | case (strpos($state, 'msgstr[') !== false): |
||
| 289 | if (!isset($entry[$state])) { |
||
| 290 | $entry[$state] = ''; |
||
| 291 | } |
||
| 292 | |||
| 293 | if (is_string($entry[$state])) { |
||
| 294 | // Convert it to array |
||
| 295 | //$entry[$state] = array($entry[$state]); |
||
| 296 | $entry[$state] = trim($entry[$state], '"'); |
||
| 297 | } |
||
| 298 | $entry[$state] .= trim($line, '"'); |
||
| 299 | break; |
||
| 300 | |||
| 301 | case 'msgstr': |
||
| 302 | // Special fix where msgid is "" |
||
| 303 | $entry['msgstr'] .= trim($line, '"'); |
||
| 304 | /*if ($entry['msgid'] === "\"\"") { |
||
| 305 | $entry['msgstr'].= trim($line, '"'); |
||
| 306 | } else { |
||
| 307 | $entry['msgstr'].= $line; |
||
| 308 | }*/ |
||
| 309 | break; |
||
| 310 | |||
| 311 | default: |
||
| 312 | throw new \Exception( |
||
| 313 | 'Parser: Parse error! Unknown key "'.$key.'" on line '.($lineNumber + 1) |
||
| 314 | ); |
||
| 315 | } |
||
| 316 | } |
||
| 317 | break; |
||
| 318 | } |
||
| 319 | |||
| 320 | $lineNumber++; |
||
| 321 | } |
||
| 322 | $this->sourceHandler->close(); |
||
| 323 | |||
| 324 | // add final entry |
||
| 325 | if ($state === 'msgstr') { |
||
| 326 | $catalog->addEntry(EntryFactory::createFromArray($entry)); |
||
| 327 | } |
||
| 328 | |||
| 329 | return $catalog; |
||
| 330 | } |
||
| 331 | |||
| 378 |