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.
Test Setup Failed
Push — master ( c5ade0...e039e4 )
by Gabriel
05:51
created

Scripts::pack()   D

Complexity

Conditions 9
Paths 12

Size

Total Lines 45
Code Lines 27

Duplication

Lines 6
Ratio 13.33 %

Importance

Changes 0
Metric Value
cc 9
eloc 27
nc 12
nop 1
dl 6
loc 45
rs 4.909
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A Scripts::render() 0 11 4
1
<?php
2
3
namespace Nip\Helpers\View;
4
5
/**
6
 * Class Scripts
7
 * @package Nip\Helpers\View
8
 */
9
class Scripts extends AbstractHelper
10
{
11
12
    /**
13
     * @var array
14
     */
15
    protected $files = [];
16
17
    protected $rawScripts = [];
18
19
    protected $defaultPlaceholder = "head";
20
21
    /**
22
     * @param $file
23
     * @param bool $placeholder
24
     * @return $this
25
     */
26
    public function add($file, $placeholder = false)
27
    {
28
        return $this->addFile($file, 'add', $placeholder);
29
    }
30
31
    /**
32
     * @param $file
33
     * @param string $direction
34
     * @param bool $placeholder
35
     * @return $this
36
     */
37
    public function addFile($file, $direction = 'add', $placeholder = false)
38
    {
39
        if ($placeholder === false || empty($placeholder)) {
40
            $placeholder = $this->defaultPlaceholder;
41
        }
42
43 View Code Duplication
        if (!isset($this->files[$placeholder]) || !is_array($this->files[$placeholder])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
44
            $this->files[$placeholder] = [];
45
        }
46
47
        if ($direction == 'prepend') {
48
            array_unshift($this->files[$placeholder], $file);
49
        } else {
50
            $this->files[$placeholder][] = $file;
51
        }
52
53
        return $this;
54
    }
55
56
    /**
57
     * @param $content
58
     * @return $this
59
     */
60
    public function addRaw($content)
61
    {
62
        $this->rawScripts[] = $content;
63
64
        return $this;
65
    }
66
67
    /**
68
     * @param $file
69
     * @param bool $placeholder
70
     * @return Scripts
71
     */
72
    public function prepend($file, $placeholder = false)
73
    {
74
        return $this->addFile($file, 'prepend', $placeholder);
75
    }
76
77
    /**
78
     * @return string
79
     */
80
    public function __toString()
81
    {
82
        return $this->render($this->defaultPlaceholder);
0 ignored issues
show
Documentation introduced by
$this->defaultPlaceholder is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
83
    }
84
85
    /**
86
     * @param bool $placeholder
87
     * @return string
88
     */
89
    public function render($placeholder = false)
90
    {
91
        if ($placeholder == false && empty($placeholder)) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
92
            $placeholder = $this->defaultPlaceholder;
93
        }
94
95
        if (isset($this->files[$placeholder])) {
96
            return $this->renderHMTL($this->files[$placeholder]);
97
        }
98
        return '';
99
    }
100
101
    /**
102
     * @param $files
103
     * @return string
104
     */
105
    public function renderHMTL($files)
106
    {
107
        $return = '';
108
        if (is_array($files)) {
109
            $internal = [];
110
            $external = [];
111
112
            foreach ($files as $file) {
113
                if (preg_match('/https?:\/\//', $file)) {
114
                    $external[] = $file;
115
                } else {
116
                    $internal[] = $file;
117
                }
118
            }
119
120
121
            if (count($external)) {
122
                foreach ($external as $file) {
123
                    $return .= $this->buildTag($file);
124
                }
125
            }
126
127
            foreach ($internal as $file) {
128
                $return .= $this->buildTag($this->buildURL($file));
129
            }
130
        }
131
132
        return $return;
133
    }
134
135
    /**
136
     * @param $path
137
     * @return string
138
     */
139
    public function buildTag($path)
140
    {
141
        return "<script type=\"text/javascript\" src=\"$path\"></script>\r\n";
142
    }
143
144
    /**
145
     * @param $source
146
     * @return string
147
     */
148
    public function buildURL($source)
149
    {
150
        return asset('/scripts/' . $source . '.js');
151
    }
152
153
    /**
154
     * @return string
155
     */
156
    public function renderRaw()
157
    {
158
        $return = '';
159
        if (count($this->rawScripts)) {
160
            $return .= '<script type="text/javascript">';
161
            $return .= implode("\r\n", $this->rawScripts);
162
            $return .= '</script>';
163
        }
164
        return $return;
165
    }
166
}
167