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.
Completed
Push — master ( f91a4c...f91a4c )
by Dave
39:26 queued 34:22
created

Assets::insertOn()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 0
cts 3
cp 0
rs 8.439
c 0
b 0
f 0
cc 6
eloc 14
nc 9
nop 3
crap 42
1
<?php
2
3
namespace SleepingOwl\Admin\Templates;
4
5
use KodiCMS\Assets\Html;
6
use KodiCMS\Assets\AssetElement;
7
use Illuminate\Support\Collection;
8
use KodiCMS\Assets\Assets as BaseAssets;
9
use KodiCMS\Assets\Contracts\AssetElementInterface;
10
use SleepingOwl\Admin\Contracts\Template\AssetsInterface as AssetsContract;
11
12
class Assets extends BaseAssets implements AssetsContract
13
{
14
    /**
15
     * @var array
16
     */
17
    protected $globalVars = [];
18
19
    /**
20
     * Gets or sets javascript assets.
21
     *
22
     * @param bool|string $handle
23
     * @param string $src Asset source
24
     * @param array|string $dependency Dependencies
25
     * @param bool $footer Whether to show in header or footer
26
     *
27 285
     * @return AssetElementInterface Setting returns asset array, getting returns asset HTML
28
     */
29 285
    public function addJs($handle = false, $src = null, $dependency = null, $footer = true)
30
    {
31
        return parent::addJs($handle, $src, $dependency, $footer);
32
    }
33
34
    /**
35
     * @param AssetElementInterface[] $assets
36
     * @return array|bool|static
37
     */
38
    protected function sort($assets)
39
    {
40
        $mainAssets = collect($assets)->filter(function (AssetElement $item) {
41
            return ! array_filter($item->getDependency());
42
        });
43
44
        $depAssets = collect($assets)->filter(function (AssetElement $item) {
45
            return array_filter($item->getDependency());
46
        });
47
48
        foreach ($depAssets as $key => $asset) {
49
            $mainAssets = $this->insertOn($asset, $mainAssets, collect($assets));
50
        }
51
52
        if ($mainAssets->count()) {
53
            return $mainAssets;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $mainAssets; (Illuminate\Support\Collection) is incompatible with the return type of the parent method KodiCMS\Assets\Assets::sort of type KodiCMS\Assets\Contracts\AssetElementInterface[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
54
        }
55
56
        return parent::sort($assets);
57
    }
58
59
    /**
60
     * @param AssetElement $asset
61
     * @param Collection $mainAssets
62
     * @param Collection $assets
63
     * @return Collection
64
     */
65
    protected function insertOn(AssetElement $asset, Collection &$mainAssets, Collection $assets)
66
    {
67
        $dependency = collect($asset->getDependency());
68
        $checkedDep = null;
69
        $hasNotDep = null;
70
71
        foreach ($dependency as $dep) {
72
            if (! $mainAssets->has($dep)) {
73
                $hasNotDep = $dep;
74
                break;
75
            }
76
77
            $checkedDep = $dep;
78
        }
79
80
        if ($hasNotDep && $assets->get($hasNotDep)) {
81
            return $this->insertOn($assets->get($hasNotDep), $mainAssets, $assets);
82
        }
83
84
        if ($checkedDep) {
85
            return $mainAssets = $this->insertAfter($checkedDep, $mainAssets, $asset->getHandle(), $asset);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression $mainAssets = $this->ins...->getHandle(), $asset); of type Illuminate\Support\Collection|false adds false to the return on line 85 which is incompatible with the return type documented by SleepingOwl\Admin\Templates\Assets::insertOn of type Illuminate\Support\Collection. It seems like you forgot to handle an error condition.
Loading history...
86
        }
87
88
        return $mainAssets;
89
    }
90
91
    /**
92
     * Inserts a new key/value before the key in the array.
93
     *
94
     * @param $key
95
     *   The key to insert before.
96
     * @param $array
97
     *   An array to insert in to.
98
     * @param $new_key
99
     *   The key to insert.
100
     * @param $new_value
101
     *   An value to insert.
102
     * @return array|bool The new array if the key exists, FALSE otherwise.
103
     * @see array_insert_after()
104
     */
105 View Code Duplication
    protected function insertBefore($key, &$array, $new_key, $new_value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
106
    {
107
        if (array_key_exists($key, $array)) {
108
            $new = [];
109
            foreach ($array as $k => $value) {
110
                if ($k === $key) {
111
                    $new[$new_key] = $new_value;
112
                }
113
                $new[$k] = $value;
114
            }
115
116
            return $new;
117
        }
118
119
        return false;
120
    }
121
122
    /**
123
     * Inserts a new key/value after the key in the array.
124
     *
125
     * @param $key
126
     *   The key to insert after.
127
     * @param $array
128
     *   An array to insert in to.
129
     * @param $new_key
130
     *   The key to insert.
131
     * @param $new_value
132
     *   An value to insert.
133
     * @return Collection|bool The new array if the key exists, FALSE otherwise.
134
     */
135 View Code Duplication
    protected function insertAfter($key, Collection &$array, $new_key, $new_value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
136
    {
137
        if ($array->has($key)) {
138
            $new = [];
139
            foreach ($array as $k => $value) {
140
                $new[$k] = $value;
141
                if ($k === $key) {
142
                    $new[$new_key] = $new_value;
143
                }
144
            }
145
146
            return collect($new);
147
        }
148
149
        return false;
150
    }
151
152
    /**
153
     * Добавление глобальной переменной.
154
     *
155
     * @param string $key
156
     * @param mixed $value
157
     *
158
     * @return self
159
     */
160
    public function putGlobalVar($key, $value)
161
    {
162
        $this->globalVars[$key] = $value;
163
164
        return $this;
165
    }
166
167
    /**
168
     * @return array
169
     */
170
    public function globalVars()
171
    {
172
        return $this->globalVars;
173
    }
174
175
    /**
176
     * @return string
177
     */
178
    public function render()
179
    {
180
        return $this->renderGlobalVars().PHP_EOL.parent::render();
181
    }
182
183
    /**
184
     * @return string
185
     */
186
    public function renderGlobalVars()
187
    {
188
        $json = json_encode($this->globalVars);
189
190
        return (new Html())->vars("{$this->namespace}.GlobalConfig = {$json};");
191
    }
192
}
193