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