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 | /** @var array */ |
||
40 | protected $assetPrefs; |
||
41 | |||
42 | /** @var boolean */ |
||
43 | private $debug = false; |
||
44 | |||
45 | /** @var array of default filter strings - may be overridden by prefs */ |
||
46 | private $default_filters = array( |
||
47 | 'css' => 'cssimport,cssembed,?cssmin', |
||
48 | 'js' => '?jsqueeze', |
||
49 | ); |
||
50 | |||
51 | /** @var array of output locations in assets directory */ |
||
52 | private $default_output = array( |
||
53 | 'css' => 'css/*.css', |
||
54 | 'js' => 'js/*.js', |
||
55 | ); |
||
56 | |||
57 | /** @var array of asset reference definitions - may be overridden by prefs */ |
||
58 | private $default_asset_refs = array( |
||
59 | array( |
||
60 | 'name' => 'jquery', |
||
61 | 'assets' => array('media/jquery/jquery.js'), |
||
62 | 'filters' => null, |
||
63 | ), |
||
64 | array( |
||
65 | 'name' => 'jqueryui', |
||
66 | 'assets' => array('media/jquery/ui/jquery-ui.js'), |
||
67 | 'filters' => null, |
||
68 | ), |
||
69 | array( |
||
70 | 'name' => 'jgrowl', |
||
71 | 'assets' => array('media/jquery/plugins/jquery.jgrowl.js'), |
||
72 | 'filters' => null, |
||
73 | ), |
||
74 | array( |
||
75 | 'name' => 'fontawesome', |
||
76 | 'assets' => array('media/font-awesome/css/font-awesome.min.css'), |
||
77 | 'filters' => null, |
||
78 | ), |
||
79 | ); |
||
80 | |||
81 | /** |
||
82 | * @var array of file assets to copy to assets |
||
83 | */ |
||
84 | private $default_file_assets = array( |
||
85 | array( |
||
86 | 'type' => 'fonts', |
||
87 | 'path' => 'media/font-awesome/fonts', |
||
88 | 'pattern' => '*', |
||
89 | ), |
||
90 | ); |
||
91 | |||
92 | |||
93 | /** @var AssetManager */ |
||
94 | private $assetManager = null; |
||
95 | |||
96 | /** @var string config file with assets prefs */ |
||
97 | private $assetsPrefsFilename = 'var/configs/system_assets_prefs.yml'; |
||
98 | |||
99 | /** @var string config cache key */ |
||
100 | private $assetsPrefsCacheKey = 'system/assets/prefs'; |
||
101 | |||
102 | /** @var string string to identify Assetic filters using instanceof */ |
||
103 | private $filterInterface = '\Assetic\Filter\FilterInterface'; |
||
104 | |||
105 | /** |
||
106 | * __construct |
||
107 | */ |
||
108 | 3 | public function __construct() |
|
109 | { |
||
110 | 3 | $this->assetManager = new AssetManager(); |
|
111 | 3 | if (isset($_REQUEST['ASSET_DEBUG'])) { |
|
112 | $this->setDebug(); |
||
113 | } |
||
114 | 3 | $this->assetPrefs = $this->readAssetsPrefs(); |
|
115 | // register any asset references |
||
116 | 3 | foreach ($this->default_asset_refs as $ref) { |
|
117 | 3 | $this->registerAssetReference($ref['name'], $ref['assets'], $ref['filters']); |
|
118 | } |
||
119 | 3 | } |
|
120 | |||
121 | /** |
||
122 | * readAssetsPrefs - read configured asset preferences |
||
123 | * |
||
124 | * @return array of assets preferences |
||
125 | */ |
||
126 | 3 | protected function readAssetsPrefs() |
|
127 | { |
||
128 | 3 | $xoops = \Xoops::getInstance(); |
|
129 | |||
130 | try { |
||
131 | 3 | $assetsPrefs = $xoops->cache()->read($this->assetsPrefsCacheKey); |
|
132 | 3 | $file = $xoops->path($this->assetsPrefsFilename); |
|
133 | 3 | $mtime = filemtime($file); |
|
134 | 3 | if ($assetsPrefs===false || !isset($assetsPrefs['mtime']) || !$mtime |
|
135 | 2 | || (isset($assetsPrefs['mtime']) && $assetsPrefs['mtime']<$mtime)) { |
|
136 | 1 | if ($mtime) { |
|
137 | $assetsPrefs = Yaml::read($file); |
||
138 | if (!is_array($assetsPrefs)) { |
||
139 | $xoops->logger()->error("Invalid config in system_assets_prefs.yml"); |
||
140 | $assetsPrefs = array(); |
||
141 | } else { |
||
142 | $assetsPrefs['mtime']=$mtime; |
||
143 | $xoops->cache()->write($this->assetsPrefsCacheKey, $assetsPrefs); |
||
144 | $this->copyBaseFileAssets(); |
||
145 | } |
||
146 | } else { |
||
147 | // use defaults to create file |
||
148 | $assetsPrefs = array( |
||
149 | 1 | 'default_filters' => $this->default_filters, |
|
150 | 1 | 'default_asset_refs' => $this->default_asset_refs, |
|
151 | 1 | 'default_file_assets' => $this->default_file_assets, |
|
152 | 1 | 'mtime' => time(), |
|
153 | ); |
||
154 | 1 | $this->saveAssetsPrefs($assetsPrefs); |
|
155 | 1 | $this->copyBaseFileAssets(); |
|
156 | } |
||
157 | } |
||
158 | 3 | if (!empty($assetsPrefs['default_filters']) && is_array($assetsPrefs['default_filters'])) { |
|
159 | 3 | $this->default_filters = $assetsPrefs['default_filters']; |
|
160 | } |
||
161 | 3 | if (!empty($assetsPrefs['default_asset_refs']) && is_array($assetsPrefs['default_asset_refs'])) { |
|
162 | 3 | $this->default_asset_refs = $assetsPrefs['default_asset_refs']; |
|
163 | } |
||
164 | 3 | if (!empty($assetsPrefs['default_file_assets']) && is_array($assetsPrefs['default_file_assets'])) { |
|
165 | 3 | $this->default_file_assets = $assetsPrefs['default_file_assets']; |
|
166 | } |
||
167 | } catch (\Exception $e) { |
||
168 | $xoops->events()->triggerEvent('core.exception', $e); |
||
169 | $assetsPrefs = array(); |
||
170 | } |
||
171 | 3 | return $assetsPrefs; |
|
172 | } |
||
173 | |||
174 | /** |
||
175 | * saveAssetsPrefs - record array of assets preferences in config file, and |
||
176 | * update cache |
||
177 | * |
||
178 | * @param array $assets_prefs array of asset preferences to save |
||
179 | * |
||
180 | * @return void |
||
181 | */ |
||
182 | 1 | View Code Duplication | protected function saveAssetsPrefs($assets_prefs) |
183 | { |
||
184 | 1 | if (is_array($assets_prefs)) { |
|
185 | 1 | $xoops = \Xoops::getInstance(); |
|
186 | try { |
||
187 | 1 | Yaml::save($assets_prefs, $xoops->path($this->assetsPrefsFilename)); |
|
188 | 1 | $xoops->cache()->write($this->assetsPrefsCacheKey, $assets_prefs); |
|
189 | } catch (\Exception $e) { |
||
190 | $xoops->events()->triggerEvent('core.exception', $e); |
||
191 | } |
||
192 | } |
||
193 | 1 | } |
|
194 | |||
195 | |||
196 | /** |
||
197 | * getUrlToAssets |
||
198 | * |
||
199 | * Create an asset file from a list of assets |
||
200 | * |
||
201 | * @param string $type type of asset, css or js |
||
202 | * @param array $assets list of source files to process |
||
203 | * @param string|array $filters either a comma separated list of known namsed filters |
||
204 | * or an array of named filters and/or filter object |
||
205 | * @param string $target target path, will default to assets directory |
||
206 | * |
||
207 | * @return string URL to asset file |
||
208 | */ |
||
209 | 1 | public function getUrlToAssets($type, $assets, $filters = 'default', $target = null) |
|
210 | { |
||
211 | 1 | if (is_scalar($assets)) { |
|
212 | $assets = array($assets); // just a single path name |
||
213 | } |
||
214 | |||
215 | 1 | if ($filters==='default') { |
|
216 | 1 | if (isset($this->default_filters[$type])) { |
|
217 | 1 | $filters = $this->default_filters[$type]; |
|
218 | } else { |
||
219 | $filters = ''; |
||
220 | } |
||
221 | } |
||
222 | |||
223 | 1 | View Code Duplication | if (!is_array($filters)) { |
224 | 1 | if (empty($filters)) { |
|
225 | $filters = array(); |
||
226 | } else { |
||
227 | 1 | $filters = explode(',', str_replace(' ', '', $filters)); |
|
228 | } |
||
229 | } |
||
230 | |||
231 | 1 | if (isset($this->default_output[$type])) { |
|
232 | 1 | $output = $this->default_output[$type]; |
|
233 | } else { |
||
234 | $output = ''; |
||
235 | } |
||
236 | |||
237 | 1 | $xoops = \Xoops::getInstance(); |
|
238 | |||
239 | 1 | if (isset($target)) { |
|
240 | $target_path = $target; |
||
241 | } else { |
||
242 | 1 | $target_path = $xoops->path('assets'); |
|
243 | } |
||
244 | |||
245 | try { |
||
246 | 1 | $am = $this->assetManager; |
|
247 | 1 | $fm = new FilterManager(); |
|
248 | |||
249 | 1 | foreach ($filters as $filter) { |
|
250 | 1 | if (is_object($filter) && $filter instanceof $this->filterInterface) { |
|
251 | $filterArray[] = $filter; |
||
|
|||
252 | } else { |
||
253 | 1 | switch (ltrim($filter, '?')) { |
|
254 | 1 | case 'cssembed': |
|
255 | 1 | $fm->set('cssembed', new Filter\PhpCssEmbedFilter()); |
|
256 | 1 | break; |
|
257 | 1 | case 'cssmin': |
|
258 | 1 | $fm->set('cssmin', new Filter\CssMinFilter()); |
|
259 | 1 | break; |
|
260 | 1 | case 'cssimport': |
|
261 | 1 | $fm->set('cssimport', new Filter\CssImportFilter()); |
|
262 | 1 | break; |
|
263 | 1 | case 'cssrewrite': |
|
264 | $fm->set('cssrewrite', new Filter\CssRewriteFilter()); |
||
265 | break; |
||
266 | 1 | case 'lessphp': |
|
267 | $fm->set('lessphp', new Filter\LessphpFilter()); |
||
268 | break; |
||
269 | 1 | case 'scssphp': |
|
270 | $fm->set('scssphp', new Filter\ScssphpFilter()); |
||
271 | break; |
||
272 | 1 | case 'jsmin': |
|
273 | $fm->set('jsmin', new Filter\JSMinFilter()); |
||
274 | break; |
||
275 | 1 | case 'jsqueeze': |
|
276 | 1 | $fm->set('jsqueeze', new Filter\JSqueezeFilter()); |
|
277 | 1 | break; |
|
278 | default: |
||
279 | throw new \Exception(sprintf('%s filter not implemented.', $filter)); |
||
280 | 1 | break; |
|
281 | } |
||
282 | } |
||
283 | } |
||
284 | |||
285 | // Factory setup |
||
286 | 1 | $factory = new AssetFactory($target_path); |
|
287 | 1 | $factory->setAssetManager($am); |
|
288 | 1 | $factory->setFilterManager($fm); |
|
289 | 1 | $factory->setDefaultOutput($output); |
|
290 | 1 | $factory->setDebug($this->debug); |
|
291 | 1 | $factory->addWorker(new CacheBustingWorker()); |
|
292 | |||
293 | // Prepare the assets writer |
||
294 | 1 | $writer = new AssetWriter($target_path); |
|
295 | |||
296 | // Translate asset paths, remove duplicates |
||
297 | 1 | $translated_assets = array(); |
|
298 | 1 | foreach ($assets as $k => $v) { |
|
299 | // translate path if not a reference or absolute path |
||
300 | 1 | if (0 == preg_match("/^\\/|^\\\\|^[a-zA-Z]:|^@/", $v)) { |
|
301 | 1 | $v = $xoops->path($v); |
|
302 | } |
||
303 | 1 | if (!in_array($v, $translated_assets)) { |
|
304 | 1 | $translated_assets[] = $v; |
|
305 | } |
||
306 | } |
||
307 | |||
308 | // Create the asset |
||
309 | 1 | $asset = $factory->createAsset( |
|
310 | $translated_assets, |
||
311 | $filters |
||
312 | ); |
||
313 | 1 | $asset_path = $asset->getTargetPath(); |
|
314 | 1 | if (!is_readable($target_path . $asset_path)) { |
|
315 | $assetKey = 'Asset '.$asset_path; |
||
316 | $xoops->events()->triggerEvent('debug.timer.start', $assetKey); |
||
317 | $oldumask = umask(0002); |
||
318 | $writer->writeAsset($asset); |
||
319 | umask($oldumask); |
||
320 | $xoops->events()->triggerEvent('debug.timer.stop', $assetKey); |
||
321 | } |
||
322 | |||
323 | 1 | return $xoops->url('assets/' . $asset_path); |
|
324 | |||
325 | } catch (\Exception $e) { |
||
326 | $xoops->events()->triggerEvent('core.exception', $e); |
||
327 | return null; |
||
328 | } |
||
329 | } |
||
330 | |||
331 | |||
332 | /** |
||
333 | * setDebug enable debug mode, will skip filters prefixed with '?' |
||
334 | * |
||
335 | * @return true |
||
336 | */ |
||
337 | 1 | public function setDebug() |
|
342 | |||
343 | /** |
||
344 | * Add an asset reference to the asset manager |
||
345 | * |
||
346 | * @param string $name the name of the reference to be added |
||
347 | * @param mixed $assets a string asset path, or an array of asset paths, |
||
348 | * may include wildcard |
||
349 | * @param string|array $filters either a comma separated list of known named filters |
||
350 | * or an array of named filters and/or filter object |
||
351 | * |
||
352 | * @return boolean true if asset registers, false on error |
||
353 | */ |
||
354 | 3 | public function registerAssetReference($name, $assets, $filters = null) |
|
430 | |||
431 | 1 | public function copyBaseFileAssets() |
|
437 | |||
438 | /** |
||
439 | * copyFileAssets - copy files to the appropriate asset directory. |
||
440 | * |
||
441 | * Copying is normally only needed for fonts or images when they are referenced by a |
||
442 | * relative url in stylesheet, or are located outside of the web root. |
||
443 | * |
||
444 | * @param string $fromPath path to files to copy |
||
445 | * @param string $pattern glob pattern to match files to be copied |
||
446 | * @param string $output output type (css, fonts, images, js) |
||
447 | * |
||
448 | * @return mixed boolean false if target directory is not writable, otherwise |
||
449 | * integer count of files copied |
||
450 | */ |
||
451 | 2 | public function copyFileAssets($fromPath, $pattern, $output) |
|
487 | } |
||
488 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.