Completed
Push — master ( bd4222...e5ba92 )
by Arjay
13:31
created

AssetServiceProvider   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 88
Duplicated Lines 18.18 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 14
Bugs 0 Features 4
Metric Value
c 14
b 0
f 4
dl 16
loc 88
rs 10
wmc 9
lcom 1
cbo 2

7 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 3 1
A register() 0 8 1
A addAdminAssets() 0 6 2
A requireAdminDefaultAssets() 0 6 2
A assetJs() 8 8 1
A assetCss() 8 8 1
A strParser() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Yajra\CMS\Providers;
4
5
use Illuminate\Support\Facades\Blade;
6
use Illuminate\Support\ServiceProvider;
7
use Roumen\Asset\Asset;
8
9
/**
10
 * Class AssetServiceProvider
11
 *
12
 * @package Yajra\CMS\Providers
13
 */
14
class AssetServiceProvider extends ServiceProvider
15
{
16
    /**
17
     * Bootstrap the application services.
18
     *
19
     * @return void
20
     */
21
    public function boot()
22
    {
23
    }
24
25
    /**
26
     * Register the application services.
27
     *
28
     * @return void
29
     */
30
    public function register()
31
    {
32
        $siteAssets = config('site.assets.' . config('site.assets.default'));
33
        $this->addAdminAssets($siteAssets);
34
        $this->requireAdminDefaultAssets();
35
        $this->assetJs($siteAssets);
36
        $this->assetCss($siteAssets);
37
    }
38
39
    /**
40
     * Add admin assets.
41
     *
42
     * @param array $siteAssets
43
     */
44
    protected function addAdminAssets($siteAssets)
45
    {
46
        foreach (config('site.admin_assets', []) as $asset => $value) {
47
            Asset::add(array_get($siteAssets, $value));
48
        }
49
    }
50
51
    /**
52
     * Add require admin default assets.
53
     */
54
    protected function requireAdminDefaultAssets()
55
    {
56
        foreach (config('site.admin_required_assets', []) as $asset => $requiredValue) {
57
            Asset::add($requiredValue);
58
        }
59
    }
60
61
    /**
62
     * Generate Javascript assets.
63
     *
64
     * @param string $siteAssets
65
     * @return string
66
     */
67 View Code Duplication
    protected function assetJs($siteAssets)
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...
68
    {
69
        Blade::directive('assetJs', function ($asset) use ($siteAssets) {
70
            $setAsset = $this->strParser($asset . '.js');
71
72
            return '<?php echo "<script src=\"' . array_get($siteAssets, $setAsset) . '\"></script>"; ?>';
0 ignored issues
show
Documentation introduced by
$siteAssets is of type string, but the function expects a object<ArrayAccess>|array.

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...
73
        });
74
    }
75
76
    /**
77
     * Generate CSS assets.
78
     *
79
     * @param string $siteAssets
80
     * @return string
81
     */
82 View Code Duplication
    protected function assetCss($siteAssets)
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...
83
    {
84
        Blade::directive('assetCss', function ($asset) use ($siteAssets) {
85
            $setAsset = $this->strParser($asset . '.css');
86
87
            return '<link rel=\"stylesheet\" type=\"text/css\" href=\"' . array_get($siteAssets, $setAsset) . '\">';
0 ignored issues
show
Documentation introduced by
$siteAssets is of type string, but the function expects a object<ArrayAccess>|array.

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...
88
        });
89
    }
90
91
    /**
92
     * @param string $asset
93
     * @return string
94
     */
95
    private function strParser($asset)
96
    {
97
        $replaceBrackets = str_replace(['(', ')'], '', $asset);
98
99
        return str_replace("'", '', $replaceBrackets);
100
    }
101
}
102