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.

Head   D
last analyzed

Complexity

Total Complexity 58

Size/Duplication

Total Lines 273
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 142
c 1
b 0
f 0
dl 0
loc 273
rs 4.5599
wmc 58

3 Methods

Rating   Name   Duplication   Size   Complexity  
D loadFile() 0 104 31
F __toString() 0 112 26
A __construct() 0 5 1

How to fix   Complexity   

Complex Class

Complex classes like Head often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Head, and based on these observations, apply Extract Interface, too.

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 Head
22
 *
23
 * @package O2System\Framework\Http\Presenter\Assets\Positions
24
 */
25
class Head extends Abstracts\AbstractPosition
26
{
27
    /**
28
     * Head::$fonts
29
     *
30
     * @var \O2System\Framework\Http\Presenter\Assets\Collections\Fonts
31
     */
32
    protected $fonts;
33
34
    /**
35
     * Head::$styles
36
     *
37
     * @var \O2System\Framework\Http\Presenter\Assets\Collections\Styles
38
     */
39
    protected $styles;
40
41
    /**
42
     * Head::$javascripts
43
     *
44
     * @var \O2System\Framework\Http\Presenter\Assets\Collections\Javascripts
45
     */
46
    protected $javascripts;
47
48
    // ------------------------------------------------------------------------
49
50
    /**
51
     * Head::__construct
52
     */
53
    public function __construct()
54
    {
55
        $this->fonts = new Collections\Fonts();
56
        $this->styles = new Collections\Styles();
57
        $this->javascripts = new Collections\Javascripts();
58
    }
59
60
    // ------------------------------------------------------------------------
61
62
    /**
63
     * Head::loadFile
64
     *
65
     * @param string      $filename
66
     * @param string|null $subDir
67
     *
68
     * @return void
69
     */
70
    public function loadFile($filename, $subDir = null)
71
    {
72
        if (is_file($filename)) {
73
            if (strpos($filename, 'font') !== false) {
74
                $this->fonts->append($filename);
75
            } else {
76
                $type = pathinfo($filename, PATHINFO_EXTENSION);
77
78
                switch ($type) {
79
                    case 'css':
80
                        $this->styles->append($filename);
81
                        break;
82
83
                    case 'js':
84
                        $this->javascripts->append($filename);
85
                        break;
86
                }
87
            }
88
        } elseif (isset($subDir)) {
89
            $type = pathinfo($filename, PATHINFO_EXTENSION);
90
91
            if (empty($type)) {
92
                $type = substr($subDir, 0, -1);
93
            }
94
95
            switch ($type) {
96
                case 'css':
97
                    if (input()->env('DEBUG_STAGE') === 'PRODUCTION') {
98
                        if (false !== ($filePath = $this->getFilePath($filename . '.min.css', $subDir))) {
0 ignored issues
show
introduced by
The condition false !== $filePath = $t... . '.min.css', $subDir) is always true.
Loading history...
99
                            $this->styles->append($filePath);
100
                        }
101
                    } else {
102
                        if (false !== ($filePath = $this->getFilePath($filename . '.css', $subDir))) {
0 ignored issues
show
introduced by
The condition false !== $filePath = $t...name . '.css', $subDir) is always true.
Loading history...
103
                            $this->styles->append($filePath);
104
                        }
105
                    }
106
                    break;
107
108
                case 'fonts':
109
                    if (input()->env('DEBUG_STAGE') === 'PRODUCTION') {
110
                        if (false !== ($filePath = $this->getFilePath($filename . '.min.css', $subDir))) {
0 ignored issues
show
introduced by
The condition false !== $filePath = $t... . '.min.css', $subDir) is always true.
Loading history...
111
                            $this->fonts->append($filePath);
112
                        }
113
                    } else {
114
                        if (false !== ($filePath = $this->getFilePath($filename . '.css', $subDir))) {
0 ignored issues
show
introduced by
The condition false !== $filePath = $t...name . '.css', $subDir) is always true.
Loading history...
115
                            $this->fonts->append($filePath);
116
                        }
117
                    }
118
                    break;
119
120
                case 'js':
121
                    if (input()->env('DEBUG_STAGE') === 'PRODUCTION') {
122
                        if (false !== ($filePath = $this->getFilePath($filename . '.min.js', $subDir))) {
0 ignored issues
show
introduced by
The condition false !== $filePath = $t...e . '.min.js', $subDir) is always true.
Loading history...
123
                            $this->styles->append($filePath);
124
                        }
125
                    } else {
126
                        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...
127
                            $this->styles->append($filePath);
128
                        }
129
                    }
130
                    break;
131
            }
132
        } else {
133
            $type = pathinfo($filename, PATHINFO_EXTENSION);
134
135
            if (empty($type)) {
136
                if (input()->env('DEBUG_STAGE') === 'PRODUCTION') {
137
                    // Search Style or Font
138
                    if (false !== ($filePath = $this->getFilePath($filename . '.min.css'))) {
0 ignored issues
show
introduced by
The condition false !== $filePath = $t...$filename . '.min.css') is always true.
Loading history...
139
                        $this->styles->append($filePath);
140
                    } elseif (false !== ($filePath = $this->getFilePath($filename . '.min.css'))) {
141
                        $this->fonts->append($filePath);
142
                    }
143
144
                    // Search Javascript
145
                    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...
146
                        $this->javascripts->append($filePath);
147
                    }
148
                } else {
149
                    // Search Style or Font
150
                    if (false !== ($filePath = $this->getFilePath($filename . '.css'))) {
0 ignored issues
show
introduced by
The condition false !== $filePath = $t...ath($filename . '.css') is always true.
Loading history...
151
                        $this->styles->append($filePath);
152
                    } elseif (false !== ($filePath = $this->getFilePath($filename . '.css'))) {
153
                        $this->fonts->append($filePath);
154
                    }
155
156
                    // Search Javascript
157
                    if (false !== ($filePath = $this->getFilePath($filename . '.js'))) {
0 ignored issues
show
introduced by
The condition false !== $filePath = $t...Path($filename . '.js') is always true.
Loading history...
158
                        $this->javascripts->append($filePath);
159
                    }
160
                }
161
            } else {
162
                switch ($type) {
163
                    case 'css':
164
                        if (false !== ($filePath = $this->getFilePath($filename))) {
0 ignored issues
show
introduced by
The condition false !== $filePath = $t...>getFilePath($filename) is always true.
Loading history...
165
                            $this->styles->append($filePath);
166
                        }
167
                        break;
168
169
                    case 'js':
170
                        if (false !== ($filePath = $this->getFilePath($filename))) {
0 ignored issues
show
introduced by
The condition false !== $filePath = $t...>getFilePath($filename) is always true.
Loading history...
171
                            $this->javascripts->append($filePath);
172
                        }
173
                        break;
174
                }
175
            }
176
        }
177
    }
178
179
    // ------------------------------------------------------------------------
180
181
    /**
182
     * Head::__toString
183
     *
184
     * @return string
185
     */
186
    public function __toString()
187
    {
188
        $output = [];
189
190
        // Render fonts
191
        if ($this->fonts->count()) {
192
193
            $bundleFontsFile = $this->bundleFile('assets' . DIRECTORY_SEPARATOR . 'fonts.css',
194
                implode(PHP_EOL, $this->fonts));
0 ignored issues
show
Bug introduced by
implode(O2System\Framewo...\PHP_EOL, $this->fonts) of type string is incompatible with the type array expected by parameter $sources of O2System\Framework\Http\...tPosition::bundleFile(). ( Ignorable by Annotation )

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

194
                /** @scrutinizer ignore-type */ implode(PHP_EOL, $this->fonts));
Loading history...
Bug introduced by
$this->fonts of type O2System\Framework\Http\...ssets\Collections\Fonts is incompatible with the type array expected by parameter $pieces of implode(). ( Ignorable by Annotation )

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

194
                implode(PHP_EOL, /** @scrutinizer ignore-type */ $this->fonts));
Loading history...
195
196
            if (is_file($bundleFontsFile[ 'filePath' ])) {
197
                if (input()->env('DEBUG_STAGE') === 'PRODUCTION') {
198
                    $output[] = '<link rel="stylesheet" type="text/css" media="all" href="' . $bundleFontsFile[ 'url' ] . '?v=' . $bundleFontsFile[ 'version' ] . '">';
199
                } else {
200
                    $output[] = '<link rel="stylesheet" type="text/css" media="all" href="' . $bundleFontsFile[ 'url' ] . '?v=' . $bundleFontsFile[ 'version' ] . '">';
201
                }
202
            }
203
        }
204
205
        $unbundledFilenames = ['app', 'app.min', 'theme', 'theme.min'];
206
207
        if (presenter()->page->file instanceof \SplFileInfo) {
0 ignored issues
show
introduced by
presenter()->page->file is always a sub-type of SplFileInfo.
Loading history...
208
            if (presenter()->page->file->getFilename() === 'index') {
209
                $bundleFilename = 'head-' . presenter()->page->file->getDirectoryInfo()->getDirName();
210
            } else {
211
                $bundleFilename = 'head-' . presenter()->page->file->getDirectoryInfo()->getDirName() . '-' . presenter()->page->file->getFilename();
212
            }
213
        } elseif (services()->has('controller')) {
214
            $bundleFilename = 'head-' . controller()->getParameter();
215
216
            if (controller()->getRequestMethod() !== 'index') {
217
                $bundleFilename .= '-' . controller()->getRequestMethod();
218
            }
219
        } else {
220
            $bundleFilename = 'head-' . uniqid();
221
        }
222
223
        $bundleFilename = 'assets' . DIRECTORY_SEPARATOR . $bundleFilename;
