Conditions | 22 |
Paths | 756 |
Total Lines | 76 |
Code Lines | 55 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
45 | public static function minify($html, $js = true, $css = true){ |
||
46 | $doc = new \DOMDocument(); |
||
47 | $doc->preserveWhiteSpace = false; |
||
48 | @$doc->loadHTML($html); |
||
|
|||
49 | $xpath = new \DOMXPath($doc); |
||
50 | foreach ($xpath->query('//comment()') as $comment) { |
||
51 | $val= $comment->nodeValue; |
||
52 | if( strpos($val,'[')!==0){ |
||
53 | $comment->parentNode->removeChild($comment); |
||
54 | } |
||
55 | } |
||
56 | $doc->normalizeDocument(); |
||
57 | $textnodes = $xpath->query('//text()'); |
||
58 | $skip = ["style","pre","code","script","textarea"]; |
||
59 | foreach($textnodes as $t){ |
||
60 | $xp = $t->getNodePath(); |
||
61 | $doskip = false; |
||
62 | foreach($skip as $pattern){ |
||
63 | if(strpos($xp,"/$pattern")!==false){ |
||
64 | $doskip = true; |
||
65 | break; |
||
66 | } |
||
67 | } |
||
68 | if($doskip){ |
||
69 | continue; |
||
70 | } |
||
71 | $t->nodeValue = preg_replace("/\s{2,}/", " ", $t->nodeValue); |
||
72 | } |
||
73 | $doc->normalizeDocument(); |
||
74 | $divnodes = $xpath->query('//div|//p|//nav|//footer|//article|//script|//hr|//br'); |
||
75 | foreach($divnodes as $d){ |
||
76 | $candidates = []; |
||
77 | if(count($d->childNodes)){ |
||
78 | $candidates[] = $d->firstChild; |
||
79 | $candidates[] = $d->lastChild; |
||
80 | $candidates[] = $d->previousSibling; |
||
81 | $candidates[] = $d->nextSibling; |
||
82 | } |
||
83 | foreach($candidates as $c){ |
||
84 | if($c==null){ |
||
85 | continue; |
||
86 | } |
||
87 | if($c->nodeType==3){ |
||
88 | $c->nodeValue = trim($c->nodeValue); |
||
89 | } |
||
90 | } |
||
91 | } |
||
92 | $doc->normalizeDocument(); |
||
93 | if($js){ |
||
94 | $scriptnodes = $xpath->query('//script'); |
||
95 | foreach($scriptnodes as $d){ |
||
96 | if($d->hasAttribute("type") && strtolower($d->getAttribute("type"))!=='text/javascript' ){ |
||
97 | continue; |
||
98 | } |
||
99 | if($d->hasAttribute("data-no-min")){ |
||
100 | continue; |
||
101 | } |
||
102 | if(trim($d->nodeValue)==""){ |
||
103 | continue; |
||
104 | } |
||
105 | $d->nodeValue = JSMin::minify( $d->nodeValue); |
||
106 | } |
||
107 | } |
||
108 | if($css){ |
||
109 | $cssnodes = $xpath->query('//style'); |
||
110 | foreach($cssnodes as $d){ |
||
111 | if($d->hasAttribute("data-no-min")){ |
||
112 | continue; |
||
113 | } |
||
114 | if(trim($d->nodeValue)==""){ |
||
115 | continue; |
||
116 | } |
||
117 | $d->nodeValue = CssMin::minify( $d->nodeValue); |
||
118 | } |
||
119 | } |
||
120 | return ($doc->saveHTML()); |
||
121 | } |
||
122 | } |
If you suppress an error, we recommend checking for the error condition explicitly: