Passed
Push — master ( 073721...924750 )
by Robin
02:35
created

auto_trans_able()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 4
rs 10
c 1
b 0
f 0
1
<?php
2
3
use TopviewDigital\TranslationHelper\Service\Translation;
4
5
if (!function_exists('is_json')) {
6
    function is_json($string)
7
    {
8
        if (!is_string($string)) {
9
            return false;
10
        }
11
        json_decode($string);
12
13
        return json_last_error() == JSON_ERROR_NONE;
14
    }
15
}
16
17
if (!function_exists('auto_trans_able')) {
18
    function auto_trans_able()
19
    {
20
        return config('trans-helper.translation.mode') == 'auto'
21
            && config('queue.default') != 'sync';
22
    }
23
}
24
if (!function_exists('localize')) {
25
    function localize($languages, string $failback = '')
26
    {
27
        if (is_array($languages) || is_json($languages)) {
28
            $languages = (!is_array($languages)) ? (array)json_decode($languages) : $languages;
29
            $locales = array_keys($languages);
30
            $system = \App::getLocale();
31
            $default = config('app.locale');
32
33
            $locale = in_array($system, $locales) ? $system : (in_array($default, $locales) ? $default : null);
34
35
            return  $locale ? $languages[$locale] : $failback;
36
        }
37
        if (is_string($languages) && empty($failback)) {
38
            $tracer = (new Exception())->getTrace();
39
            $cite = [];
40
            $cite['file'] = preg_replace('/(\.php)(.+)$/', '${1}', $tracer[0]['file']);
41
            $cite['line'] = $tracer[0]['line'];
42
            array_shift($tracer);
43
            $cite['function'] = $tracer[0]['function'] ?? '';
44
            $cite['class'] = $tracer[0]['class'] ?? '';
45
            $vocab = [];
46
            $vocab['namespace'] = preg_replace(
47
                '/(^' . addcslashes(base_path(), '\/') . ')|(\.php$)/',
48
                '',
49
                $cite['file']
50
            );
51
            $vocab['term'] = $languages;
52
            $vocab = VocabTerm::firstOrCreate($vocab);
0 ignored issues
show
Bug introduced by
The type VocabTerm was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
53
            if (!$vocab->transaltion) {
54
                $vocab->translation = [config('app.locale') => $vocab['term']];
55
                $vocab->save();
56
                if (auto_trans_able()) {
57
                    dispatch(new Translation($vocab));
58
                }
59
            }
60
            if (auto_trans_able() && !in_array(app()->getLocale(), array_keys($vocab->translation))) {
0 ignored issues
show
introduced by
The method getLocale() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

60
            if (auto_trans_able() && !in_array(app()->/** @scrutinizer ignore-call */ getLocale(), array_keys($vocab->translation))) {
Loading history...
61
                dispatch(new Translation($vocab, [app()->getLocale()]));
62
            }
63
            $cite['file'] = preg_replace(
64
                '/^' . addcslashes(base_path(), '\/') . '/',
65
                '',
66
                $cite['file']
67
            );
68
            $cite = VocabCite::firstOrCreate($cite);
0 ignored issues
show
Bug introduced by
The type VocabCite was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
69
            $vocab->cites()->sync([$cite->id], false);
70
            if (!$cite->code) {
71
                $lines = explode("\n", file_get_contents(base_path() . $cite->file));
72
                $cite->code = $lines[$cite->line - 1];
73
                if (substr($cite->file, -10) != '.blade.php') {
74
                    for ($start = $cite->line - 2; $start > -1; $start--) {
75
                        $char = substr(rtrim($lines[$start]), -1);
76
                        if ($char == ';' || $char == '}' || $char == '{') {
77
                            $start++;
78
                            break;
79
                        }
80
                    }
81
                    $count = count($lines);
82
                    for ($end = $cite->line - 1; $end < $count; $end++) {
83
                        $char = substr(rtrim($lines[$end]), -1);
84
                        if ($char == ';') {
85
                            break;
86
                        }
87
                    }
88
                    $code = array_filter(array_slice($lines, $start, $end - $start + 1, true), 'trim');
89
                    $max = strlen($end);
90
                    $cite->code = implode("\n", array_map(function ($u, $v) use ($max) {
91
                        return sprintf("%{$max}d    %s", $u + 1, rtrim($v));
92
                    }, array_keys($code), $code));
93
                    $cite->save();
94
                }
95
            }
96
97
            return localize($vocab->translation, $vocab->term);
98
        }
99
100
        return $failback;
101
    }
102
}
103
104
if (!function_exists('sweep')) {
105
    function sweep()
106
    {
107
        array_map(
108
            function ($u) {
109
                $u->sweep();
110
            },
111
            array_merge(
112
                VocabCite::get()->all(),
113
                VocabTerm::get()->all()
114
            )
115
        );
116
    }
117
}
118
119
if (!function_exists('translate')) {
120
    function translate($locales = [])
121
    {
122
        dispatch(new Translation(null, $locales));
123
    }
124
}
125
if (!function_exists('slugify')) {
126
    function slugify($text)
127
    {
128
        // replace non letter or digits by -
129
        $text = preg_replace('~[^\pL\d]+~u', '-', $text);
130
131
        // transliterate
132
        $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
133
134
        // remove unwanted characters
135
        $text = preg_replace('~[^-\w]+~', '', $text);
136
137
        // trim
138
        $text = trim($text, '-');
139
140
        // remove duplicated - symbols
141
        $text = preg_replace('~-+~', '-', $text);
142
143
        // lowercase
144
        $text = strtolower($text);
145
146
        if (empty($text)) {
147
            return 'n-a';
148
        }
149
150
        return $text;
151
    }
152
}
153
154
if (!function_exists('export')) {
155
    function export($path = null)
156
    {
157
        $path = $path ?? config('trans-helper.export.path');
158
        $locales = VocabTerm::locales();
159
        $namespaces = VocabTerm::namespaces();
160
        foreach ($namespaces as $namespace) {
161
            foreach ($locales as $locale) {
162
                $langue_file=str_replace('/', '.', strtolower(ltrim($namespace,'/')));
163
                $langue_file = str_replace('//', '/', implode('/',[$path,$locale,$langue_file]));
164
                $lines=['<?php','','return ['];
165
                foreach (VocabTerm::where('namespace', $namespace)->get() as $term) {
166
                        $lines[]=sprintf("    '%s'=>'%s',",
167
                        $term->slug,
168
                        localize($term->translation,$term->translation[config('app.locale')]));
169
                }
170
                $lines[]='];';
171
                file_put_contents($langue_file,implode("\n",$lines));
172
            }
173
        }
174
    }
175
}
176
177
 
178
 
179
 
180
 
181
 
182
 
183
 
184
 
185