Passed
Push — master ( 848351...70e8d0 )
by
unknown
07:56 queued 01:57
created

Loader::loadAddonOverrides()   C

Complexity

Conditions 13
Paths 37

Size

Total Lines 41
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 41
rs 6.6166
cc 13
nc 37
nop 4

How to fix   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 namespace App\Lang;
2
3
use Anomaly\Streams\Platform\Addon\Addon;
4
use Anomaly\Streams\Platform\Addon\AddonCollection;
5
use Anomaly\Streams\Platform\Application\Application;
6
use Illuminate\Filesystem\Filesystem;
7
use Illuminate\Translation\FileLoader;
8
9
/**
10
 * Class Loader
11
 *
12
 * @link   http://pyrocms.com/
13
 * @author PyroCMS, Inc. <[email protected]>
14
 * @author Ryan Thompson <[email protected]>
15
 */
16
class Loader extends FileLoader
17
{
18
19
    /**
20
     * The runtime cache.
21
     *
22
     * @var array
23
     */
24
    protected static $disabled = [];
25
26
    /**
27
     * The streams path.
28
     *
29
     * @var string
30
     */
31
    protected $streams;
32
33
    /**
34
     * The addon collection instance.
35
     *
36
     * @var AddonCollection
37
     */
38
    protected $addons;
39
40
    /**
41
     * The application instance.
42
     *
43
     * @var Application
44
     */
45
    protected $application;
46
47
    /**
48
     * Create a new Loader instance.
49
     *
50
     * @param Filesystem $files
51
     * @param string $path
52
     */
53
    public function __construct(Filesystem $files, $path)
54
    {
55
        $this->streams = base_path('vendor/visiosoft/streams-platform/resources/lang');
56
57
        $this->application = app(Application::class);
58
        $this->addons = app(AddonCollection::class);
59
60
        parent::__construct($files, $path);
61
    }
62
63
    /**
64
     * Load a locale from a given path.
65
     *
66
     * Keep streams overrides in place
67
     * that are NOT namespaced cause
68
     * we're overriding Laravel's
69
     * base language files too.
70
     *
71
     * @param string $path
72
     * @param string $locale
73
     * @param string $group
74
     * @return array
75
     */
76
    protected function loadPath($path, $locale, $group)
77
    {
78
        $lines = parent::loadPath($path, $locale, $group);
79
80
        if ($path == $this->streams && $lines) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $lines of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
81
            $lines = $this->loadAddonOverrides($lines, $locale, $group);
82
            $lines = $this->loadSystemOverrides($lines, $locale, $group);
83
            $lines = $this->loadApplicationOverrides($lines, $locale, $group);
84
        }
85
86
        return $lines;
87
    }
88
89
    /**
90
     * Load namespaced overrides from
91
     * system AND application paths.
92
     *
93
     * @param array $lines
94
     * @param string $locale
95
     * @param string $group
96
     * @param string $namespace
97
     * @return array
98
     */
99
    protected function loadNamespaceOverrides(array $lines, $locale, $group, $namespace)
100
    {
101
        /**
102
         * @deprecated since 1.6; Use manual loading or publishing.
103
         */
104
        if (env('AUTOMATIC_ADDON_OVERRIDES', true)) {
105
            $lines = $this->loadAddonOverrides($lines, $locale, $group, $namespace);
106
        }
107
108
        $lines = $this->loadSystemOverrides($lines, $locale, $group, $namespace);
109
        $lines = $this->loadApplicationOverrides($lines, $locale, $group, $namespace);
110
111
        return parent::loadNamespaceOverrides($lines, $locale, $group, $namespace);
112
    }
113
114
    /**
115
     * Load system overrides.
116
     *
117
     * @param array $lines
118
     * @param        $locale
119
     * @param        $group
120
     * @param        $namespace
121
     * @return array
122
     */
123
    protected function loadSystemOverrides(array $lines, $locale, $group, $namespace = null)
