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 ResourceLoader 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 ResourceLoader, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class ResourceLoader |
||
9 | { |
||
10 | |||
11 | /** |
||
12 | * @var ResourceLoader |
||
13 | */ |
||
14 | private static $instance; |
||
15 | |||
16 | protected $base; |
||
17 | |||
18 | /** |
||
19 | * List of template "sets" that contain a test manifest, and have an alias. |
||
20 | * E.g. '$default' |
||
21 | * |
||
22 | * @var ThemeList[] |
||
23 | */ |
||
24 | protected $sets = []; |
||
25 | |||
26 | /** |
||
27 | * @return ResourceLoader |
||
28 | */ |
||
29 | public static function inst() |
||
33 | |||
34 | /** |
||
35 | * Set instance |
||
36 | * |
||
37 | * @param ResourceLoader $instance |
||
38 | */ |
||
39 | public static function set_instance(ResourceLoader $instance) |
||
43 | |||
44 | public function __construct($base = null) |
||
48 | |||
49 | /** |
||
50 | * Add a new theme manifest for a given identifier. E.g. '$default' |
||
51 | * |
||
52 | * @param string $set |
||
53 | * @param ThemeList $manifest |
||
54 | */ |
||
55 | public function addSet($set, ThemeList $manifest) |
||
59 | |||
60 | /** |
||
61 | * Get a named theme set |
||
62 | * |
||
63 | * @param string $set |
||
64 | * @return ThemeList |
||
65 | */ |
||
66 | public function getSet($set) |
||
73 | |||
74 | /** |
||
75 | * Returns the absolute path to the given resource |
||
76 | */ |
||
77 | public function getResourcePath($theme, $resource) |
||
81 | |||
82 | /** |
||
83 | * Returns the URL of the given resource. |
||
84 | * The URL will be relative to the domain root (it will start with "/") unless it exists on a different domain. |
||
85 | */ |
||
86 | public function getResourceURL($theme, $resource) |
||
90 | |||
91 | /** |
||
92 | * Given a theme identifier, determine the path from the root directory |
||
93 | * |
||
94 | * The mapping from $identifier to path follows these rules: |
||
95 | * - A simple theme name ('mytheme') which maps to the standard themes dir (/themes/mytheme) |
||
96 | * - A theme path with a leading slash ('/mymodule/themes/mytheme') which maps directly to that path. |
||
97 | * - or a vendored theme path. (vendor/mymodule:mytheme) which maps to the nested 'theme' within |
||
98 | * that module. ('/mymodule/themes/mytheme'). |
||
99 | * - A vendored module with no nested theme (vendor/mymodule) which maps to the root directory |
||
100 | * of that module. ('/mymodule'). |
||
101 | * |
||
102 | * @param string $identifier Theme identifier. |
||
103 | * @return string Path from root, not including leading or trailing forward slash. E.g. themes/mytheme |
||
104 | */ |
||
105 | public function getPath($identifier) |
||
159 | |||
160 | /** |
||
161 | * Attempts to find possible candidate templates from a set of template |
||
162 | * names from modules, current theme directory and finally the application |
||
163 | * folder. |
||
164 | * |
||
165 | * The template names can be passed in as plain strings, or be in the |
||
166 | * format "type/name", where type is the type of template to search for |
||
167 | * (e.g. Includes, Layout). |
||
168 | * |
||
169 | * @param string|array $template Template name, or template spec in array format with the keys |
||
170 | * 'type' (type string) and 'templates' (template hierarchy in order of precedence). |
||
171 | * If 'templates' is ommitted then any other item in the array will be treated as the template |
||
172 | * list, or list of templates each in the array spec given. |
||
173 | * Templates with an .ss extension will be treated as file paths, and will bypass |
||
174 | * theme-coupled resolution. |
||
175 | * @param array $themes List of themes to use to resolve themes. In most cases |
||
176 | * you should pass in {@see SSViewer::get_themes()} |
||
177 | * @return string Absolute path to resolved template file, or null if not resolved. |
||
178 | * File location will be in the format themes/<theme>/templates/<directories>/<type>/<basename>.ss |
||
179 | * Note that type (e.g. 'Layout') is not the root level directory under 'templates'. |
||
180 | */ |
||
181 | public function findTemplate($template, $themes) |
||
234 | |||
235 | /** |
||
236 | * Resolve themed CSS path |
||
237 | * |
||
238 | * @param string $name Name of CSS file without extension |
||
239 | * @param array $themes List of themes |
||
240 | * @return string Path to resolved CSS file (relative to base dir) |
||
241 | */ |
||
242 | View Code Duplication | public function findThemedCSS($name, $themes) |
|
255 | |||
256 | /** |
||
257 | * Resolve themed javascript path |
||
258 | * |
||
259 | * A javascript file in the current theme path name 'themename/javascript/$name.js' is first searched for, |
||
260 | * and it that doesn't exist and the module parameter is set then a javascript file with that name in |
||
261 | * the module is used. |
||
262 | * |
||
263 | * @param string $name The name of the file - eg '/js/File.js' would have the name 'File' |
||
264 | * @param array $themes List of themes |
||
265 | * @return string Path to resolved javascript file (relative to base dir) |
||
266 | */ |
||
267 | View Code Duplication | public function findThemedJavascript($name, $themes) |
|
280 | |||
281 | /** |
||
282 | * Resolve a themed resource |
||
283 | * |
||
284 | * A themed resource and be any file that resides in a theme folder. |
||
285 | * |
||
286 | * @param string $resource A file path relative to the root folder of a theme |
||
287 | * @param array $themes An order listed of themes to search |
||
288 | */ |
||
289 | public function findThemedResource($resource, $themes) |
||
307 | |||
308 | /** |
||
309 | * Resolve all themes to the list of root folders relative to site root |
||
310 | * |
||
311 | * @param array $themes List of themes to resolve. Supports named theme sets. |
||
312 | * @return array List of root-relative folders in order of precendence. |
||
313 | */ |
||
314 | public function getThemePaths($themes) |
||
329 | } |
||
330 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.