Completed
Push — master ( 02b122...2a8558 )
by James Ekow Abaka
02:43
created

MinifiablesHelper   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 5
Dependencies 3

Test Coverage

Coverage 91.49%

Importance

Changes 0
Metric Value
dl 0
loc 108
ccs 43
cts 47
cp 0.9149
rs 10
c 0
b 0
f 0
wmc 21
lcom 5
cbo 3

12 Methods

Rating   Name   Duplication   Size   Complexity  
getExtension() 0 1 ?
getMinifier() 0 1 ?
getTag() 0 1 ?
A makePublic() 0 4 1
A context() 0 5 1
A combine() 0 5 1
A setContext() 0 4 1
A setCombine() 0 4 1
A writeTags() 0 9 2
B __toString() 0 26 4
B help() 0 16 5
B add() 0 15 5
1
<?php
2
namespace ntentan\honam\helpers;
3
4
use ntentan\honam\Helper;
5
use ntentan\honam\AssetsLoader;
6
use ntentan\honam\helpers\minifiables\Minifier;
7
8
abstract class MinifiablesHelper extends Helper
9
{
10
    private $minifiableScripts = array();
11
    private $otherScripts = array();
12
    private $context = 'default';
13
    private $combine = false;
14
15
    protected abstract function getExtension();
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
16
    protected abstract function getMinifier();
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
17
    protected abstract function getTag($url);
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
18
    
19 7
    private function makePublic($file)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
20
    {
21 7
        return $this->getTag(AssetsLoader::load($this->getExtension() . "/" . basename($file), $file));        
22
    }
23
    
24 11
    private function writeTags($files)
25
    {
26 11
        $tags = '';
27 11
        foreach($files as $script)
28
        {
29 7
            $tags .= $this->makePublic($script);
30
        }
31 9
        return $tags;
32
    }
33
    
34 11
    public function __toString()
35
    {
36 11
        $minifiedScript = '';
37 11
        $tags = '';
38 11
        $filename = "/" . $this->getExtension() . "/combined_{$this->context}." . $this->getExtension();
39 11
        if($this->combine === true)
40
        {
41 4
            foreach($this->minifiableScripts as $script)
42
            {
43 4
                $minifiedScript .= file_get_contents($script);
44
            }
45 4
            file_put_contents(AssetsLoader::getDestinationDir() . $filename, Minifier::minify($minifiedScript, $this->getMinifier()));
46 4
            $tags = $this->getTag(AssetsLoader::getSiteUrl() . $filename);
47
        }
48 7
        else if($this->combine == false)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
49
        {
50 7
            $tags .= $this->writeTags($this->minifiableScripts);
51
        }
52
        
53 9
        $tags .= $this->writeTags($this->otherScripts);
54
        
55 9
        $this->minifiableScripts = array();
56 9
        $this->otherScripts = array();
57
        
58 9
        return $tags;
59
    }
60
61 2
    public function help($arguments)
62
    {
63 2
        if(is_array($arguments))
64
        {
65 1
            foreach($arguments as $argument)
66
            {
67 1
                if($argument == '') continue;
68 1
                $this->otherScripts[]= $argument;
69
            }
70
        }
71 1
        else if($arguments != '')
72
        {
73 1
            $this->otherScripts[]= $arguments;
74
        }
75 2
        return $this;
76
    }
77
78 9
    public function add($script)
79
    {
80 9
        if($script != '' && is_string($script))
81
        { 
82 7
            $this->minifiableScripts[] = $script;
83
        }
84 2
        else if(is_array($script))
85
        {
86 2
            foreach($script as $scriptFile)
87
            {
88 2
                $this->minifiableScripts[] = $scriptFile;
89
            }
90
        }
91 9
        return $this;
92
    }
93
    
94
    public function setContext($context)
95
    {
96
        return $this->context($context);
97
    }
98
    
99
    public function setCombine($combine)
100
    {
101
        return $this->combine($combine);
102
    }
103
    
104 1
    public function context($context)
105
    {
106 1
        $this->context = $context;
107 1
        return $this;
108
    }
109
    
110 4
    public function combine($combine)
111
    {
112 4
        $this->combine = $combine;
113 4
        return $this;
114
    }    
115
}
116