| Conditions | 16 |
| Paths | 256 |
| Total Lines | 79 |
| Code Lines | 43 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 72 | private function htmlCompress($data, $options = null) |
||
| 73 | { |
||
| 74 | if (!isset($options)) { |
||
| 75 | $options = []; |
||
| 76 | } |
||
| 77 | |||
| 78 | $data .= "\n"; |
||
| 79 | $out = ''; |
||
| 80 | $inside_pre = false; |
||
| 81 | $inside_textarea = false; |
||
| 82 | $bytecount = 0; |
||
| 83 | |||
| 84 | while ($line = $this->getLine($data)) { |
||
| 85 | $bytecount += strlen($line); |
||
| 86 | |||
| 87 | if ($inside_pre) { |
||
| 88 | list($line, $inside_pre) = $this->checkInsidePre($line); |
||
| 89 | } elseif ($inside_textarea) { |
||
| 90 | list($line, $inside_textarea) = $this->checkInsideTextarea($line); |
||
| 91 | } else { |
||
| 92 | if (strpos($line, '<pre') !== false) { |
||
| 93 | // Only trim the beginning since we just entered a <pre> block... |
||
| 94 | $line = ltrim($line); |
||
| 95 | |||
| 96 | // If the <pre> ends on the same line, don't turn on $inside_pre... |
||
| 97 | list($line, $inside_pre) = $this->checkInsidePre($line); |
||
| 98 | } elseif (strpos($line, '<textarea') !== false) { |
||
| 99 | // Only trim the beginning since we just entered a <textarea> block... |
||
| 100 | $line = ltrim($line); |
||
| 101 | |||
| 102 | // If the <textarea> ends on the same line, don't turn on $inside_textarea... |
||
| 103 | list($line, $inside_textarea) = $this->checkInsideTextarea($line); |
||
| 104 | } else { |
||
| 105 | // Since we're not inside a <pre> block, we can trim both ends of the line |
||
| 106 | $line = trim($line); |
||
| 107 | |||
| 108 | // And condense multiple spaces down to one |
||
| 109 | $line = preg_replace('/\s\s+/', ' ', $line); |
||
| 110 | } |
||
| 111 | } |
||
| 112 | |||
| 113 | // Filter out any blank lines that aren't inside a <pre> block... |
||
| 114 | if ($inside_pre || $inside_textarea) { |
||
| 115 | $out .= $line; |
||
| 116 | } elseif ($line != '') { |
||
| 117 | $out .= $line . "\n"; |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | // Perform any extra (unsafe) compression techniques... |
||
| 122 | if (array_key_exists('x', $options) || array_key_exists('extra', $options)) { |
||
| 123 | // Can break layouts that are dependent on whitespace between tags |
||
| 124 | $out = str_replace(">\n<", '><', $out); |
||
| 125 | } |
||
| 126 | |||
| 127 | // Remove HTML comments... |
||
| 128 | if (array_key_exists('c', $options) || array_key_exists('no-comments', $options)) { |
||
| 129 | // $out = preg_replace('/(<!--.*?-->)/ms', '', $out); |
||
|
|
|||
| 130 | // adding browser's condition's exceptions |
||
| 131 | $out = preg_replace('#\\s*<!--[^\\[].*?(?<!!)-->\\s*#s', '', $out); |
||
| 132 | $out = str_replace('<!>', '', $out); |
||
| 133 | } |
||
| 134 | |||
| 135 | // Remove the trailing \n |
||
| 136 | $out = trim($out); |
||
| 137 | |||
| 138 | // Output either our stats or the compressed data... |
||
| 139 | if (array_key_exists('s', $options) || array_key_exists('stats', $options)) { |
||
| 140 | $echo = ''; |
||
| 141 | $echo .= "Original Size: $bytecount\n"; |
||
| 142 | $echo .= "Compressed Size: " . strlen($out) . "\n"; |
||
| 143 | $echo .= "Savings: " . round((1 - strlen($out) / $bytecount) * 100, 2) . "%\n"; |
||
| 144 | echo $echo; |
||
| 145 | } else { |
||
| 146 | return $out; |
||
| 147 | } |
||
| 148 | |||
| 149 | return false; |
||
| 150 | } |
||
| 151 | |||
| 240 | } |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.