| Conditions | 53 |
| Paths | > 20000 |
| Total Lines | 254 |
| Code Lines | 156 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 13 | ||
| Bugs | 4 | Features | 2 |
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 |
||
| 115 | public function parse(InterfaceHandler $handle=null ) |
||
| 116 | { |
||
| 117 | if ($handle===null) { |
||
| 118 | |||
| 119 | if ($this->sourceHandle===null) { |
||
| 120 | throw new \InvalidArgumentException('Must provide a valid InterfaceHandler'); |
||
| 121 | } |
||
| 122 | else { |
||
| 123 | $handle = $this->sourceHandle; |
||
| 124 | } |
||
| 125 | } |
||
| 126 | |||
| 127 | $headers = array(); |
||
| 128 | $hash = array(); |
||
| 129 | $entry = array(); |
||
| 130 | $justNewEntry = false; // A new entry has been just inserted. |
||
| 131 | $firstLine = true; |
||
| 132 | $lastPreviousKey = null; // Used to remember last key in a multiline previous entry. |
||
| 133 | $state = null; |
||
| 134 | $lineNumber = 0; |
||
| 135 | |||
| 136 | while (!$handle->ended()) { |
||
| 137 | $line = trim($handle->getNextLine()); |
||
| 138 | $split = preg_split('/\s+/ ', $line, 2); |
||
| 139 | $key = $split[0]; |
||
| 140 | |||
| 141 | // If a blank line is found, or a new msgid when already got one |
||
| 142 | if ($line === '' || ($key=='msgid' && isset($entry['msgid']))) { |
||
| 143 | // Two consecutive blank lines |
||
| 144 | if ($justNewEntry) { |
||
| 145 | $lineNumber++; |
||
| 146 | continue; |
||
| 147 | } |
||
| 148 | |||
| 149 | if ($firstLine) { |
||
| 150 | $firstLine = false; |
||
| 151 | if (self::isHeader($entry)) { |
||
| 152 | array_shift($entry['msgstr']); |
||
| 153 | $headers = $entry['msgstr']; |
||
| 154 | } else { |
||
| 155 | $hash[] = $entry; |
||
| 156 | } |
||
| 157 | } else { |
||
| 158 | // A new entry is found! |
||
| 159 | $hash[] = $entry; |
||
| 160 | } |
||
| 161 | |||
| 162 | $entry = array(); |
||
| 163 | $state = null; |
||
| 164 | $justNewEntry = true; |
||
| 165 | $lastPreviousKey = null; |
||
| 166 | if ($line==='') { |
||
| 167 | $lineNumber++; |
||
| 168 | continue; |
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | $justNewEntry = false; |
||
| 173 | $data = isset($split[1]) ? $split[1] : null; |
||
| 174 | |||
| 175 | switch ($key) { |
||
| 176 | // Flagged translation |
||
| 177 | case '#,': |
||
| 178 | $entry['flags'] = preg_split('/,\s*/', $data); |
||
| 179 | break; |
||
| 180 | |||
| 181 | // # Translator comments |
||
| 182 | case '#': |
||
| 183 | $entry['tcomment'] = !isset($entry['tcomment']) ? array() : $entry['tcomment']; |
||
| 184 | $entry['tcomment'][] = $data; |
||
| 185 | break; |
||
| 186 | |||
| 187 | // #. Comments extracted from source code |
||
| 188 | case '#.': |
||
| 189 | $entry['ccomment'] = !isset($entry['ccomment']) ? array() : $entry['ccomment']; |
||
| 190 | $entry['ccomment'][] = $data; |
||
| 191 | break; |
||
| 192 | |||
| 193 | // Reference |
||
| 194 | case '#:': |
||
| 195 | $entry['reference'][] = addslashes($data); |
||
| 196 | break; |
||
| 197 | |||
| 198 | |||
| 199 | case '#|': // #| Previous untranslated string |
||
| 200 | case '#~': // #~ Old entry |
||
| 201 | case '#~|': // #~| Previous-Old untranslated string. Reported by @Cellard |
||
| 202 | |||
| 203 | switch ($key) { |
||
| 204 | case '#|': $key = 'previous'; |
||
| 205 | break; |
||
| 206 | case '#~': $key = 'obsolete'; |
||
| 207 | break; |
||
| 208 | case '#~|': $key = 'previous-obsolete'; |
||
| 209 | break; |
||
| 210 | } |
||
| 211 | |||
| 212 | $tmpParts = explode(' ', $data); |
||
| 213 | $tmpKey = $tmpParts[0]; |
||
| 214 | |||
| 215 | if (!in_array($tmpKey, array('msgid','msgid_plural','msgstr','msgctxt'))) { |
||
| 216 | $tmpKey = $lastPreviousKey; // If there is a multiline previous string we must remember what key was first line. |
||
| 217 | $str = $data; |
||
| 218 | } else { |
||
| 219 | $str = implode(' ', array_slice($tmpParts, 1)); |
||
| 220 | } |
||
| 221 | |||
| 222 | $entry[$key] = isset($entry[$key])? $entry[$key]:array('msgid'=>array(),'msgstr'=>array()); |
||
| 223 | |||
| 224 | if (strpos($key, 'obsolete')!==false) { |
||
| 225 | $entry['obsolete'] = true; |
||
| 226 | switch ($tmpKey) { |
||
| 227 | case 'msgid': |
||
| 228 | $entry['msgid'][] = $str; |
||
| 229 | $lastPreviousKey = $tmpKey; |
||
| 230 | break; |
||
| 231 | |||
| 232 | case 'msgstr': |
||
| 233 | if ($str == "\"\"") { |
||
| 234 | $entry['msgstr'][] = trim($str, '"'); |
||
| 235 | } else { |
||
| 236 | $entry['msgstr'][] = $str; |
||
| 237 | } |
||
| 238 | $lastPreviousKey = $tmpKey; |
||
| 239 | break; |
||
| 240 | |||
| 241 | default: |
||
| 242 | break; |
||
| 243 | } |
||
| 244 | } |
||
| 245 | |||
| 246 | if ($key!=='obsolete') { |
||
| 247 | switch ($tmpKey) { |
||
| 248 | case 'msgid': |
||
| 249 | case 'msgid_plural': |
||
| 250 | case 'msgstr': |
||
| 251 | $entry[$key][$tmpKey][] = $str; |
||
| 252 | $lastPreviousKey = $tmpKey; |
||
| 253 | break; |
||
| 254 | |||
| 255 | default: |
||
| 256 | $entry[$key][$tmpKey] = $str; |
||
| 257 | break; |
||
| 258 | } |
||
| 259 | } |
||
| 260 | break; |
||
| 261 | |||
| 262 | |||
| 263 | // context |
||
| 264 | // Allows disambiguations of different messages that have same msgid. |
||
| 265 | // Example: |
||
| 266 | // |
||
| 267 | // #: tools/observinglist.cpp:700 |
||
| 268 | // msgctxt "First letter in 'Scope'" |
||
| 269 | // msgid "S" |
||
| 270 | // msgstr "" |
||
| 271 | // |
||
| 272 | // #: skycomponents/horizoncomponent.cpp:429 |
||
| 273 | // msgctxt "South" |
||
| 274 | // msgid "S" |
||
| 275 | // msgstr "" |
||
| 276 | case 'msgctxt': |
||
| 277 | // untranslated-string |
||
| 278 | case 'msgid': |
||
| 279 | // untranslated-string-plural |
||
| 280 | case 'msgid_plural': |
||
| 281 | $state = $key; |
||
| 282 | $entry[$state][] = $data; |
||
| 283 | break; |
||
| 284 | // translated-string |
||
| 285 | case 'msgstr': |
||
| 286 | $state = 'msgstr'; |
||
| 287 | $entry[$state][] = $data; |
||
| 288 | break; |
||
| 289 | |||
| 290 | default: |
||
| 291 | if (strpos($key, 'msgstr[') !== false) { |
||
| 292 | // translated-string-case-n |
||
| 293 | $state = $key; |
||
| 294 | $entry[$state][] = $data; |
||
| 295 | } else { |
||
| 296 | // "multiline" lines |
||
| 297 | switch ($state) { |
||
| 298 | case 'msgctxt': |
||
| 299 | case 'msgid': |
||
| 300 | case 'msgid_plural': |
||
| 301 | case (strpos($state, 'msgstr[') !== false): |
||
| 302 | if (is_string($entry[$state])) { |
||
| 303 | // Convert it to array |
||
| 304 | $entry[$state] = array($entry[$state]); |
||
| 305 | } |
||
| 306 | $entry[$state][] = $line; |
||
| 307 | break; |
||
| 308 | |||
| 309 | case 'msgstr': |
||
| 310 | // Special fix where msgid is "" |
||
| 311 | if ($entry['msgid'] == "\"\"") { |
||
| 312 | $entry['msgstr'][] = trim($line, '"'); |
||
| 313 | } else { |
||
| 314 | $entry['msgstr'][] = $line; |
||
| 315 | } |
||
| 316 | break; |
||
| 317 | |||
| 318 | default: |
||
| 319 | throw new \Exception( |
||
| 320 | 'PoParser: Parse error! Unknown key "' . $key . '" on line ' . ($lineNumber+1) |
||
| 321 | ); |
||
| 322 | } |
||
| 323 | } |
||
| 324 | break; |
||
| 325 | } |
||
| 326 | |||
| 327 | $lineNumber++; |
||
| 328 | } |
||
| 329 | $handle->close(); |
||
| 330 | |||
| 331 | // add final entry |
||
| 332 | if ($state == 'msgstr') { |
||
| 333 | $hash[] = $entry; |
||
| 334 | } |
||
| 335 | |||
| 336 | // - Cleanup header data |
||
| 337 | $this->headers = array(); |
||
| 338 | foreach ($headers as $header) { |
||
| 339 | $header = $this->clean( $header ); |
||
| 340 | $this->headers[] = "\"" . preg_replace("/\\n/", '\n', $header) . "\""; |
||
| 341 | } |
||
| 342 | |||
| 343 | // - Cleanup data, |
||
| 344 | // - merge multiline entries |
||
| 345 | // - Reindex hash for ksort |
||
| 346 | $temp = $hash; |
||
| 347 | $this->entries = array(); |
||
| 348 | foreach ($temp as $entry) { |
||
| 349 | foreach ($entry as &$v) { |
||
| 350 | $or = $v; |
||
| 351 | $v = $this->clean($v); |
||
| 352 | if ($v === false) { |
||
| 353 | // parse error |
||
| 354 | throw new \Exception( |
||
| 355 | 'PoParser: Parse error! poparser::clean returned false on "' . htmlspecialchars($or) . '"' |
||
| 356 | ); |
||
| 357 | } |
||
| 358 | } |
||
| 359 | |||
| 360 | // check if msgid and a key starting with msgstr exists |
||
| 361 | if (isset($entry['msgid']) && count(preg_grep('/^msgstr/', array_keys($entry)))) { |
||
| 362 | $id = $this->getEntryId($entry); |
||
| 363 | $this->entries[$id] = $entry; |
||
| 364 | } |
||
| 365 | } |
||
| 366 | |||
| 367 | return $this->entries; |
||
| 368 | } |
||
| 369 | |||
| 794 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.