Asset   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 106
Duplicated Lines 22.64 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 19
c 2
b 0
f 0
lcom 1
cbo 7
dl 24
loc 106
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
C __construct() 0 24 7
C publish() 24 38 12

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 /** MicroAsset */
2
3
namespace Micro\Web;
4
5
use Micro\Auth\Injector;
6
use Micro\Base\Autoload;
7
use Micro\Base\Exception;
8
use Micro\Base\KernelInjector;
9
use Micro\File\FileHelper;
10
use Micro\Mvc\Views\IView;
11
12
/**
13
 * Asset class file.
14
 *
15
 * @author Oleg Lunegov <[email protected]>
16
 * @link https://github.com/linpax/microphp-framework
17
 * @copyright Copyright (c) 2013 Oleg Lunegov
18
 * @license https://github.com/linpax/microphp-framework/blob/master/LICENSE
19
 * @package Micro
20
 * @subpackage Web
21
 * @version 1.0
22
 * @since 1.0
23
 */
24
class Asset
25
{
26
    /** @var string $sourcePath Full-path to source asset dir */
27
    public $sourcePath;
28
29
    /** @var bool $isHead Is a publish into head block */
30
    public $isHead = true;
31
    /** @var array $js JavaScript files links */
32
    public $js = [];
33
    /** @var array $css CSS files links */
34
    public $css = [];
35
    /** @var array $required Required assets */
36
    public $required = [];
37
    /** @var array $excludes Excludes extensions */
38
    public $excludes = [];
39
40
    /** @var IView $view View for install current asset */
41
    protected $view;
42
    /** @var string $hash Unique directory to publish into assets dir */
43
    protected $hash;
44
    /** @var string $publishPath Publish path */
45
    protected $publishPath;
46
    /** @var array $published Published required extends */
47
    private $published = [];
48
49
50
    /**
51
     * Constructor asset
52
     *
53
     * @access public
54
     *
55
     * @param IView $view
56
     *
57
     * @result void
58
     * @throws \Micro\Base\Exception
59
     */
60
    public function __construct(IView $view)
61
    {
62
        $this->view = $view;
63
64
        if (!$this->sourcePath) {
65
            $this->sourcePath = dirname(Autoload::getClassPath(get_class($this)));
66
        }
67
68
        $this->hash = md5($this->sourcePath);
69
70
        $this->publishPath = '/' . (($dir = (new Injector)->param('assetsDirName')) ? $dir : 'assets') . '/' . $this->hash;
71
72
        $web = (new KernelInjector)->build()->getWebDir();
73
74
        if (!file_exists($this->sourcePath)) {
75
            throw new Exception('Asset dir not exists: '.$this->sourcePath);
76
        }
77
78
        if (!is_dir($web.$this->publishPath) && (!mkdir($web.$this->publishPath, 0777) && !is_dir($web.$this->publishPath))) {
79
            throw new Exception('Could not access to publish dir: '.$this->publishPath);
80
        }
81
82
        FileHelper::recurseCopyIfEdited($this->sourcePath, $web.$this->publishPath, $this->excludes);
83
    }
84
85
    /**
86
     * Send asset into view
87
     *
88
     * @access public
89
     * @return void
90
     */
91
    public function publish()
92
    {
93
        foreach ($this->required AS $require) {
94
            if (!in_array($require, $this->published, true) && class_exists($require)) {
95
                $this->published[] = $require;
96
97
                /** @var Asset $require */
98
                $require = new $require($this->view);
99
                $require->publish();
100
            }
101
        }
102
103 View Code Duplication
        if ($this->js) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->js of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
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...
104
            if (is_string($this->js)) {
105
                $this->js = [$this->js];
106
            }
107
108
            if (0 !== count($this->js)) {
109
                /** @noinspection ForeachSourceInspection */
110
                foreach ($this->js AS $script) {
111
                    $this->view->registerScriptFile($this->publishPath.$script, $this->isHead);
112
                }
113
            }
114
        }
115
116 View Code Duplication
        if ($this->css) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->css of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
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...
117
            if (is_string($this->css)) {
118
                $this->css = [$this->css];
119
            }
120
121
            if (0 !== count($this->css)) {
122
                /** @noinspection ForeachSourceInspection */
123
                foreach ($this->css AS $style) {
124
                    $this->view->registerCssFile($this->publishPath.$style, $this->isHead);
125
                }
126
            }
127
        }
128
    }
129
}
130