| Conditions | 26 |
| Paths | 342 |
| Total Lines | 128 |
| 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 |
||
| 15 | public function loadString(string $string, Translations $translations = null): Translations |
||
| 16 | { |
||
| 17 | $translations = parent::loadString($string, $translations); |
||
| 18 | |||
| 19 | $lines = explode("\n", $string); |
||
| 20 | $line = current($lines); |
||
| 21 | $translation = $this->createTranslation(null, ''); |
||
| 22 | |||
| 23 | while ($line !== false) { |
||
| 24 | $line = trim($line); |
||
| 25 | $nextLine = next($lines); |
||
| 26 | |||
| 27 | //Multiline |
||
| 28 | while ( |
||
| 29 | substr($line, -1, 1) === '"' |
||
| 30 | && $nextLine !== false |
||
| 31 | && substr(trim($nextLine), 0, 1) === '"' |
||
| 32 | ) { |
||
| 33 | $line = substr($line, 0, -1).substr(trim($nextLine), 1); |
||
| 34 | $nextLine = next($lines); |
||
| 35 | } |
||
| 36 | |||
| 37 | //End of translation |
||
| 38 | if ($line === '') { |
||
| 39 | if (!self::isEmpty($translation)) { |
||
| 40 | $translations->add($translation); |
||
| 41 | } |
||
| 42 | |||
| 43 | $translation = $this->createTranslation(null, ''); |
||
| 44 | $line = $nextLine; |
||
| 45 | continue; |
||
| 46 | } |
||
| 47 | |||
| 48 | $splitLine = preg_split('/\s+/', $line, 2); |
||
| 49 | $key = $splitLine[0]; |
||
| 50 | $data = $splitLine[1] ?? ''; |
||
| 51 | |||
| 52 | if ($key === '#~') { |
||
| 53 | $translation->setDisabled(true); |
||
|
|
|||
| 54 | |||
| 55 | $splitLine = preg_split('/\s+/', $data, 2); |
||
| 56 | $key = $splitLine[0]; |
||
| 57 | $data = $splitLine[1] ?? ''; |
||
| 58 | } |
||
| 59 | |||
| 60 | if ($data === '') { |
||
| 61 | $line = $nextLine; |
||
| 62 | continue; |
||
| 63 | } |
||
| 64 | |||
| 65 | switch ($key) { |
||
| 66 | case '#': |
||
| 67 | $translation->getComments()->add($data); |
||
| 68 | break; |
||
| 69 | |||
| 70 | case '#.': |
||
| 71 | $translation->getExtractedComments()->add($data); |
||
| 72 | break; |
||
| 73 | |||
| 74 | case '#,': |
||
| 75 | foreach (array_map('trim', explode(',', trim($data))) as $value) { |
||
| 76 | $translation->getFlags()->add($value); |
||
| 77 | } |
||
| 78 | break; |
||
| 79 | |||
| 80 | case '#:': |
||
| 81 | foreach (preg_split('/\s+/', trim($data)) as $value) { |
||
| 82 | if (preg_match('/^(.+)(:(\d*))?$/U', $value, $matches)) { |
||
| 83 | $translation->getReferences()->add($matches[1], $matches[3] ?? null); |
||
| 84 | } |
||
| 85 | } |
||
| 86 | break; |
||
| 87 | |||
| 88 | case 'msgctxt': |
||
| 89 | $translation = $translation->withContext(static::decode($data)); |
||
| 90 | break; |
||
| 91 | |||
| 92 | case 'msgid': |
||
| 93 | $translation = $translation->withOriginal(static::decode($data)); |
||
| 94 | break; |
||
| 95 | |||
| 96 | case 'msgid_plural': |
||
| 97 | $translation->setPlural(static::decode($data)); |
||
| 98 | break; |
||
| 99 | |||
| 100 | case 'msgstr': |
||
| 101 | case 'msgstr[0]': |
||
| 102 | $translation->translate(static::decode($data)); |
||
| 103 | break; |
||
| 104 | |||
| 105 | case 'msgstr[1]': |
||
| 106 | $translation->translatePlural(static::decode($data)); |
||
| 107 | break; |
||
| 108 | |||
| 109 | default: |
||
| 110 | if (strpos($key, 'msgstr[') === 0) { |
||
| 111 | $p = $translation->getPluralTranslations(); |
||
| 112 | $p[] = static::decode($data); |
||
| 113 | |||
| 114 | $translation->translatePlural(...$p); |
||
| 115 | break; |
||
| 116 | } |
||
| 117 | break; |
||
| 118 | } |
||
| 119 | |||
| 120 | $line = $nextLine; |
||
| 121 | } |
||
| 122 | |||
| 123 | if (!self::isEmpty($translation)) { |
||
| 124 | $translations->add($translation); |
||
| 125 | } |
||
| 126 | |||
| 127 | //Headers |
||
| 128 | $translation = $translations->find(null, ''); |
||
| 129 | |||
| 130 | if (!$translation) { |
||
| 131 | return $translations; |
||
| 132 | } |
||
| 133 | |||
| 134 | $translations->remove($translation); |
||
| 135 | $headers = $translations->getHeaders(); |
||
| 136 | |||
| 137 | foreach (static::parseHeaders($translation->getTranslation()) as $name => $value) { |
||
| 138 | $headers->set($name, $value); |
||
| 139 | } |
||
| 140 | |||
| 141 | return $translations; |
||
| 142 | } |
||
| 143 | |||
| 186 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.