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.
Passed
Push — master ( cf9260...b52382 )
by
unknown
03:19
created

Body::__toString()   C

Complexity

Conditions 14
Paths 25

Size

Total Lines 60
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 37
nc 25
nop 0
dl 0
loc 60
rs 6.2666
c 0
b 0
f 0

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\Assets\Positions;
15
16
// ------------------------------------------------------------------------
17
18
use O2System\Framework\Http\Presenter\Assets\Collections;
19
20
/**
21
 * Class Body
22
 *
23
 * @package O2System\Framework\Http\Presenter\Assets\Positions
24
 */
25
class Body extends Abstracts\AbstractPosition
26
{
27
    /**
28
     * Body::$javascripts
29
     *
30
     * @var \O2System\Framework\Http\Presenter\Assets\Collections\Javascripts
31
     */
32
    protected $javascripts;
33
34
    // ------------------------------------------------------------------------
35
36
    /**
37
     * Body::__construct
38
     */
39
    public function __construct()
40
    {
41
        $this->javascripts = new Collections\Javascripts();
42
    }
43
44
    // ------------------------------------------------------------------------
45
46
    /**
47
     * Body::loadFile
48
     *
49
     * @param string      $filename
50
     * @param string|null $subDir
51
     *
52
     * @return void
53
     */
54
    public function loadFile($filename, $subDir = null)
55
    {
56
        if (is_file($filename)) {
57
            $this->javascripts->append($filename);
58
        } else {
59
            $extension = pathinfo($filename, PATHINFO_EXTENSION);
60
61
            if (empty($extension)) {
62
                if (input()->env('DEBUG_STAGE') === 'PRODUCTION') {
63
                    if (false !== ($filePath = $this->getFilePath($filename . '.min.js'))) {
0 ignored issues
show
introduced by
The condition false !== $filePath = $t...($filename . '.min.js') is always true.
Loading history...
64
                        $this->javascripts->append($filePath);
65
                    }
66
                } else {
67
                    if (false !== ($filePath = $this->getFilePath($filename . '.js', $subDir))) {
0 ignored issues
show
introduced by
The condition false !== $filePath = $t...ename . '.js', $subDir) is always true.
Loading history...
68
                        $this->javascripts->append($filePath);
69
                    }
70
                }
71
            } elseif (false !== ($filePath = $this->getFilePath($filename, $subDir))) {
0 ignored issues
show
introduced by
The condition false !== $filePath = $t...ath($filename, $subDir) is always true.
Loading history...
72
                $this->javascripts->append($filePath);
73
            }
74
        }
75
    }
76
77
    // ------------------------------------------------------------------------
78
79
    /**
80
     * Body::__toString
81
     *
82
     * @return string
83
     */
84
    public function __toString()
85
    {
86
        $output = [];
87
        $unbundledFilenames = ['app', 'app.min', 'theme', 'theme.min'];
88
89
        if (presenter()->page->file instanceof \SplFileInfo) {
0 ignored issues
show
introduced by
presenter()->page->file is always a sub-type of SplFileInfo.
Loading history...
90
            if (presenter()->page->file->getFilename() === 'index') {
91
                $bundleFilename = 'body-' . presenter()->page->file->getDirectoryInfo()->getDirName();
92
            } else {
93
                $bundleFilename = 'body-' . presenter()->page->file->getDirectoryInfo()->getDirName() . '-' . presenter()->page->file->getFilename();
94
            }
95
        } elseif (services()->has('controller')) {
96
            $bundleFilename = 'body-' . controller()->getParameter();
97
98
            if (controller()->getRequestMethod() !== 'index') {
99
                $bundleFilename .= '-' . controller()->getRequestMethod();
100
            }
101
        } else {
102
            $bundleFilename = 'body-' . uniqid();
103
        }
104
105
        $bundleFilename = 'assets' . DIRECTORY_SEPARATOR . $bundleFilename;
106
107
        // Render js
108
        if ($this->javascripts->count()) {
109
            $bundleJavascriptSources = [];
110
111
            foreach ($this->javascripts as $javascript) {
112
                if (in_array(pathinfo($javascript, PATHINFO_FILENAME), $unbundledFilenames)) {
113
                    $fileVersion = $this->getVersion(filemtime($javascript));
114
                    $output[] = '<script type="text/javascript" id="js-'.pathinfo($javascript, PATHINFO_FILENAME).'" src="' . $this->getUrl($javascript) . '?v=' . $fileVersion . '"></script>';
115
                } elseif (in_array(pathinfo($javascript, PATHINFO_FILENAME), ['module', 'module.min'])) {
116
                    $modulePublicFile = $this->publishFile($javascript);
117
118
                    if (is_file($modulePublicFile[ 'filePath' ])) {
119
                        if (input()->env('DEBUG_STAGE') === 'PRODUCTION') {
120
                            $output[] = '<script type="text/javascript" id="js-module" src="' . $modulePublicFile[ 'minify' ][ 'url' ] . '?v=' . $modulePublicFile[ 'version' ] . '"></script>';
121
                        } else {
122
                            $output[] = '<script type="text/javascript" id="js-module" src="' . $modulePublicFile[ 'url' ] . '?v=' . $modulePublicFile[ 'version' ] . '"></script>';
123
                        }
124
                    }
125
                } else {
126
                    $bundleJavascriptSources[] = $javascript;
127
                }
128
            }
129
130
            if (count($bundleJavascriptSources)) {
131
                $bundleJavascriptPublicFile = $this->bundleFile($bundleFilename . '.js', $bundleJavascriptSources);
132
133
                if (is_file($bundleJavascriptPublicFile[ 'filePath' ])) {
134
                    if (input()->env('DEBUG_STAGE') === 'PRODUCTION') {
135
                        $output[] = '<script type="text/javascript" id="js-bundle" src="' . $bundleJavascriptPublicFile[ 'minify' ][ 'url' ] . '?v=' . $bundleJavascriptPublicFile[ 'version' ] . '"></script>';
136
                    } else {
137
                        $output[] = '<script type="text/javascript" id="js-bundle" src="' . $bundleJavascriptPublicFile[ 'url' ] . '?v=' . $bundleJavascriptPublicFile[ 'version' ] . '"></script>';
138
                    }
139
                }
140
            }
141
        }
142
143
        return implode(PHP_EOL, $output);
144
    }
145
}