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.

Assets::loadPackage()   D
last analyzed

Complexity

Conditions 19
Paths 21

Size

Total Lines 81
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 19
eloc 53
c 2
b 0
f 0
nc 21
nop 2
dl 0
loc 81
rs 4.5166

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
 * This file is part of the O2System Framework package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @author         Steeve Andrian Salim
9
 * @copyright      Copyright (c) Steeve Andrian Salim
10
 */
11
12
// ------------------------------------------------------------------------
13
14
namespace O2System\Framework\Http\Presenter;
15
16
// ------------------------------------------------------------------------
17
18
use O2System\Spl\Traits\Collectors\FilePathCollectorTrait;
19
20
/**
21
 * Class Assets
22
 *
23
 * @package O2System\Framework\Http\Presenter
24
 */
25
class Assets
26
{
27
    use FilePathCollectorTrait;
28
29
    /**
30
     * Assets::$head
31
     *
32
     * @var \O2System\Framework\Http\Presenter\Assets\Positions\Head
33
     */
34
    protected $head;
35
36
    /**
37
     * Assets::$body
38
     *
39
     * @var \O2System\Framework\Http\Presenter\Assets\Positions\Body
40
     */
41
    protected $body;
42
43
    // ------------------------------------------------------------------------
44
45
    /**
46
     * Assets::__construct
47
     */
48
    public function __construct()
49
    {
50
        $this->addFilePaths([
51
            PATH_RESOURCES,
52
            PATH_RESOURCES . 'views' . DIRECTORY_SEPARATOR
53
        ]);
54
55
        $this->head = new Assets\Positions\Head();
56
        $this->body = new Assets\Positions\Body();
57
    }
58
59
    // ------------------------------------------------------------------------
60
61
    /**
62
     * Assets::autoload
63
     *
64
     * @param array $assets
65
     *
66
     * @return void
67
     */
68
    public function autoload($assets)
69
    {
70
        foreach ($assets as $position => $collections) {
71
            if (property_exists($this, $position)) {
72
73
                if ($collections instanceof \ArrayObject) {
74
                    $collections = $collections->getArrayCopy();
75
                }
76
77
                $this->{$position}->loadCollections($collections);
78
            } elseif ($position === 'packages') {
79
                $this->loadPackages($collections);
80
            } elseif ($position === 'css') {
81
                $this->loadCss($collections);
82
            } elseif ($position === 'js') {
83
                $this->loadJs($collections);
84
            }
85
        }
86
    }
87
88
    // ------------------------------------------------------------------------
89
90
    /**
91
     * Assets::loadPackages
92
     *
93
     * @param array $packages
94
     *
95
     * @return void
96
     */
97
    public function loadPackages($packages)
98
    {
99
        foreach ($packages as $package => $files) {
100
            if (is_string($files)) {
101
                $this->loadPackage($files);
102
            } elseif (is_array($files)) {
103
                $this->loadPackage($package, $files);
104
            } elseif (is_object($files)) {
105
                $this->loadPackage($package, get_object_vars($files));
106
            }
107
        }
108
    }
109
110
    // ------------------------------------------------------------------------
111
112
    /**
113
     * Assets::loadPackage
114
     *
115
     * @param string $package
116
     * @param array  $subPackages
117
     *
118
     * @return void
119
     */
120
    public function loadPackage($package, $subPackages = [])
121
    {
122
        $packageDir = implode(DIRECTORY_SEPARATOR, [
123
                'packages',
124
                $package,
125
            ]) . DIRECTORY_SEPARATOR;
126
127
        if (count($subPackages)) {
128
129
            if (array_key_exists('libraries', $subPackages)) {
130
                foreach ($subPackages[ 'libraries' ] as $subPackageFile) {
131
                    $pluginDir = $packageDir . 'libraries' . DIRECTORY_SEPARATOR;
132
                    $pluginName = $subPackageFile;
133
134
                    if ($this->body->loadFile($pluginDir . $pluginName . DIRECTORY_SEPARATOR . $pluginName . '.js')) {
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->body->loadFile($p... . $pluginName . '.js') targeting O2System\Framework\Http\...itions\Body::loadFile() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
135
                        $this->head->loadFile($pluginDir . $pluginName . DIRECTORY_SEPARATOR . $pluginName . '.css');
136
                    } else {
137
                        $this->body->loadFile($pluginDir . $pluginName . '.js');
138
                    }
139
                }
140
141
                unset($subPackages[ 'libraries' ]);
142
            }
143
144
            $this->head->loadFile($packageDir . $package . '.css');
145
            $this->body->loadFile($packageDir . $package . '.js');
146
147
            foreach ($subPackages as $subPackage => $subPackageFiles) {
148
                if ($subPackage === 'theme' or $subPackage === 'themes') {
149
                    if (is_string($subPackageFiles)) {
150
                        $subPackageFiles = [$subPackageFiles];
151
                    }
152
153
                    foreach ($subPackageFiles as $themeName) {
154
                        $themeDir = $packageDir . 'themes' . DIRECTORY_SEPARATOR;
155
156
                        if ($this->head->loadFile($themeDir . $themeName . DIRECTORY_SEPARATOR . $themeName . '.css')) {
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->head->loadFile($t... . $themeName . '.css') targeting O2System\Framework\Http\...itions\Head::loadFile() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
157
                            $this->body->loadFile($themeDir . $themeName . DIRECTORY_SEPARATOR . $themeName . '.js');
158
                        } else {
159
                            $this->head->loadFile($themeDir . $themeName . '.css');
160
                        }
161
                    }
162
                } elseif ($subPackage === 'plugins') {
163
                    foreach ($subPackageFiles as $subPackageFile) {
164
                        $pluginDir = $packageDir . 'plugins' . DIRECTORY_SEPARATOR;
165
                        $pluginName = $subPackageFile;
166
167
                        $pluginName = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $pluginName);
168
                        if (strpos($pluginName, DIRECTORY_SEPARATOR) !== false) {
169
                            $pluginDir .= pathinfo($pluginName, PATHINFO_DIRNAME) . DIRECTORY_SEPARATOR;
170
                            $pluginName = pathinfo($pluginName, PATHINFO_BASENAME);
171
                        }
172
173
                        if ($this->body->loadFile($pluginDir . $pluginName . DIRECTORY_SEPARATOR . $pluginName . '.js')) {
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->body->loadFile($p... . $pluginName . '.js') targeting O2System\Framework\Http\...itions\Body::loadFile() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
174
                            $this->head->loadFile($pluginDir . $pluginName . DIRECTORY_SEPARATOR . $pluginName . '.css');
175
                        } else {
176
                            $this->body->loadFile($pluginDir . $pluginName . '.js');
177
                        }
178
                    }
179
                } elseif ($subPackage === 'libraries') {
180
                    foreach ($subPackageFiles as $subPackageFile) {
181
                        $libraryDir = $packageDir . 'libraries' . DIRECTORY_SEPARATOR;
182
                        $libraryName = $subPackageFile;
183
184
                        $libraryName = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $libraryName);
185
                        if (strpos($libraryName, DIRECTORY_SEPARATOR) !== false) {
186
                            $libraryDir .= pathinfo($libraryName, PATHINFO_DIRNAME) . DIRECTORY_SEPARATOR;
187
                            $libraryName = pathinfo($libraryName, PATHINFO_BASENAME);
188
                        }
189
190
                        if ($this->body->loadFile($libraryDir . $libraryName . DIRECTORY_SEPARATOR . $libraryName . '.js')) {
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->body->loadFile($l.... $libraryName . '.js') targeting O2System\Framework\Http\...itions\Body::loadFile() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
191
                            $this->head->loadFile($libraryDir . $libraryName . DIRECTORY_SEPARATOR . $libraryName . '.css');
192
                        } else {
193
                            $this->body->loadFile($libraryDir . $libraryName . '.js');
194
                        }
195
                    }
196
                }
197
            }
198
        } else {
199
            $this->head->loadFile($packageDir . $package . '.css');
200
            $this->body->loadFile($packageDir . $package . '.js');
201
        }
