Completed
Push — master ( 579af5...b29473 )
by Oleg
07:53
created

Asset::publish()   D

Complexity

Conditions 10
Paths 45

Size

Total Lines 28
Code Lines 16

Duplication

Lines 16
Ratio 57.14 %
Metric Value
dl 16
loc 28
rs 4.8196
cc 10
eloc 16
nc 45
nop 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php /** MicroAsset */
2
3
namespace Micro\Web;
4
5
use Micro\Base\Autoload;
6
use Micro\Base\Exception;
7
use Micro\File\FileHelper;
8
use Micro\Mvc\Views\IView;
9
10
/**
11
 * Asset class file.
12
 *
13
 * @author Oleg Lunegov <[email protected]>
14
 * @link https://github.com/lugnsk/micro
15
 * @copyright Copyright &copy; 2013 Oleg Lunegov
16
 * @license /LICENSE
17
 * @package Micro
18
 * @subpackage Web
19
 * @version 1.0
20
 * @since 1.0
21
 */
22
class Asset
23
{
24
    /** @var string $sourcePath Full-path to source asset dir */
25
    public $sourcePath;
26
27
    /** @var bool $isHead Is a publish into head block */
28
    public $isHead = true;
29
    /** @var array $js JavaScript files links */
30
    public $js = [];
31
    /** @var array $css CSS files links */
32
    public $css = [];
33
    /** @var array $required Required assets */
34
    public $required = [];
35
    /** @var array $excludes Excludes extensions */
36
    public $excludes = [];
37
38
    /** @var IView $view View for install current asset */
39
    protected $view;
40
    /** @var string $hash Unique directory to publish into assets dir */
41
    protected $hash;
42
    /** @var string $publishPath Publish path */
43
    protected $publishPath;
44
    /** @var array $published Published required extends */
45
    private $published = [];
46
47
48
    /**
49
     * Constructor asset
50
     *
51
     * @access public
52
     *
53
     * @param IView $view
54
     *
55
     * @result void
56
     * @throws \Micro\Base\Exception
57
     */
58
    public function __construct(IView $view)
59
    {
60
        $this->view = $view;
61
62
        if (!$this->sourcePath) {
63
            $this->sourcePath = dirname(Autoload::getClassPath(get_class($this)));
64
        }
65
66
        $this->hash = md5($this->sourcePath);
67
68
        $this->publishPath = '/' . (($dir = $view->container->assetsDirName) ? $dir : 'assets') . '/' . $this->hash;
0 ignored issues
show
Bug introduced by
Accessing container on the interface Micro\Mvc\Views\IView suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
69
70
        $web = $this->view->container->kernel->getWebDir();
0 ignored issues
show
Bug introduced by
Accessing container on the interface Micro\Mvc\Views\IView suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
71
72
        if (!file_exists($this->sourcePath)) {
73
            throw new Exception('Asset dir not exists: ' . $this->sourcePath);
74
        }
75
76
        if (!@mkdir($web . $this->publishPath, 0777) && !is_dir($web . $this->publishPath)) {
77
            throw new Exception('Could not access to publish dir: ' . $this->publishPath);
78
        }
79
80
        FileHelper::recurseCopyIfEdited($this->sourcePath, $web . $this->publishPath, $this->excludes);
81
    }
82
83
    /**
84
     * Send asset into view
85
     *
86
     * @access public
87
     * @return void
88
     */
89
    public function publish()
90
    {
91
        foreach ($this->required AS $require) {
92
            if (!in_array($require, $this->published, true) && class_exists($require)) {
93
                $this->published[] = $require;
94
                /** @var Asset $require */
95
                $require = new $require($this->view);
96
                $require->publish();
97
            }
98
        }
99
100 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...
101
            if (is_string($this->js)) {
102
                $this->js = [$this->js];
103
            }
104
            foreach ($this->js AS $script) {
105
                $this->view->registerScriptFile($this->publishPath . $script, $this->isHead);
106
            }
107
        }
108 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...
109
            if (is_string($this->css)) {
110
                $this->css = [$this->css];
111
            }
112
            foreach ($this->css AS $style) {
113
                $this->view->registerCssFile($this->publishPath . $style, $this->isHead);
114
            }
115
        }
116
    }
117
}
118