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 ( 890c9c...35b399 )
by
unknown
02:03
created

Body::__toString()   D

Complexity

Conditions 16
Paths 226

Size

Total Lines 89
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 16
eloc 52
nc 226
nop 0
dl 0
loc 89
rs 4.4083
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 MatthiasMullie\Minify\JS;
19
use O2System\Framework\Http\Presenter\Assets\Collections;
20
21
/**
22
 * Class Body
23
 *
24
 * @package O2System\Framework\Http\Presenter\Assets\Positions
25
 */
26
class Body extends Abstracts\AbstractPosition
27
{
28
    /**
29
     * Body::$javascript
30
     *
31
     * @var \O2System\Framework\Http\Presenter\Assets\Collections\Javascript
32
     */
33
    protected $javascript;
34
35
    // ------------------------------------------------------------------------
36
37
    /**
38
     * Body::__construct
39
     */
40
    public function __construct()
41
    {
42
        $this->javascript = new Collections\Javascript();
43
    }
44
45
    // ------------------------------------------------------------------------
46
47
    /**
48
     * Body::__toString
49
     *
50
     * @return string
51
     */
52
    public function __toString()
53
    {
54
        $output = [];
55
        $unbundledFilename = ['app', 'app.min', 'theme', 'theme.min'];
56
57
        // Render js
58
        if ($this->javascript->count()) {
59
            $bundleJsContents = [];
60
61
            foreach ($this->javascript as $js) {
62
                if (in_array(pathinfo($js, PATHINFO_FILENAME), $unbundledFilename)) {
63
                    $jsVersion = $this->getVersion($js);
64
                    $output[] = '<script type="text/javascript" src="' . $this->getUrl($js) . '?v=' . $jsVersion . '"></script>';
65
                } else {
66
                    $bundleJsMap[ 'sources' ][] = $js;
67
                    $bundleJsContents[] = file_get_contents($js);
68
                }
69
            }
70
71
            // Bundled Js
72
            $bundleJsVersion = $this->getVersion(serialize($bundleJsContents));
73
74
            if(presenter()->page->file instanceof \SplFileInfo) {
0 ignored issues
show
introduced by
presenter()->page->file is always a sub-type of SplFileInfo.
Loading history...
75
                if(presenter()->page->file->getFilename() === 'index') {
76
                    $bundleJsFilename = 'body-' . presenter()->page->file->getDirectoryInfo()->getDirName();
77
                } else {
78
                    $bundleJsFilename = 'body-' . presenter()->page->file->getDirectoryInfo()->getDirName() . '-' . presenter()->page->file->getFilename();
79
                }
80
            } elseif(services()->has('controller')) {
81
                $bundleJsFilename = 'body-' . controller()->getParameter();
82
83
                if(controller()->getRequestMethod() !== 'index') {
84
                    $bundleJsFilename.= '-' . controller()->getRequestMethod();
85
                }
86
            } else {
87
                $bundleJsFilename = 'body-' . md5($bundleJsVersion);
88
            }
89
90
            $bundlePublicDir = modules()->current()->getPublicDir() . 'assets' . DIRECTORY_SEPARATOR;
0 ignored issues
show
Bug introduced by
The method current() does not exist on O2System\Framework\Conta...s\DataStructures\Module. ( Ignorable by Annotation )

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

90
            $bundlePublicDir = modules()->/** @scrutinizer ignore-call */ current()->getPublicDir() . 'assets' . DIRECTORY_SEPARATOR;

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
91
            $bundleJsFilePath = $bundlePublicDir . $bundleJsFilename . '.js';
92
            $bundleJsMinifyFilePath = $bundlePublicDir . $bundleJsFilename . '.min.js';
93
94
            if (is_file($bundleJsFilePath . '.map')) {
95
                $bundleJsMap = json_decode(file_get_contents($bundleJsFilePath . '.map'), true);
96
                // if the file version is changed delete it first
97
                if ( ! hash_equals($bundleJsVersion, $bundleJsMap[ 'version' ])) {
98
                    unlink($bundleJsFilePath);
99
                    unlink($bundleJsFilePath . '.map');
100
                }
101
            }
102
103
            if ( ! is_file($bundleJsFilePath)) {
104
                $bundleJsMap[ 'version' ] = $bundleJsVersion;
105
106
                // Create css file
107
                if (count($bundleJsContents)) {
108
                    if ($bundleFileStream = @fopen($bundleJsFilePath, 'ab')) {
109
                        flock($bundleFileStream, LOCK_EX);
110
                        fwrite($bundleFileStream, implode(PHP_EOL, $bundleJsContents));
111
                        flock($bundleFileStream, LOCK_UN);
112
                        fclose($bundleFileStream);
113
114
                        // Create css map
115
                        if ($bundleFileStream = @fopen($bundleJsFilePath . '.map', 'ab')) {
116
                            flock($bundleFileStream, LOCK_EX);
117
118
                            fwrite($bundleFileStream, json_encode($bundleJsMap));
119
120
                            flock($bundleFileStream, LOCK_UN);
121
                            fclose($bundleFileStream);
122
                        }
123
124
                        // Create javascript file minify version
125
                        $minifyJsHandler = new JS($bundleJsFilePath);
126
                        $minifyJsHandler->minify($bundleJsMinifyFilePath);
127
                    }
128
                }
129
            }
130
131
            if(is_file($bundleJsFilePath)) {
132
                if (input()->env('DEBUG_STAGE') === 'PRODUCTION') {
133
                    $output[] = '<script type="text/javascript" src="' . $this->getUrl($bundleJsMinifyFilePath) . '?v=' . $bundleJsVersion . '"></script>';
134
                } else {
135
                    $output[] = '<script type="text/javascript" src="' . $this->getUrl($bundleJsFilePath) . '?v=' . $bundleJsVersion . '"></script>';
136
                }
137
            }
138
        }
139
140
        return implode(PHP_EOL, $output);
141
    }
142
}