Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Assets 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 Assets, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
37 | class Assets |
||
38 | { |
||
39 | /** |
||
40 | * @var boolean |
||
41 | */ |
||
42 | private $debug = false; |
||
43 | |||
44 | /** |
||
45 | * @var array of default filter strings - may be overridden by prefs |
||
46 | */ |
||
47 | private $default_filters = array( |
||
48 | 'css' => 'cssimport,cssembed,?cssmin', |
||
49 | 'js' => '?jsqueeze', |
||
50 | ); |
||
51 | |||
52 | /** |
||
53 | * @var array of output locations in assets directory |
||
54 | */ |
||
55 | private $default_output = array( |
||
56 | 'css' => 'css/*.css', |
||
57 | 'js' => 'js/*.js', |
||
58 | ); |
||
59 | |||
60 | /** |
||
61 | * @var array of asset reference definitions - may be overridden by prefs |
||
62 | */ |
||
63 | private $default_asset_refs = array( |
||
64 | array( |
||
65 | 'name' => 'jquery', |
||
66 | 'assets' => array('media/jquery/jquery.js'), |
||
67 | 'filters' => null, |
||
68 | ), |
||
69 | array( |
||
70 | 'name' => 'jqueryui', |
||
71 | 'assets' => array('media/jquery/ui/jquery-ui.js'), |
||
72 | 'filters' => null, |
||
73 | ), |
||
74 | array( |
||
75 | 'name' => 'jgrowl', |
||
76 | 'assets' => array('media/jquery/plugins/jquery.jgrowl.js'), |
||
77 | 'filters' => null, |
||
78 | ), |
||
79 | ); |
||
80 | |||
81 | /** |
||
82 | * @var AssetManager |
||
83 | */ |
||
84 | private $assetManager = null; |
||
85 | |||
86 | /** |
||
87 | * @var string config file with assets prefs |
||
88 | */ |
||
89 | private $assetsPrefsFilename = 'var/configs/system_assets_prefs.yml'; |
||
90 | |||
91 | /** |
||
92 | * @var string config cache key |
||
93 | */ |
||
94 | private $assetsPrefsCacheKey = 'system/assets/prefs'; |
||
95 | |||
96 | /** |
||
97 | * @var string string to identify Assetic filters using instanceof |
||
98 | */ |
||
99 | |||
100 | private $filterInterface = '\Assetic\Filter\FilterInterface'; |
||
101 | /** |
||
102 | * __construct |
||
103 | */ |
||
104 | 3 | public function __construct() |
|
116 | |||
117 | /** |
||
118 | * readAssetsPrefs - read configured asset preferences |
||
119 | * |
||
120 | * @return array of assets preferences |
||
121 | */ |
||
122 | 3 | protected function readAssetsPrefs() |
|
165 | |||
166 | /** |
||
167 | * saveAssetsPrefs - record array of assets preferences in config file, and |
||
168 | * update cache |
||
169 | * |
||
170 | * @param array $assets_prefs array of asset preferences to save |
||
171 | * |
||
172 | * @return void |
||
173 | */ |
||
174 | 1 | View Code Duplication | protected function saveAssetsPrefs($assets_prefs) |
186 | |||
187 | |||
188 | /** |
||
189 | * getUrlToAssets |
||
190 | * |
||
191 | * Create an asset file from a list of assets |
||
192 | * |
||
193 | * @param string $type type of asset, css or js |
||
194 | * @param array $assets list of source files to process |
||
195 | * @param string|array $filters either a comma separated list of known namsed filters |
||
196 | * or an array of named filters and/or filter object |
||
197 | * @param string $target target path, will default to assets directory |
||
198 | * |
||
199 | * @return string URL to asset file |
||
200 | */ |
||
201 | 1 | public function getUrlToAssets($type, $assets, $filters = 'default', $target = null) |
|
202 | { |
||
203 | 1 | if (is_scalar($assets)) { |
|
204 | $assets = array($assets); // just a single path name |
||
205 | } |
||
206 | |||
207 | 1 | if ($filters==='default') { |
|
208 | 1 | if (isset($this->default_filters[$type])) { |
|
209 | 1 | $filters = $this->default_filters[$type]; |
|
210 | } else { |
||
211 | $filters = ''; |
||
212 | } |
||
213 | } |
||
214 | |||
215 | 1 | View Code Duplication | if (!is_array($filters)) { |
216 | 1 | if (empty($filters)) { |
|
217 | $filters = array(); |
||
218 | } else { |
||
219 | 1 | $filters = explode(',', str_replace(' ', '', $filters)); |
|
220 | } |
||
221 | } |
||
222 | |||
223 | 1 | if (isset($this->default_output[$type])) { |
|
224 | 1 | $output = $this->default_output[$type]; |
|
225 | } else { |
||
226 | $output = ''; |
||
227 | } |
||
228 | |||
229 | 1 | $xoops = \Xoops::getInstance(); |
|
230 | |||
231 | 1 | if (isset($target)) { |
|
232 | $target_path = $target; |
||
233 | } else { |
||
234 | 1 | $target_path = $xoops->path('assets'); |
|
235 | } |
||
236 | |||
237 | try { |
||
238 | 1 | $am = $this->assetManager; |
|
239 | 1 | $fm = new FilterManager(); |
|
240 | |||
241 | 1 | foreach ($filters as $filter) { |
|
242 | 1 | if (is_object($filter) && $filter instanceof $this->filterInterface) { |
|
243 | $filterArray[] = $filter; |
||
244 | } else { |
||
245 | 1 | switch (ltrim($filter, '?')) { |
|
246 | 1 | case 'cssembed': |
|
247 | $fm->set('cssembed', new Filter\PhpCssEmbedFilter()); |
||
248 | break; |
||
249 | 1 | case 'cssmin': |
|
250 | $fm->set('cssmin', new Filter\CssMinFilter()); |
||
251 | break; |
||
252 | 1 | case 'cssimport': |
|
253 | $fm->set('cssimport', new Filter\CssImportFilter()); |
||
254 | break; |
||
255 | 1 | case 'cssrewrite': |
|
256 | $fm->set('cssrewrite', new Filter\CssRewriteFilter()); |
||
257 | break; |
||
258 | 1 | case 'lessphp': |
|
259 | $fm->set('lessphp', new Filter\LessphpFilter()); |
||
260 | break; |
||
261 | 1 | case 'scssphp': |
|
262 | $fm->set('scssphp', new Filter\ScssphpFilter()); |
||
263 | break; |
||
264 | 1 | case 'jsmin': |
|
265 | $fm->set('jsmin', new Filter\JSMinFilter()); |
||
266 | break; |
||
267 | 1 | case 'jsqueeze': |
|
268 | 1 | $fm->set('jsqueeze', new Filter\JSqueezeFilter()); |
|
269 | 1 | break; |
|
270 | default: |
||
271 | throw new \Exception(sprintf('%s filter not implemented.', $filter)); |
||
272 | 1 | break; |
|
273 | } |
||
274 | } |
||
275 | } |
||
276 | |||
277 | // Factory setup |
||
278 | 1 | $factory = new AssetFactory($target_path); |
|
279 | 1 | $factory->setAssetManager($am); |
|
280 | 1 | $factory->setFilterManager($fm); |
|
281 | 1 | $factory->setDefaultOutput($output); |
|
282 | 1 | $factory->setDebug($this->debug); |
|
283 | 1 | $factory->addWorker(new CacheBustingWorker()); |
|
284 | |||
285 | // Prepare the assets writer |
||
286 | 1 | $writer = new AssetWriter($target_path); |
|
287 | |||
288 | // Translate asset paths, remove duplicates |
||
289 | 1 | $translated_assets = array(); |
|
290 | 1 | foreach ($assets as $k => $v) { |
|
291 | // translate path if not a reference or absolute path |
||
292 | 1 | if (0 == preg_match("/^\\/|^\\\\|^[a-zA-Z]:|^@/", $v)) { |
|
293 | 1 | $v = $xoops->path($v); |
|
294 | } |
||
295 | 1 | if (!in_array($v, $translated_assets)) { |
|
296 | 1 | $translated_assets[] = $v; |
|
297 | } |
||
298 | } |
||
299 | |||
300 | // Create the asset |
||
301 | 1 | $asset = $factory->createAsset( |
|
302 | $translated_assets, |
||
303 | $filters |
||
304 | ); |
||
305 | 1 | $asset_path = $asset->getTargetPath(); |
|
306 | 1 | if (!is_readable($target_path . $asset_path)) { |
|
307 | $assetKey = 'Asset '.$asset_path; |
||
308 | $xoops->events()->triggerEvent('debug.timer.start', $assetKey); |
||
309 | $oldumask = umask(0002); |
||
310 | $writer->writeAsset($asset); |
||
311 | umask($oldumask); |
||
312 | $xoops->events()->triggerEvent('debug.timer.stop', $assetKey); |
||
313 | } |
||
314 | |||
315 | 1 | return $xoops->url('assets/' . $asset_path); |
|
316 | |||
317 | } catch (\Exception $e) { |
||
318 | $xoops->events()->triggerEvent('core.exception', $e); |
||
319 | return null; |
||
320 | } |
||
321 | } |
||
322 | |||
323 | |||
324 | /** |
||
325 | * setDebug enable debug mode, will skip filters prefixed with '?' |
||
326 | * |
||
327 | * @return true |
||
328 | */ |
||
329 | 1 | public function setDebug() |
|
334 | |||
335 | /** |
||
336 | * Add an asset reference to the asset manager |
||
337 | * |
||
338 | * @param string $name the name of the reference to be added |
||
339 | * @param mixed $assets a string asset path, or an array of asset paths, |
||
340 | * may include wildcard |
||
341 | * @param string|array $filters either a comma separated list of known named filters |
||
342 | * or an array of named filters and/or filter object |
||
343 | * |
||
344 | * @return boolean true if asset registers, false on error |
||
345 | */ |
||
346 | 3 | public function registerAssetReference($name, $assets, $filters = null) |
|
422 | |||
423 | /** |
||
424 | * copyFileAssets - copy files to the appropriate asset directory. |
||
425 | * |
||
426 | * Copying is normally only needed for fonts or images when they are referenced by a |
||
427 | * relative url in stylesheet, or are located outside of the web root. |
||
428 | * |
||
429 | * @param string $from_path path to files to copy |
||
430 | * @param string $pattern glob pattern to match files to be copied |
||
431 | * @param string $output output type (css, fonts, images, js) |
||
432 | * |
||
433 | * @return mixed boolean false if target directory is not writable, otherwise |
||
434 | * integer count of files copied |
||
435 | */ |
||
436 | 1 | public function copyFileAssets($from_path, $pattern, $output) |
|
470 | } |
||
471 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: