| Conditions | 60 |
| Paths | 56 |
| Total Lines | 298 |
| Code Lines | 179 |
| 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 |
||
| 100 | public function parse() |
||
| 101 | { |
||
| 102 | $catalog = new Catalog(); |
||
| 103 | $this->lineNumber = 0; |
||
| 104 | $entry = array(); |
||
| 105 | $this->mode = null; // current mode |
||
|
|
|||
| 106 | $this->property = null; // current property |
||
| 107 | |||
| 108 | // Flags |
||
| 109 | $headersFound = false; |
||
| 110 | |||
| 111 | // A new entry has been just inserted. |
||
| 112 | |||
| 113 | // Used to remember last key in a multiline previous entry. |
||
| 114 | $lastPreviousKey = null; |
||
| 115 | $state = null; |
||
| 116 | |||
| 117 | while (!$this->sourceHandler->ended()) { |
||
| 118 | |||
| 119 | $line = trim($this->sourceHandler->getNextLine()); |
||
| 120 | |||
| 121 | if ($this->shouldIgnoreLine($line, $entry)) { |
||
| 122 | $this->lineNumber++; |
||
| 123 | continue; |
||
| 124 | } |
||
| 125 | |||
| 126 | if ($this->shouldCloseEntry($line, $entry)) { |
||
| 127 | if (!$headersFound && $this->isHeader($entry)) { |
||
| 128 | $headersFound = true; |
||
| 129 | $catalog->addHeaders(array_filter(explode('\\n', $entry['msgstr']))); |
||
| 130 | } else { |
||
| 131 | $catalog->addEntry(EntryFactory::createFromArray($entry)); |
||
| 132 | } |
||
| 133 | |||
| 134 | $entry = array(); |
||
| 135 | $this->mode = null; |
||
| 136 | $this->property = null; |
||
| 137 | |||
| 138 | if (empty($line)) { |
||
| 139 | $this->lineNumber++; |
||
| 140 | continue; |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | $firstChar = strlen($line) > 0 ? $line[0] : ''; |
||
| 145 | |||
| 146 | switch ($firstChar) { |
||
| 147 | case '#': |
||
| 148 | $entry = $this->parseComment($line, $entry); |
||
| 149 | break; |
||
| 150 | |||
| 151 | case 'm': |
||
| 152 | $entry = $this->parseProperty($line, $entry); |
||
| 153 | break; |
||
| 154 | |||
| 155 | case '"': |
||
| 156 | $entry = $this->parseMultiline($line, $entry); |
||
| 157 | break; |
||
| 158 | } |
||
| 159 | |||
| 160 | $this->lineNumber++; |
||
| 161 | continue; |
||
| 162 | |||
| 163 | |||
| 164 | $split = preg_split('/\s+/ ', $line, 2); |
||
| 165 | $lineKey = $split[0]; |
||
| 166 | |||
| 167 | if (empty($line) && count($entry) === 0) { |
||
| 168 | $lineNumber++; |
||
| 169 | continue; |
||
| 170 | } |
||
| 171 | |||
| 172 | |||
| 173 | // If a blank line is found, or a new msgid when already got one |
||
| 174 | if ($line === '' || ($lineKey === 'msgid' && isset($entry['msgid']))) { |
||
| 175 | // Two consecutive blank lines |
||
| 176 | if ($justNewEntry) { |
||
| 177 | $lineNumber++; |
||
| 178 | continue; |
||
| 179 | } |
||
| 180 | |||
| 181 | if ($firstLine) { |
||
| 182 | $firstLine = false; |
||
| 183 | if (self::isHeader($entry)) { |
||
| 184 | $catalog->addHeaders(array_filter(explode('\\n', $entry['msgstr']))); |
||
| 185 | } else { |
||
| 186 | $catalog->addEntry(EntryFactory::createFromArray($entry)); |
||
| 187 | } |
||
| 188 | } else { |
||
| 189 | // A new entry is found! |
||
| 190 | $catalog->addEntry(EntryFactory::createFromArray($entry)); |
||
| 191 | } |
||
| 192 | |||
| 193 | $entry = array(); |
||
| 194 | $state = null; |
||
| 195 | $justNewEntry = true; |
||
| 196 | $lastPreviousKey = null; |
||
| 197 | if ($line === '') { |
||
| 198 | $lineNumber++; |
||
| 199 | continue; |
||
| 200 | } |
||
| 201 | } |
||
| 202 | |||
| 203 | $justNewEntry = false; // ? |
||
| 204 | $data = isset($split[1]) ? $split[1] : null; |
||
| 205 | |||
| 206 | switch ($lineKey) { |
||
| 207 | // Flagged translation |
||
| 208 | case '#,': |
||
| 209 | $entry['flags'] = preg_split('/,\s*/', $data); |
||
| 210 | break; |
||
| 211 | |||
| 212 | // # Translator comments |
||
| 213 | case '#': |
||
| 214 | $entry['tcomment'] = !isset($entry['tcomment']) ? array() : $entry['tcomment']; |
||
| 215 | $entry['tcomment'][] = $data; |
||
| 216 | break; |
||
| 217 | |||
| 218 | // #. Comments extracted from source code |
||
| 219 | case '#.': |
||
| 220 | $entry['ccomment'] = !isset($entry['ccomment']) ? array() : $entry['ccomment']; |
||
| 221 | $entry['ccomment'][] = $data; |
||
| 222 | break; |
||
| 223 | |||
| 224 | // Reference |
||
| 225 | case '#:': |
||
| 226 | $entry['reference'][] = addslashes($data); |
||
| 227 | break; |
||
| 228 | |||
| 229 | |||
| 230 | case '#|': // #| Previous untranslated string |
||
| 231 | case '#~': // #~ Old entry |
||
| 232 | case '#~|': // #~| Previous-Old untranslated string. Reported by @Cellard |
||
| 233 | switch ($lineKey) { |
||
| 234 | case '#|': |
||
| 235 | $lineKey = 'previous'; |
||
| 236 | break; |
||
| 237 | case '#~': |
||
| 238 | $lineKey = 'obsolete'; |
||
| 239 | break; |
||
| 240 | case '#~|': |
||
| 241 | $lineKey = 'previous-obsolete'; |
||
| 242 | break; |
||
| 243 | } |
||
| 244 | |||
| 245 | $tmpParts = explode(' ', $data); |
||
| 246 | $tmpKey = $tmpParts[0]; |
||
| 247 | |||
| 248 | if (!in_array($tmpKey, array('msgid', 'msgid_plural', 'msgstr', 'msgctxt'), true)) { |
||
| 249 | // If there is a multi-line previous string we must remember |
||
| 250 | // what key was first line. |
||
| 251 | $tmpKey = $lastPreviousKey; |
||
| 252 | $str = $data; |
||
| 253 | } else { |
||
| 254 | $str = implode(' ', array_slice($tmpParts, 1)); |
||
| 255 | } |
||
| 256 | |||
| 257 | //array('obsolete' => true, 'msgid' => '', 'msgstr' => ''); |
||
| 258 | |||
| 259 | if (strpos($lineKey, 'obsolete') !== false) { |
||
| 260 | $entry['obsolete'] = true; |
||
| 261 | switch ($tmpKey) { |
||
| 262 | case 'msgid': |
||
| 263 | if (!isset($entry['msgid'])) { |
||
| 264 | $entry['msgid'] = ''; |
||
| 265 | } |
||
| 266 | $entry['msgid'] .= trim($str, '"'); |
||
| 267 | $lastPreviousKey = $tmpKey; |
||
| 268 | break; |
||
| 269 | |||
| 270 | case 'msgstr': |
||
| 271 | if (!isset($entry['msgstr'])) { |
||
| 272 | $entry['msgstr'] = ''; |
||
| 273 | } |
||
| 274 | $entry['msgstr'] .= trim($str, '"'); |
||
| 275 | $lastPreviousKey = $tmpKey; |
||
| 276 | break; |
||
| 277 | |||
| 278 | case 'msgctxt': |
||
| 279 | $entry['msgctxt'] = trim($str, '"'); |
||
| 280 | $lastPreviousKey = $tmpKey; |
||
| 281 | break; |
||
| 282 | |||
| 283 | default: |
||
| 284 | break; |
||
| 285 | } |
||
| 286 | } else { |
||
| 287 | $entry[$lineKey] = isset($entry[$lineKey]) ? $entry[$lineKey] : array( |
||
| 288 | 'msgid' => '', |
||
| 289 | 'msgstr' => '', |
||
| 290 | ); |
||
| 291 | } |
||
| 292 | |||
| 293 | if ($lineKey !== 'obsolete') { |
||
| 294 | switch ($tmpKey) { |
||
| 295 | case 'msgid': |
||
| 296 | case 'msgid_plural': |
||
| 297 | case 'msgstr': |
||
| 298 | if (!isset($entry[$lineKey][$tmpKey])) { |
||
| 299 | $entry[$lineKey][$tmpKey] = ''; |
||
| 300 | } |
||
| 301 | $entry[$lineKey][$tmpKey] .= trim($str, '"'); |
||
| 302 | $lastPreviousKey = $tmpKey; |
||
| 303 | break; |
||
| 304 | |||
| 305 | default: |
||
| 306 | $entry[$lineKey][$tmpKey] = $str; |
||
| 307 | break; |
||
| 308 | } |
||
| 309 | } |
||
| 310 | break; |
||
| 311 | |||
| 312 | |||
| 313 | // context |
||
| 314 | // Allows disambiguations of different messages that have same msgid. |
||
| 315 | // Example: |
||
| 316 | // |
||
| 317 | // #: tools/observinglist.cpp:700 |
||
| 318 | // msgctxt "First letter in 'Scope'" |
||
| 319 | // msgid "S" |
||
| 320 | // msgstr "" |
||
| 321 | // |
||
| 322 | // #: skycomponents/horizoncomponent.cpp:429 |
||
| 323 | // msgctxt "South" |
||
| 324 | // msgid "S" |
||
| 325 | // msgstr "" |
||
| 326 | case 'msgctxt': |
||
| 327 | // untranslated-string |
||
| 328 | case 'msgid': |
||
| 329 | // untranslated-string-plural |
||
| 330 | case 'msgid_plural': |
||
| 331 | $state = $lineKey; |
||
| 332 | if (!isset($entry[$state])) { |
||
| 333 | $entry[$state] = ''; |
||
| 334 | } |
||
| 335 | |||
| 336 | $entry[$state] .= trim($data, '"'); |
||
| 337 | break; |
||
| 338 | // translated-string |
||
| 339 | case 'msgstr': |
||
| 340 | $state = 'msgstr'; |
||
| 341 | $entry[$state] = trim($data, '"'); |
||
| 342 | break; |
||
| 343 | |||
| 344 | default: |
||
| 345 | if (strpos($lineKey, 'msgstr[') !== false) { |
||
| 346 | // translated-string-case-n |
||
| 347 | $state = $lineKey; |
||
| 348 | $entry[$state] = trim($data, '"'); |
||
| 349 | } else { |
||
| 350 | // "multiline" lines |
||
| 351 | switch ($state) { |
||
| 352 | case 'msgctxt': |
||
| 353 | case 'msgid': |
||
| 354 | case 'msgid_plural': |
||
| 355 | case (strpos($state, 'msgstr[') !== false): |
||
| 356 | if (!isset($entry[$state])) { |
||
| 357 | $entry[$state] = ''; |
||
| 358 | } |
||
| 359 | |||
| 360 | if (is_string($entry[$state])) { |
||
| 361 | // Convert it to array |
||
| 362 | //$entry[$state] = array($entry[$state]); |
||
| 363 | $entry[$state] = trim($entry[$state], '"'); |
||
| 364 | } |
||
| 365 | $entry[$state] .= trim($line, '"'); |
||
| 366 | break; |
||
| 367 | |||
| 368 | case 'msgstr': |
||
| 369 | // Special fix where msgid is "" |
||
| 370 | $entry['msgstr'] .= trim($line, '"'); |
||
| 371 | /*if ($entry['msgid'] === "\"\"") { |
||
| 372 | $entry['msgstr'].= trim($line, '"'); |
||
| 373 | } else { |
||
| 374 | $entry['msgstr'].= $line; |
||
| 375 | }*/ |
||
| 376 | break; |
||
| 377 | |||
| 378 | default: |
||
| 379 | throw new \Exception( |
||
| 380 | 'Parser: Parse error! Unknown key "'.$lineKey.'" on line '.($lineNumber + 1) |
||
| 381 | ); |
||
| 382 | } |
||
| 383 | } |
||
| 384 | break; |
||
| 385 | } |
||
| 386 | |||
| 387 | $lineNumber++; |
||
| 388 | } |
||
| 389 | $this->sourceHandler->close(); |
||
| 390 | |||
| 391 | // add final entry |
||
| 392 | if (count($entry)) { |
||
| 393 | $catalog->addEntry(EntryFactory::createFromArray($entry)); |
||
| 394 | } |
||
| 395 | |||
| 396 | return $catalog; |
||
| 397 | } |
||
| 398 | |||
| 545 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: