| Conditions | 12 |
| Paths | 54 |
| Total Lines | 76 |
| Code Lines | 41 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 1 | 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 |
||
| 65 | public function prepare(Application $app, $locale) |
||
| 66 | { |
||
| 67 | $templatesPath = APP_DIR.'/templates'; |
||
| 68 | $untranslatedMessagesFile = APP_DIR.'/locales/'.$locale.'/messages_untranslated.yml'; |
||
| 69 | |||
| 70 | $extractor = new TwigExtractor($app['twig']); |
||
| 71 | |||
| 72 | /***** All translations *****/ |
||
| 73 | $catalogueAll = new MessageCatalogue($locale); |
||
| 74 | $extractor->extract($templatesPath, $catalogueAll); |
||
| 75 | $allMessages = $catalogueAll->all('messages'); |
||
| 76 | |||
| 77 | // String from controller, controller provider, etc. |
||
| 78 | $finder = new Finder(); |
||
| 79 | $finder->files()->in(ROOT_DIR.'/src'); |
||
| 80 | |||
| 81 | foreach ($finder as $file) { |
||
| 82 | $fileMessageStrings = array(); |
||
| 83 | |||
| 84 | $filePath = $file->getRealpath(); |
||
| 85 | $fileContent = file_get_contents($filePath); |
||
| 86 | |||
| 87 | $pregMatch = "#->trans.*\(\s*'(.+?)(?=')#m"; |
||
| 88 | preg_match_all($pregMatch, $fileContent, $matches); |
||
| 89 | |||
| 90 | $matches = $matches[1]; |
||
| 91 | |||
| 92 | if ($matches) { |
||
| 93 | foreach ($matches as $match) { |
||
| 94 | $fileMessageStrings[] = $match; |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | if (!empty($fileMessageStrings)) { |
||
| 99 | foreach ($fileMessageStrings as $fileMessageString) { |
||
| 100 | if (!isset($allMessages[$fileMessageString])) { |
||
| 101 | $allMessages[$fileMessageString] = $fileMessageString; |
||
| 102 | } |
||
| 103 | } |
||
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | /***** Already translated *****/ |
||
| 108 | $app['application.translator']->setLocale($locale); |
||
| 109 | $translatedMessages = $app['translator']->getCatalogue($locale); |
||
| 110 | $translatedMessages = $translatedMessages->all('messages'); |
||
| 111 | |||
| 112 | /***** Untranslated *****/ |
||
| 113 | $untranslatedMessages = array(); |
||
| 114 | |||
| 115 | if (!empty($allMessages)) { |
||
| 116 | foreach ($allMessages as $singleMessageKey => $singleMessage) { |
||
| 117 | if (!isset($translatedMessages[$singleMessageKey])) { |
||
| 118 | $untranslatedMessages[$singleMessageKey] = $singleMessage; |
||
| 119 | } |
||
| 120 | } |
||
| 121 | } |
||
| 122 | |||
| 123 | if (!empty($untranslatedMessages)) { |
||
| 124 | $dumper = new Dumper(); |
||
| 125 | |||
| 126 | $yaml = $dumper->dump($untranslatedMessages, 1); |
||
| 127 | |||
| 128 | if (file_exists($untranslatedMessagesFile)) { |
||
| 129 | unlink($untranslatedMessagesFile); |
||
| 130 | } |
||
| 131 | |||
| 132 | file_put_contents($untranslatedMessagesFile, $yaml); |
||
| 133 | } |
||
| 134 | |||
| 135 | return array( |
||
| 136 | 'allMessages' => $allMessages, |
||
| 137 | 'translatedMessages' => $translatedMessages, |
||
| 138 | 'untranslatedMessages' => $untranslatedMessages, |
||
| 139 | ); |
||
| 140 | } |
||
| 141 | } |
||
| 142 |