124
    {
125
        if (!$namespace || $namespace == 'streams') {
126
127
            $file = base_path("resources/streams/lang/{$locale}/{$group}.php");
128
129
            if (is_dir(base_path("resources/streams/lang")) && $this->files->exists($file)) {
130
                $lines = array_replace_recursive($lines, $this->files->getRequire($file));
131
            }
132
        }
133
134
        if (str_is('*.*.*', $namespace)) {
0 ignored issues
show
Deprecated Code introduced by
The function str_is() has been deprecated: Str::is() should be used directly instead. Will be removed in Laravel 6.0. ( Ignorable by Annotation )

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

134
        if (/** @scrutinizer ignore-deprecated */ str_is('*.*.*', $namespace)) {

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
135
136
            list($vendor, $type, $slug) = explode('.', $namespace);
137
138
            $file = base_path("resources/addons/{$vendor}/{$slug}-{$type}/lang/{$locale}/{$group}.php");
139
140
            if (is_dir(base_path("resources/addons/{$vendor}/{$slug}-{$type}/lang")) && $this->files->exists($file)) {
141
                $lines = array_replace_recursive($lines, $this->files->getRequire($file));
142
            }
143
        }
144
145
        return $lines;
146
    }
147
148
    /**
149
     * Load system overrides.
150
     *
151
     * @param array $lines
152
     * @param        $locale
153
     * @param        $group
154
     * @param        $namespace
155
     * @return array
156
     */
157
    protected function loadApplicationOverrides(array $lines, $locale, $group, $namespace = null)
158
    {
159
        if (!$namespace || $namespace == 'streams') {
160
161
            $file = $this->application->getResourcesPath("streams/lang/{$locale}/{$group}.php");
162
163
            if (is_dir($this->application->getResourcesPath("streams/lang")) && $this->files->exists($file)) {
164
                $lines = array_replace_recursive($lines, $this->files->getRequire($file));
165
            }
166
        }
167
168
        if (str_is('*.*.*', $namespace)) {
0 ignored issues
show
Deprecated Code introduced by
The function str_is() has been deprecated: Str::is() should be used directly instead. Will be removed in Laravel 6.0. ( Ignorable by Annotation )

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

168
        if (/** @scrutinizer ignore-deprecated */ str_is('*.*.*', $namespace)) {

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
169
170
            list($vendor, $type, $slug) = explode('.', $namespace);
171
172
            $file = $this->application->getResourcesPath(
173
                "addons/{$vendor}/{$slug}-{$type}/lang/{$locale}/{$group}.php"
174
            );
175
176
            if (
177
                is_dir($this->application->getResourcesPath("addons/{$vendor}/{$slug}-{$type}/lang"))
178
                && $this->files->exists($file)
179
            ) {
180
                $lines = array_replace_recursive($lines, $this->files->getRequire($file));
181
            }
182
        }
183
        if (config()->has('override_text')) {
184
            foreach (config()->get('override_text') as $override) {
185
                $override = explode(':', $override);
186
187
                $lines = $this->findArrayValue($override[0], $override[1], $lines);
188
            }
189
        }
190
191
        return $lines;
192
    }
193
194
    function replaceNewValue($find_value, $new_value, $arr)
195
    {
196
        if (is_array($arr)) {
197
            foreach ($arr as $key => $item) {
198
                $arr[$key] = $this->replaceNewValue($find_value, $new_value, $item);
199
            }
200
            return $arr;
201
        }
202
        if (strtolower($arr) == strtolower($find_value)) {
203
            return $new_value;
204
        }
205
        return $arr;
206
    }
207
208
    function findArrayValue($find_value, $new_value, $arr)
209
    {
210
        foreach ($arr as $key => $item) {
211
            $arr[$key] = $this->replaceNewValue($find_value, $new_value, $item);
212
        }
213
        return $arr;
214
    }
215
216
217
    /**
218
     * @param array $lines
219
     * @param       $locale
220
     * @param       $group
221
     * @param null $namespace
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $namespace is correct as it would always require null to be passed?
Loading history...
222
     * @return array
223
     */
224
    protected function loadAddonOverrides(array $lines, $locale, $group, $namespace = null)
225
    {
226
        /** @var Addon $addon */
227
        foreach ($this->addons->enabled() as $addon) {
228
229
            $disabled = array_get(self::$disabled, $key = $addon->getNamespace('streams'), false);
0 ignored issues
show
Deprecated Code introduced by
The function array_get() has been deprecated: Arr::get() should be used directly instead. Will be removed in Laravel 6.0. ( Ignorable by Annotation )

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

229
            $disabled = /** @scrutinizer ignore-deprecated */ array_get(self::$disabled, $key = $addon->getNamespace('streams'), false);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
230
231
            if (!$disabled && !$this->files->isDirectory($addon->getPath('resources/streams'))) {
232
                self::$disabled[$key] = $disabled = true;
233
            }
234
235
            if (!$disabled && (!$namespace || $namespace == 'streams')) {
236
237
                $file = $addon->getPath("resources/streams/lang/{$locale}/{$group}.php");
238
239
                if ($this->files->exists($file)) {
240
                    $lines = array_replace_recursive($lines, $this->files->getRequire($file));
241
                }
242
            }
243
244
            $disabled = array_get(self::$disabled, $key = $addon->getNamespace('addons'), false);
0 ignored issues
show
Deprecated Code introduced by
The function array_get() has been deprecated: Arr::get() should be used directly instead. Will be removed in Laravel 6.0. ( Ignorable by Annotation )

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

244
            $disabled = /** @scrutinizer ignore-deprecated */ array_get(self::$disabled, $key = $addon->getNamespace('addons'), false);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
245
246
            if (!$disabled && !$this->files->isDirectory($addon->getPath('resources/addons'))) {
247
                self::$disabled[$key] = $disabled = true;
248
            }
249
250
            if (!$disabled && str_is('*.*.*', $namespace)) {
0 ignored issues
show
Deprecated Code introduced by
The function str_is() has been deprecated: Str::is() should be used directly instead. Will be removed in Laravel 6.0. ( Ignorable by Annotation )

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

250
            if (!$disabled && /** @scrutinizer ignore-deprecated */ str_is('*.*.*', $namespace)) {

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
251
252
                list($vendor, $type, $slug) = explode('.', $namespace);
253
254
                $file = $addon->getPath(
255
                    "resources/addons/{$vendor}/{$slug}-{$type}/lang/{$locale}/{$group}.php"
256
                );
257
258
                if ($this->files->exists($file)) {
259
                    $lines = array_replace_recursive($lines, $this->files->getRequire($file));
260
                }
261
            }
262
        }
263
264
        return $lines;
265
    }
266
}
267