202
    }
203
204
    // ------------------------------------------------------------------------
205
206
    /**
207
     * Assets::loadCss
208
     *
209
     * @param string|array $files
210
     *
211
     * @return void
212
     */
213
    public function loadCss($files)
214
    {
215
        $files = is_string($files) ? [$files] : $files;
216
        $this->head->loadCollections(['css' => $files]);
217
    }
218
219
    // ------------------------------------------------------------------------
220
221
    /**
222
     * Assets::loadJs
223
     *
224
     * @param string|array $files
225
     * @param string       $position
226
     *
227
     * @return void
228
     */
229
    public function loadJs($files, $position = 'body')
230
    {
231
        $files = is_string($files) ? [$files] : $files;
232
        $this->{$position}->loadCollections(['js' => $files]);
233
    }
234
235
    // ------------------------------------------------------------------------
236
237
    /**
238
     * Assets::loadFiles
239
     *
240
     * @param array $assets
241
     *
242
     * @return void
243
     */
244
    public function loadFiles($assets)
245
    {
246
        foreach ($assets as $type => $item) {
247
            $addMethod = 'load' . ucfirst($type);
248
249
            if (method_exists($this, $addMethod)) {
250
                call_user_func_array([&$this, $addMethod], [$item]);
251
            }
252
        }
253
    }
254
255
    // ------------------------------------------------------------------------
256
257
    /**
258
     * Assets::theme
259
     *
260
     * @param string $path
261
     *
262
     * @return string
263
     */
264
    public function theme($path)
265
    {
266
        $path = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $path);
267
268
        if (is_file($filePath = PATH_THEME . $path)) {
269
            return path_to_url($filePath);
270
        }
271
    }
272
273
    // ------------------------------------------------------------------------
274
275
    /**
276
     * Assets::file
277
     *
278
     * @param string $file
279
     *
280
     * @return string
281
     */
282
    public function file($file)
