| Conditions | 48 |
| Paths | 15874 |
| Total Lines | 239 |
| Code Lines | 144 |
| 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(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 | $modifier = ''; |
||
|
|
|||
| 173 | switch ($key) { |
||
| 174 | case '#|': |
||
| 175 | $key = 'previous'; |
||
| 176 | break; |
||
| 177 | case '#~': |
||
| 178 | $key = 'obsolete'; |
||
| 179 | break; |
||
| 180 | case '#~|': |
||
| 181 | $key = 'previous-obsolete'; |
||
| 182 | break; |
||
| 183 | } |
||
| 184 | |||
| 185 | $tmpParts = explode(' ', $data); |
||
| 186 | $tmpKey = $tmpParts[0]; |
||
| 187 | |||
| 188 | if (!in_array($tmpKey, array('msgid', 'msgid_plural', 'msgstr', 'msgctxt'), true)) { |
||
| 189 | // If there is a multi-line previous string we must remember |
||
| 190 | // what key was first line. |
||
| 191 | $tmpKey = $lastPreviousKey; |
||
| 192 | $str = $data; |
||
| 193 | } else { |
||
| 194 | $str = implode(' ', array_slice($tmpParts, 1)); |
||
| 195 | } |
||
| 196 | |||
| 197 | //array('obsolete' => true, 'msgid' => '', 'msgstr' => ''); |
||
| 198 | |||
| 199 | if (strpos($key, 'obsolete') !== false) { |
||
| 200 | $entry['obsolete'] = true; |
||
| 201 | switch ($tmpKey) { |
||
| 202 | case 'msgid': |
||
| 203 | if (!isset($entry['msgid'])) { |
||
| 204 | $entry['msgid'] = ''; |
||
| 205 | } |
||
| 206 | $entry['msgid'].= trim($str, '"'); |
||
| 207 | $lastPreviousKey = $tmpKey; |
||
| 208 | break; |
||
| 209 | |||
| 210 | case 'msgstr': |
||
| 211 | if (!isset($entry['msgstr'])) { |
||
| 212 | $entry['msgstr'] = ''; |
||
| 213 | } |
||
| 214 | $entry['msgstr'].= trim($str, '"'); |
||
| 215 | $lastPreviousKey = $tmpKey; |
||
| 216 | break; |
||
| 217 | |||
| 218 | case 'msgctxt': |
||
| 219 | $entry['msgctxt'] = trim($str, '"'); |
||
| 220 | $lastPreviousKey = $tmpKey; |
||
| 221 | break; |
||
| 222 | |||
| 223 | default: |
||
| 224 | break; |
||
| 225 | } |
||
| 226 | } else { |
||
| 227 | $entry[$key] = isset($entry[$key]) ? $entry[$key] : array('msgid' => '', 'msgstr' => ''); |
||
| 228 | } |
||
| 229 | |||
| 230 | if ($key !== 'obsolete') { |
||
| 231 | switch ($tmpKey) { |
||
| 232 | case 'msgid': |
||
| 233 | case 'msgid_plural': |
||
| 234 | case 'msgstr': |
||
| 235 | $entry[$key][$tmpKey] = trim($str, '"'); |
||
| 236 | $lastPreviousKey = $tmpKey; |
||
| 237 | break; |
||
| 238 | |||
| 239 | default: |
||
| 240 | $entry[$key][$tmpKey] = $str; |
||
| 241 | break; |
||
| 242 | } |
||
| 243 | } |
||
| 244 | break; |
||
| 245 | |||
| 246 | |||
| 247 | // context |
||
| 248 | // Allows disambiguations of different messages that have same msgid. |
||
| 249 | // Example: |
||
| 250 | // |
||
| 251 | // #: tools/observinglist.cpp:700 |
||
| 252 | // msgctxt "First letter in 'Scope'" |
||
| 253 | // msgid "S" |
||
| 254 | // msgstr "" |
||
| 255 | // |
||
| 256 | // #: skycomponents/horizoncomponent.cpp:429 |
||
| 257 | // msgctxt "South" |
||
| 258 | // msgid "S" |
||
| 259 | // msgstr "" |
||
| 260 | case 'msgctxt': |
||
| 261 | // untranslated-string |
||
| 262 | case 'msgid': |
||
| 263 | // untranslated-string-plural |
||
| 264 | case 'msgid_plural': |
||
| 265 | $state = $key; |
||
| 266 | if (!isset($entry[$state])) { |
||
| 267 | $entry[$state] = ''; |
||
| 268 | } |
||
| 269 | |||
| 270 | $entry[$state] .= trim($data, '"'); |
||
| 271 | break; |
||
| 272 | // translated-string |
||
| 273 | case 'msgstr': |
||
| 274 | $state = 'msgstr'; |
||
| 275 | $entry[$state] = trim($data, '"'); |
||
| 276 | break; |
||
| 277 | |||
| 278 | default: |
||
| 279 | if (strpos($key, 'msgstr[') !== false) { |
||
| 280 | // translated-string-case-n |
||
| 281 | $state = $key; |
||
| 282 | $entry[$state] = trim($data, '"'); |
||
| 283 | } else { |
||
| 284 | // "multiline" lines |
||
| 285 | switch ($state) { |
||
| 286 | case 'msgctxt': |
||
| 287 | case 'msgid': |
||
| 288 | case 'msgid_plural': |
||
| 289 | case (strpos($state, 'msgstr[') !== false): |
||
| 290 | if (!isset($entry[$state]) === false) { |
||
| 291 | $entry[$state] = ''; |
||
| 292 | } |
||
| 293 | |||
| 294 | if (is_string($entry[$state])) { |
||
| 295 | // Convert it to array |
||
| 296 | //$entry[$state] = array($entry[$state]); |
||
| 297 | $entry[$state] = trim($entry[$state], '"'); |
||
| 298 | } |
||
| 299 | $entry[$state] .= trim($line, '"'); |
||
| 300 | break; |
||
| 301 | |||
| 302 | case 'msgstr': |
||
| 303 | // Special fix where msgid is "" |
||
| 304 | $entry['msgstr'] .= trim($line, '"'); |
||
| 305 | /*if ($entry['msgid'] === "\"\"") { |
||
| 306 | $entry['msgstr'].= trim($line, '"'); |
||
| 307 | } else { |
||
| 308 | $entry['msgstr'].= $line; |
||
| 309 | }*/ |
||
| 310 | break; |
||
| 311 | |||
| 312 | default: |
||
| 313 | throw new \Exception( |
||
| 314 | 'Parser: Parse error! Unknown key "'.$key.'" on line '.($lineNumber + 1) |
||
| 315 | ); |
||
| 316 | } |
||
| 317 | } |
||
| 318 | break; |
||
| 319 | } |
||
| 320 | |||
| 321 | $lineNumber++; |
||
| 322 | } |
||
| 323 | $this->sourceHandler->close(); |
||
| 324 | |||
| 325 | // add final entry |
||
| 326 | if ($state === 'msgstr') { |
||
| 327 | $catalog->addEntry(EntryFactory::createFromArray($entry)); |
||
| 328 | } |
||
| 329 | |||
| 330 | return $catalog; |
||
| 331 | } |
||
| 332 | |||
| 379 |
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.