GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — develop ( 16e366...ebde4e )
by Borut
02:50
created

Translator::prepare()   C

Complexity

Conditions 12
Paths 54

Size

Total Lines 76
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 2 Features 0
Metric Value
c 5
b 2
f 0
dl 0
loc 76
rs 5.2846
cc 12
eloc 41
nc 54
nop 2

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
namespace Application;
4
5
use Symfony\Bridge\Twig\Translation\TwigExtractor;
6
use Symfony\Component\Translation\MessageCatalogue;
7
use Symfony\Component\Finder\Finder;
8
use Symfony\Component\Yaml\Dumper;
9
use Silex\Application;
10
11
/**
12
 * @author Borut Balažek <[email protected]>
13
 */
14
class Translator
15
{
16
    protected $app;
17
18
    /**
19
     * @param Application $app
20
     */
21
    public function __construct(Application $app)
22
    {
23
        $this->app = $app;
24
    }
25
26
    /**
27
     * Sets the locale (if it can).
28
     */
29
    public function setLocale($locale)
30
    {
31
        $app = $this->app;
32
33
        $app['translator']->setLocale($locale);
34
35
        $localeMessagesFile = APP_DIR.'/locales/'.$app['locale'].'/messages.yml';
36
        if (file_exists($localeMessagesFile)) {
37
            $app['translator']->addResource(
38
                'yaml',
39
                $localeMessagesFile,
40
                $app['locale']
41
            );
42
        }
43
44
        $localeValidatorsFile = APP_DIR.'/locales/'.$app['locale'].'/validators.yml';
45
        if (file_exists($localeValidatorsFile)) {
46
            $app['translator']->addResource(
47
                'yaml',
48
                $localeValidatorsFile,
49
                $app['locale'],
50
                'validators'
51
            );
52
        }
53
    }
54
55
    /**
56
     * Prepares and finds all the translated and untranslated string in tempates and controllers.
57
     */
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