| Conditions | 32 |
| Paths | 702 |
| Total Lines | 158 |
| Lines | 14 |
| Ratio | 8.86 % |
| 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 | $i = 0; |
||
| 21 | $append = null; |
||
| 22 | |||
| 23 | $translation = $this->createTranslation(null, ''); |
||
| 24 | |||
| 25 | for ($n = count($lines); $i < $n; ++$i) { |
||
| 26 | $line = trim($lines[$i]); |
||
| 27 | $line = self::fixMultiLines($line, $lines, $i); |
||
| 28 | |||
| 29 | if ($line === '') { |
||
| 30 | if (!self::isEmpty($translation)) { |
||
| 31 | $translations->add($translation); |
||
| 32 | } |
||
| 33 | |||
| 34 | $translation = $this->createTranslation(null, ''); |
||
| 35 | continue; |
||
| 36 | } |
||
| 37 | |||
| 38 | $splitLine = preg_split('/\s+/', $line, 2); |
||
| 39 | $key = $splitLine[0]; |
||
| 40 | $data = isset($splitLine[1]) ? $splitLine[1] : ''; |
||
| 41 | |||
| 42 | if ($key === '#~') { |
||
| 43 | $translation->setDisabled(true); |
||
|
|
|||
| 44 | |||
| 45 | $splitLine = preg_split('/\s+/', $data, 2); |
||
| 46 | $key = $splitLine[0]; |
||
| 47 | $data = isset($splitLine[1]) ? $splitLine[1] : ''; |
||
| 48 | } |
||
| 49 | |||
| 50 | if ($data === '') { |
||
| 51 | continue; |
||
| 52 | } |
||
| 53 | |||
| 54 | switch ($key) { |
||
| 55 | case '#': |
||
| 56 | $translation->getComments()->add($data); |
||
| 57 | $append = null; |
||
| 58 | break; |
||
| 59 | |||
| 60 | case '#.': |
||
| 61 | $translation->getExtractedComments()->add($data); |
||
| 62 | $append = null; |
||
| 63 | break; |
||
| 64 | |||
| 65 | case '#,': |
||
| 66 | foreach (array_map('trim', explode(',', trim($data))) as $value) { |
||
| 67 | $translation->getFlags()->add($value); |
||
| 68 | } |
||
| 69 | $append = null; |
||
| 70 | break; |
||
| 71 | |||
| 72 | case '#:': |
||
| 73 | foreach (preg_split('/\s+/', trim($data)) as $value) { |
||
| 74 | if (preg_match('/^(.+)(:(\d*))?$/U', $value, $matches)) { |
||
| 75 | $translation->getReferences()->add($matches[1], isset($matches[3]) ? $matches[3] : null); |
||
| 76 | } |
||
| 77 | } |
||
| 78 | $append = null; |
||
| 79 | break; |
||
| 80 | |||
| 81 | case 'msgctxt': |
||
| 82 | $translation = $translation->withContext(static::decode($data)); |
||
| 83 | $append = 'Context'; |
||
| 84 | break; |
||
| 85 | |||
| 86 | case 'msgid': |
||
| 87 | $translation = $translation->withOriginal(static::decode($data)); |
||
| 88 | $append = 'Original'; |
||
| 89 | break; |
||
| 90 | |||
| 91 | case 'msgid_plural': |
||
| 92 | $translation->setPlural(static::decode($data)); |
||
| 93 | $append = 'Plural'; |
||
| 94 | break; |
||
| 95 | |||
| 96 | case 'msgstr': |
||
| 97 | case 'msgstr[0]': |
||
| 98 | $translation->translate(static::decode($data)); |
||
| 99 | $append = 'Translation'; |
||
| 100 | break; |
||
| 101 | |||
| 102 | case 'msgstr[1]': |
||
| 103 | $translation->translatePlural(static::decode($data)); |
||
| 104 | $append = 'PluralTranslation'; |
||
| 105 | break; |
||
| 106 | |||
| 107 | default: |
||
| 108 | View Code Duplication | if (strpos($key, 'msgstr[') === 0) { |
|
| 109 | $p = $translation->getPluralTranslations(); |
||
| 110 | $p[] = static::decode($data); |
||
| 111 | |||
| 112 | $translation->translatePlural(...$p); |
||
| 113 | $append = 'PluralTranslation'; |
||
| 114 | break; |
||
| 115 | } |
||
| 116 | |||
| 117 | if (isset($append)) { |
||
| 118 | if ($append === 'Context') { |
||
| 119 | $context = $translation->getContext()."\n".static::decode($data); |
||
| 120 | $translation = $translation->withContext($context); |
||
| 121 | break; |
||
| 122 | } |
||
| 123 | |||
| 124 | if ($append === 'Original') { |
||
| 125 | $original = $translation->getOriginal()."\n".static::decode($data); |
||
| 126 | $translation = $translation->withOriginal($original); |
||
| 127 | break; |
||
| 128 | } |
||
| 129 | |||
| 130 | if ($append === 'Plural') { |
||
| 131 | $plural = $translation->getPlural()."\n".static::decode($data); |
||
| 132 | $translation->setPlural($plural); |
||
| 133 | break; |
||
| 134 | } |
||
| 135 | |||
| 136 | if ($append === 'Translation') { |
||
| 137 | $text = $translation->getTranslation()."\n".static::decode($data); |
||
| 138 | $translation->translate($text); |
||
| 139 | break; |
||
| 140 | } |
||
| 141 | |||
| 142 | View Code Duplication | if ($append === 'PluralTranslation') { |
|
| 143 | $p = $translation->getPluralTranslations(); |
||
| 144 | $p[] = array_pop($p)."\n".static::decode($data); |
||
| 145 | $translation->translatePlural(...$p); |
||
| 146 | break; |
||
| 147 | } |
||
| 148 | } |
||
| 149 | break; |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | if (!self::isEmpty($translation)) { |
||
| 154 | $translations->add($translation); |
||
| 155 | } |
||
| 156 | |||
| 157 | //Headers |
||
| 158 | $translation = $translations->find(null, ''); |
||
| 159 | |||
| 160 | if (!$translation) { |
||
| 161 | return $translations; |
||
| 162 | } |
||
| 163 | |||
| 164 | $translations->remove($translation); |
||
| 165 | $headers = $translations->getHeaders(); |
||
| 166 | |||
| 167 | foreach (static::parseHeaders($translation->getTranslation()) as $name => $value) { |
||
| 168 | $headers->set($name, $value); |
||
| 169 | } |
||
| 170 | |||
| 171 | return $translations; |
||
| 172 | } |
||
| 173 | |||
| 236 |
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.