283
    {
284
        $filePaths = loader()->getPublicDirs(true);
285
286
        foreach ($filePaths as $filePath) {
287
            if (is_file($filePath . $file)) {
288
                return path_to_url($filePath . $file);
289
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
290
            }
291
        }
292
    }
293
294
    // ------------------------------------------------------------------------
295
296
    /**
297
     * Assets::image
298
     *
299
     * @param string $image
300
     *
301
     * @return string
302
     */
303
    public function image($image)
304
    {
305
        $filePaths = loader()->getPublicDirs(true);
306
        $image = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $image);
307
308
        foreach ($filePaths as $filePath) {
309
            $filePath .= 'img' . DIRECTORY_SEPARATOR;
310
311
            if (is_file($filePath . $image)) {
312
                return path_to_url($filePath . $image);
313
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
314
            }
315
316
            unset($filePath);
317
        }
318
    }
319
320
    // ------------------------------------------------------------------------
321
322
    /**
323
     * Assets::media
324
     *
325
     * @param string $media
326
     *
327
     * @return string
328
     */
329
    public function media($media)
330
    {
331
        $filePaths = loader()->getPublicDirs(true);
332
        $media = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $media);
333
334
        foreach ($filePaths as $filePath) {
335
            $filePath .= 'media' . DIRECTORY_SEPARATOR;
336
337
            if (is_file($filePath . $media)) {
338
                return path_to_url($filePath . $media);
339
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
340
            }
341
342
            unset($filePath);
343
        }
344
    }
345
346
    // ------------------------------------------------------------------------
347
348
    /**
349
     * Assets::parseSourceCode
350
     *
351
     * @param string $sourceCode
352
     *
353
     * @return string
354
     */
355
    public function parseSourceCode($sourceCode)
356
    {
357
        $sourceCode = str_replace(
358
            [
359
                '"../assets/',
360
                "'../assets/",
361
                "(../assets/",
362
            ],
363
            [
364
                '"' . base_url() . '/assets/',
365
                "'" . base_url() . '/assets/',
366
                "(" . base_url() . '/assets/',
367
            ],
368
            $sourceCode);
369
370
        if (presenter()->theme) {
371
            $sourceCode = str_replace(
372
                [
373
                    '"assets/',
374
                    "'assets/",
375
                    "(assets/",
376
377
                    // with dot
378
                    '"./assets/',
379
                    "'./assets/",
380
                    "(./assets/",
381
                ],
382
                [
383
                    '"' . presenter()->theme->getUrl('assets/'),
384
                    "'" . presenter()->theme->getUrl('assets/'),
385
                    "(" . presenter()->theme->getUrl('assets/'),
386
387
                    // with dot
388
                    '"' . presenter()->theme->getUrl('assets/'),
389
                    "'" . presenter()->theme->getUrl('assets/'),
390
                    "(" . presenter()->theme->getUrl('assets/'),
391
                ],
392
                $sourceCode);
393
        }
394
395
        // Valet path fixes
396
        if (isset($_SERVER[ 'SCRIPT_FILENAME' ])) {
397
            $valetPath = dirname($_SERVER[ 'SCRIPT_FILENAME' ]) . DIRECTORY_SEPARATOR;
398
        } else {
399
            $PATH_ROOT = $_SERVER[ 'DOCUMENT_ROOT' ];
400
401
            if (isset($_SERVER[ 'PHP_SELF' ])) {
402
                $valetPath = $PATH_ROOT . dirname($_SERVER[ 'PHP_SELF' ]) . DIRECTORY_SEPARATOR;
403
            } elseif (isset($_SERVER[ 'DOCUMENT_URI' ])) {
404
                $valetPath = $PATH_ROOT . dirname($_SERVER[ 'DOCUMENT_URI' ]) . DIRECTORY_SEPARATOR;
405
            } elseif (isset($_SERVER[ 'REQUEST_URI' ])) {
406
                $valetPath = $PATH_ROOT . dirname($_SERVER[ 'REQUEST_URI' ]) . DIRECTORY_SEPARATOR;
407
            } elseif (isset($_SERVER[ 'SCRIPT_NAME' ])) {
408
                $valetPath = $PATH_ROOT . dirname($_SERVER[ 'SCRIPT_NAME' ]) . DIRECTORY_SEPARATOR;
409
            }
410
        }
411
412
        if (isset($valetPath)) {
413
            $sourceCode = str_replace($valetPath, '/', $sourceCode);
414
        }
415
416
        return $sourceCode;
417
    }
418
419
    // ------------------------------------------------------------------------
420
421
    /**
422
     * Assets::getHead
423
     *
424
     * @return \O2System\Framework\Http\Presenter\Assets\Positions\Head
425
     */
426
    public function getHead()
427
    {
428
        return $this->head;
429
    }
430
431
    // ------------------------------------------------------------------------
432
433
    /**
434
     * Assets::getBody
435
     *
436
     * @return \O2System\Framework\Http\Presenter\Assets\Positions\Body
437
     */
438
    public function getBody()
439
    {
440
        return $this->body;
441
    }
442
}
443