| Conditions | 22 |
| Paths | 348 |
| Total Lines | 119 |
| Code Lines | 73 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 25 | public function formatFileContents(string $lang, string $fileName) : string |
||
| 26 | { |
||
| 27 | $enLines = $this->loadLangFileLines('en', $fileName); |
||
| 28 | $langContent = $this->loadLang($lang, $fileName); |
||
| 29 | $enContent = $this->loadLang('en', $fileName); |
||
| 30 | |||
| 31 | // Calculate the longest top-level key length |
||
| 32 | $longestKeyLength = $this->calculateKeyPadding($enContent); |
||
| 33 | |||
| 34 | // Start formatted content |
||
| 35 | $formatted = []; |
||
| 36 | $mode = 'header'; |
||
| 37 | $skipArray = false; |
||
| 38 | $arrayKeys = []; |
||
| 39 | |||
| 40 | foreach ($enLines as $index => $line) { |
||
| 41 | $trimLine = trim($line); |
||
| 42 | if ($mode === 'header') { |
||
| 43 | $formatted[$index] = $line; |
||
| 44 | if (str_replace(' ', '', $trimLine) === 'return[') { |
||
| 45 | $mode = 'body'; |
||
| 46 | } |
||
| 47 | } |
||
| 48 | |||
| 49 | if ($mode === 'body') { |
||
| 50 | $matches = []; |
||
| 51 | $arrayEndMatch = preg_match('/]\s*,\s*$/', $trimLine); |
||
| 52 | |||
| 53 | if ($skipArray) { |
||
| 54 | if ($arrayEndMatch) { |
||
| 55 | $skipArray = false; |
||
| 56 | } |
||
| 57 | continue; |
||
| 58 | } |
||
| 59 | |||
| 60 | // Comment to ignore |
||
| 61 | if (strpos($trimLine, '//!') === 0) { |
||
| 62 | $formatted[$index] = ""; |
||
| 63 | continue; |
||
| 64 | } |
||
| 65 | |||
| 66 | // Comment |
||
| 67 | if (strpos($trimLine, '//') === 0) { |
||
| 68 | $formatted[$index] = "\t" . $trimLine; |
||
| 69 | continue; |
||
| 70 | } |
||
| 71 | |||
| 72 | // Arrays |
||
| 73 | $arrayStartMatch = preg_match('/^\'(.*)\'\s+?=>\s+?\[(\],)?\s*?$/', $trimLine, $matches); |
||
| 74 | |||
| 75 | $indent = count($arrayKeys) + 1; |
||
| 76 | if ($arrayStartMatch === 1) { |
||
| 77 | if ($fileName === 'settings' && $matches[1] === 'language_select') { |
||
| 78 | $skipArray = true; |
||
| 79 | continue; |
||
| 80 | } |
||
| 81 | $arrayKeys[] = $matches[1]; |
||
| 82 | $formatted[$index] = str_repeat(" ", $indent * 4) . str_pad("'{$matches[1]}'", $longestKeyLength) . "=> ["; |
||
| 83 | if ($arrayEndMatch !== 1) { |
||
| 84 | continue; |
||
| 85 | } |
||
| 86 | } |
||
| 87 | if ($arrayEndMatch === 1) { |
||
| 88 | $this->unsetArrayByKeys($langContent, $arrayKeys); |
||
| 89 | array_pop($arrayKeys); |
||
| 90 | if (isset($formatted[$index])) { |
||
| 91 | $formatted[$index] .= '],'; |
||
| 92 | } else { |
||
| 93 | $formatted[$index] = str_repeat(" ", ($indent-1) * 4) . "],"; |
||
| 94 | } |
||
| 95 | continue; |
||
| 96 | } |
||
| 97 | |||
| 98 | // Translation |
||
| 99 | $translationMatch = preg_match('/^\'(.*)\'\s+?=>\s+?[\'"](.*)?[\'"].+?$/', $trimLine, $matches); |
||
| 100 | if ($translationMatch === 1) { |
||
| 101 | $key = $matches[1]; |
||
| 102 | $keys = array_merge($arrayKeys, [$key]); |
||
| 103 | $langVal = $this->getTranslationByKeys($langContent, $keys); |
||
| 104 | if (empty($langVal)) { |
||
| 105 | continue; |
||
| 106 | } |
||
| 107 | |||
| 108 | $keyPad = $longestKeyLength; |
||
| 109 | if (count($arrayKeys) === 0) { |
||
| 110 | unset($langContent[$key]); |
||
| 111 | } else { |
||
| 112 | $keyPad = $this->calculateKeyPadding($this->getTranslationByKeys($enContent, $arrayKeys)); |
||
|
|
|||
| 113 | } |
||
| 114 | |||
| 115 | $formatted[$index] = $this->formatTranslationLine($key, $langVal, $indent, $keyPad); |
||
| 116 | continue; |
||
| 117 | } |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | // Fill missing lines |
||
| 122 | $arraySize = max(array_keys($formatted)); |
||
| 123 | $formatted = array_replace(array_fill(0, $arraySize, ''), $formatted); |
||
| 124 | |||
| 125 | // Add remaining translations |
||
| 126 | $langContent = array_filter($langContent, function ($item) { |
||
| 127 | return !is_null($item) && !empty($item); |
||
| 128 | }); |
||
| 129 | if (count($langContent) > 0) { |
||
| 130 | $formatted[] = ''; |
||
| 131 | $formatted[] = "\t// Unmatched"; |
||
| 132 | } |
||
| 133 | foreach ($langContent as $key => $value) { |
||
| 134 | if (is_array($value)) { |
||
| 135 | $formatted[] = $this->formatTranslationArray($key, $value); |
||
| 136 | } else { |
||
| 137 | $formatted[] = $this->formatTranslationLine($key, $value); |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | // Add end line |
||
| 142 | $formatted[] = '];'; |
||
| 143 | return implode("\n", $formatted); |
||
| 144 | } |
||
| 350 | } |