SwiperSliderAsset   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 0
loc 91
ccs 31
cts 31
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 6 1
A fromCdn() 0 5 1
A setupAssets() 0 16 3
A makePathAssets() 0 9 3
A register() 0 10 2
1
<?php
2
3
/**
4
 * @license BSD-3-Clause
5
 * @copyright Copyright (C) 2012-2020 Sergio coderius <coderius>
6
 * @contacts [email protected] - Have suggestions, contact me :)
7
 *
8
 * @see https://github.com/coderius - My github
9
 */
10
11
namespace coderius\swiperslider;
12
13
use Yii;
14
use yii\web\AssetBundle;
15
16
/**
17
 * Asset bundle SwiperSlider.
18
 */
19
class SwiperSliderAsset extends AssetBundle
20
{
21
    public $sourcePath = '@npm/swiper';
22
23
    /**
24
     * {@inheritdoc}
25
     */
26 18
    public function init()
27
    {
28 18
        $this->setupAssets('css', ['swiper-bundle']);
29 18
        $this->setupAssets('js', ['swiper-bundle']);
30 18
        parent::init();
31 18
    }
32
33
    /**
34
     * Create cdn url like base path to assets.
35
     *
36
     * @param string $cdn
37
     *
38
     * @return void
39
     */
40 6
    public function fromCdn($cdn)
41
    {
42 6
        $this->sourcePath = null;
43 6
        $this->baseUrl = $cdn;
44 6
    }
45
46
    /**
47
     * Set js or css props to AssetBundle.
48
     *
49
     * @see yii\web\AssetBundle
50
     *
51
     * @param string $ext
52
     * @param array  $paths
53
     *
54
     * @return void
55
     */
56 18
    public function setupAssets($ext, $paths)
57
    {
58 18
        $allowedExts = ['css', 'js'];
59
60 18
        if (!in_array($ext, $allowedExts)) {
61 3
            throw new \InvalidArgumentException("Extention {$ext} not allowed");
62
        }
63
64 18
        $fullFiles = [];
65 18
        $minFiles = [];
66
67 18
        $fullFiles[] = static::makePathAssets($ext, $paths);
68 18
        $minFiles[] = static::makePathAssets($ext, $paths, 'min');
69
70 18
        $this->$ext = YII_DEBUG ? $fullFiles : $minFiles;
71 18
    }
72
73
    /**
74
     * Make path for asset.
75
     *
76
     * @param string      $ext
77
     * @param array       $paths
78
     * @param bool|string $pref
79
     *
80
     * @return array
81
     */
82 21
    public static function makePathAssets($ext, $paths, $pref = false)
83
    {
84 21
        $p = [];
85 21
        foreach ($paths as $path) {
86 21
            $p[] = $pref ? "{$path}.{$pref}.{$ext}" : "{$path}.{$ext}";
87 7
        }
88
89 21
        return $p;
90
    }
91
92
    /**
93
     * Registers this asset bundle with a view.
94
     *
95
     * @param View $view the view to be registered with
96
     *
97
     * @return static the registered asset bundle instance
98
     */
99 18
    public static function register($view, $cdn = false)
100
    {
101 18
        $bundle = parent::register($view);
102
103 18
        if ($cdn) {
104 3
            $bundle->fromCdn($cdn);
0 ignored issues
show
Documentation introduced by
$cdn is of type boolean, but the function expects a string.

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...
105 1
        }
106
107 18
        return $bundle;
108
    }
109
}
110