Complex classes like Gzip often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Gzip, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class Gzip |
||
27 | { |
||
28 | const REGEX_IMAGE_FORMAT = '/url\([\'"]*(.+?\.)(gif|png|jpg|jpeg|otf|eot|ttf|woff|svg)[\'"]*\)*/msi'; |
||
29 | const CACHE_DIR = 'pH7_static/'; |
||
30 | const MAX_IMG_SIZE_BASE64_CONVERTOR = 24000; // 24KB |
||
31 | |||
32 | /** @var File */ |
||
33 | private $oFile; |
||
34 | |||
35 | /** @var HttpRequest */ |
||
36 | private $oHttpRequest; |
||
37 | |||
38 | /** @var string */ |
||
39 | private $sBase; |
||
40 | |||
41 | /** @var string */ |
||
42 | private $sBaseUrl; |
||
43 | |||
44 | /** @var string */ |
||
45 | private $sType; |
||
46 | |||
47 | /** @var string */ |
||
48 | private $sDir; |
||
49 | |||
50 | /** @var string */ |
||
51 | private $sFiles; |
||
52 | |||
53 | /** @var string */ |
||
54 | private $sContents; |
||
55 | |||
56 | /** @var string */ |
||
57 | private $sCacheDir; |
||
58 | |||
59 | /** @var array */ |
||
60 | private $aElements; |
||
61 | |||
62 | /** @var integer */ |
||
63 | private $iIfModified; |
||
64 | |||
65 | /** @var boolean */ |
||
66 | private $bCaching; |
||
67 | |||
68 | /** @var boolean */ |
||
69 | private $bCompressor; |
||
70 | |||
71 | /** @var boolean */ |
||
72 | private $bDataUri; |
||
73 | |||
74 | /** @var boolean */ |
||
75 | private $bGzipContent; |
||
76 | |||
77 | /** @var boolean */ |
||
78 | private $bIsGzip; |
||
79 | |||
80 | /** @var string|boolean */ |
||
81 | private $mEncoding; |
||
82 | |||
83 | public function __construct() |
||
84 | { |
||
85 | $this->oFile = new File; |
||
86 | $this->oHttpRequest = new HttpRequest; |
||
87 | |||
88 | $this->bCaching = (bool) Config::getInstance()->values['cache']['enable.static.cache']; |
||
89 | $this->bCompressor = (bool) Config::getInstance()->values['cache']['enable.static.minify']; |
||
90 | $this->bGzipContent = (bool) Config::getInstance()->values['cache']['enable.static.gzip_compress']; |
||
91 | $this->bDataUri = (bool) Config::getInstance()->values['cache']['enable.static.data_uri']; |
||
92 | |||
93 | $this->bIsGzip = $this->isGzip(); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Set cache directory. |
||
98 | * If the directory is not correct, the method will cause an exception. |
||
99 | * If you do not use this method, a default directory will be created. |
||
100 | * |
||
101 | * @param string $sCacheDir |
||
102 | * |
||
103 | * @return void |
||
104 | * |
||
105 | * @throws PH7InvalidArgumentException If the cache directory does not exist. |
||
106 | */ |
||
107 | public function setCacheDir($sCacheDir) |
||
108 | { |
||
109 | if (is_dir($sCacheDir)) |
||
110 | $this->sCacheDir = $sCacheDir; |
||
111 | else |
||
112 | throw new PH7InvalidArgumentException('"' . $sCacheDir . '" cache directory cannot be found!'); |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Displays compressed files. |
||
117 | * |
||
118 | * @return void |
||
119 | * |
||
120 | * @throws Exception If the cache file couldn't be written. |
||
121 | * |
||
122 | * @throws \PH7\Framework\File\Exception |
||
123 | */ |
||
124 | public function run() |
||
125 | { |
||
126 | // Determine the directory and type we should use |
||
127 | if (!$this->oHttpRequest->getExists('t') || ($this->oHttpRequest->get('t') !== |
||
128 | 'html' && $this->oHttpRequest->get('t') !== 'css' && $this->oHttpRequest->get('t') !== 'js')) |
||
129 | { |
||
130 | Http::setHeadersByCode(503); |
||
131 | exit('Invalid type file!'); |
||
132 | } |
||
133 | $this->sType = ($this->oHttpRequest->get('t') === 'js') ? 'javascript' : $this->oHttpRequest->get('t'); |
||
134 | |||
135 | // Directory |
||
136 | if (!$this->oHttpRequest->getExists('d')) |
||
137 | { |
||
138 | Http::setHeadersByCode(503); |
||
139 | exit('No directory specified!'); |
||
140 | } |
||
141 | |||
142 | $this->sDir = $this->oHttpRequest->get('d'); |
||
143 | $this->sBase = $this->oFile->checkExtDir(realpath($this->sDir)); |
||
144 | $this->sBaseUrl = $this->clearUrl($this->oFile->checkExtDir($this->sDir)); |
||
145 | |||
146 | // The Files |
||
147 | if (!$this->oHttpRequest->getExists('f')) |
||
148 | { |
||
149 | Http::setHeadersByCode(503); |
||
150 | exit('No file specified!'); |
||
151 | } |
||
152 | |||
153 | $this->sFiles = $this->oHttpRequest->get('f'); |
||
154 | $this->aElements = explode(',', $this->sFiles); |
||
155 | |||
156 | foreach ($this->aElements as $sElement) |
||
157 | { |
||
158 | $sPath = realpath($this->sBase . $sElement); |
||
159 | |||
160 | if (($this->sType == 'html' && substr($sPath, -5) != '.html') || ($this-> |
||
161 | _sType == 'javascript' && substr($sPath, -3) != '.js') || ($this->sType == 'css' && substr($sPath, -4) != '.css')) |
||
162 | { |
||
163 | Http::setHeadersByCode(403); |
||
164 | exit('Error file extension.'); |
||
165 | } |
||
166 | |||
167 | if (substr($sPath, 0, strlen($this->sBase)) != $this->sBase || !is_file($sPath)) { |
||
168 | Http::setHeadersByCode(404); |
||
169 | exit('The file not found!'); |
||
170 | } |
||
171 | } |
||
172 | |||
173 | $this->setHeaders(); |
||
174 | |||
175 | // If the cache is enabled, reads cache and displays, otherwise reads and displays the contents. |
||
176 | $this->bCaching ? $this->cache() : $this->getContents(); |
||
177 | |||
178 | echo $this->sContents; |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * Set Caching. |
||
183 | * |
||
184 | * @return string The cached contents. |
||
185 | * |
||
186 | * @throws Exception If the cache file couldn't be written. |
||
187 | * |
||
188 | * @throws \PH7\Framework\File\Exception If the file cannot be created. |
||
189 | */ |
||
190 | public function cache() |
||
236 | |||
237 | /** |
||
238 | * Routing for files compressing. |
||
239 | * |
||
240 | * @return void |
||
241 | */ |
||
242 | protected function makeCompress() |
||
267 | |||
268 | /** |
||
269 | * Transform the contents into a gzip compressed string. |
||
270 | * |
||
271 | * @return void |
||
272 | */ |
||
273 | protected function gzipContent() |
||
277 | |||
278 | /** |
||
279 | * Get contents of the files. |
||
280 | * |
||
281 | * @return void |
||
282 | */ |
||
283 | protected function getContents() |
||
309 | |||
310 | /** |
||
311 | * @return void |
||
312 | */ |
||
313 | protected function setHeaders() |
||
323 | |||
324 | /** |
||
325 | * Check if gzip is activate. |
||
326 | * |
||
327 | * @return boolean Returns FALSE if compression is disabled or is not valid, otherwise returns TRUE |
||
328 | */ |
||
329 | protected function isGzip() |
||
334 | |||
335 | /** |
||
336 | * Parser the CSS/JS variables in cascading style sheets and JavaScript files. |
||
337 | * |
||
338 | * @return void |
||
339 | */ |
||
340 | protected function parseVariable() |
||
354 | |||
355 | /** |
||
356 | * @return void |
||
357 | */ |
||
358 | protected function getSubCssFile() |
||
368 | |||
369 | /** |
||
370 | * @return void |
||
371 | */ |
||
372 | protected function getSubJsFile() |
||
383 | |||
384 | /** |
||
385 | * Get the images into the CSS files. |
||
386 | * |
||
387 | * @return void |
||
388 | */ |
||
389 | private function getImageIntoCss() |
||
405 | |||
406 | /** |
||
407 | * Set CSS/JS variables. |
||
408 | * |
||
409 | * @param array $aVars Variable names containing the values. |
||
410 | * |
||
411 | * @return void |
||
412 | */ |
||
413 | private function setVariables(array $aVars) |
||
419 | |||
420 | /** |
||
421 | * Checks if the cache directory has been defined otherwise we create a default directory. |
||
422 | * If the directory cache does not exist, it creates a directory. |
||
423 | * |
||
424 | * @return void |
||
425 | */ |
||
426 | private function checkCacheDir() |
||
430 | |||
431 | /** |
||
432 | * Remove backslashes on Windows. |
||
433 | * |
||
434 | * @param string $sPath |
||
435 | * |
||
436 | * @return string The path without backslashes and/or double slashes. |
||
437 | */ |
||
438 | private function clearUrl($sPath) |
||
442 | } |
||
443 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.