224
        
225
        // Render style
226
        if ($this->styles->count()) {
227
            $bundleStyleSources = [];
228
229
            foreach ($this->styles as $style) {
230
                if (in_array(pathinfo($style, PATHINFO_FILENAME), $unbundledFilenames)) {
231
                    $fileVersion = $this->getVersion(filemtime($style));
232
                    $output[] = '<link rel="stylesheet" type="text/css" media="all" href="' . $this->getUrl($style) . '?v=' . $fileVersion . '">';
233
                } elseif (in_array(pathinfo($style, PATHINFO_FILENAME), ['module', 'module.min'])) {
234
                    $modulePublicFile = $this->publishFile($style);
235
                    
236
                    if (is_file($modulePublicFile[ 'filePath' ])) {
237
                        if (input()->env('DEBUG_STAGE') === 'PRODUCTION') {
238
                            $output[] = '<link rel="stylesheet" type="text/css" media="all" href="' . $modulePublicFile[ 'minify' ][ 'url' ] . '?v=' . $modulePublicFile[ 'version' ] . '">';
239
                        } else {
240
                            $output[] = '<link rel="stylesheet" type="text/css" media="all" href="' . $modulePublicFile[ 'url' ] . '?v=' . $modulePublicFile[ 'version' ] . '">';
241
                        }
242
                    }
243
                } else {
244
                    $bundleStyleSources[] = $style;
245
                }
246
            }
247
248
            if (count($bundleStyleSources)) {
249
                $bundleStylePublicFile = $this->bundleFile($bundleFilename . '.css', $bundleStyleSources);
250
251
                if (is_file($bundleStylePublicFile[ 'filePath' ])) {
252
                    if (input()->env('DEBUG_STAGE') === 'PRODUCTION') {
253
                        $output[] = '<link rel="stylesheet" type="text/css" media="all" href="' . $bundleStylePublicFile[ 'minify' ][ 'url' ] . '?v=' . $bundleStylePublicFile[ 'version' ] . '">';
254
                    } else {
255
                        $output[] = '<link rel="stylesheet" type="text/css" media="all" href="' . $bundleStylePublicFile[ 'url' ] . '?v=' . $bundleStylePublicFile[ 'version' ] . '">';
256
                    }
257
                }
258
            }
259
        }
260
261
        // Render javascript
262
        if ($this->javascripts->count()) {
263
            $bundleJavascriptSources = [];
264
265
            foreach ($this->javascripts as $javascript) {
266
                if (in_array(pathinfo($style, PATHINFO_FILENAME), $unbundledFilenames)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $style does not seem to be defined for all execution paths leading up to this point.
Loading history...
267
                    $fileVersion = $this->getVersion(filemtime($javascript));
268
                    $output[] = '<script type="text/javascript" id="js-'.pathinfo($javascript, PATHINFO_FILENAME).'" src="' . $this->getUrl($javascript) . '?v=' . $fileVersion . '"></script>';
269
                } elseif (in_array(pathinfo($javascript, PATHINFO_FILENAME), ['module', 'module.min'])) {
270
                    $modulePublicFile = $this->publishFile($javascript);
271
                    
272
                    if (is_file($modulePublicFile[ 'filePath' ])) {
273
                        if (input()->env('DEBUG_STAGE') === 'PRODUCTION') {
274
                            $output[] = '<script type="text/javascript" id="js-module" src="' . $modulePublicFile[ 'minify' ][ 'url' ] . '?v=' . $modulePublicFile[ 'version' ] . '"></script>';
275
                        } else {
276
                            $output[] = '<script type="text/javascript" id="js-module" src="' . $modulePublicFile[ 'url' ] . '?v=' . $modulePublicFile[ 'version' ] . '"></script>';
277
                        }
278
                    }
279
                } else {
280
                    $bundleJavascriptSources[] = $javascript;
281
                }
282
            }
283
284
            if (count($bundleJavascriptSources)) {
285
                $bundleJavascriptPublicFile = $this->bundleFile($bundleFilename . '.js', $bundleJavascriptSources);
286
287
                if (is_file($bundleJavascriptPublicFile[ 'filePath' ])) {
288
                    if (input()->env('DEBUG_STAGE') === 'PRODUCTION') {
289
                        $output[] = '<script type="text/javascript" id="js-bundle" src="' . $bundleJavascriptPublicFile[ 'minify' ][ 'url' ] . '?v=' . $bundleJavascriptPublicFile[ 'version' ] . '"></script>';
290
                    } else {
291
                        $output[] = '<script type="text/javascript" id="js-bundle" src="' . $bundleJavascriptPublicFile[ 'url' ] . '?v=' . $bundleJavascriptPublicFile[ 'version' ] . '"></script>';
292
                    }
293
                }
294
            }
295
        }
296
297
        return implode(PHP_EOL, $output);
298
    }
299
}