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:
1 | <?php |
||
8 | class ThemeResourceLoader { |
||
9 | |||
10 | /** |
||
11 | * @var ThemeResourceLoader |
||
12 | */ |
||
13 | private static $instance; |
||
14 | |||
15 | protected $base; |
||
16 | |||
17 | /** |
||
18 | * List of template "sets" that contain a test manifest, and have an alias. |
||
19 | * E.g. '$default' |
||
20 | * |
||
21 | * @var ThemeList[] |
||
22 | */ |
||
23 | protected $sets = []; |
||
24 | |||
25 | /** |
||
26 | * @return ThemeResourceLoader |
||
27 | */ |
||
28 | public static function instance() { |
||
31 | |||
32 | /** |
||
33 | * Set instance |
||
34 | * |
||
35 | * @param ThemeResourceLoader $instance |
||
36 | */ |
||
37 | public static function set_instance(ThemeResourceLoader $instance) { |
||
40 | |||
41 | public function __construct($base = null) { |
||
44 | |||
45 | /** |
||
46 | * Add a new theme manifest for a given identifier. E.g. '$default' |
||
47 | * |
||
48 | * @param string $set |
||
49 | * @param ThemeList $manifest |
||
50 | */ |
||
51 | public function addSet($set, ThemeList $manifest) { |
||
54 | |||
55 | /** |
||
56 | * Get a named theme set |
||
57 | * |
||
58 | * @param string $set |
||
59 | * @return ThemeList |
||
60 | */ |
||
61 | public function getSet($set) { |
||
67 | |||
68 | /** |
||
69 | * Given a theme identifier, determine the path from the root directory |
||
70 | * |
||
71 | * The mapping from $identifier to path follows these rules: |
||
72 | * - A simple theme name ('mytheme') which maps to the standard themes dir (/themes/mytheme) |
||
73 | * - A theme path with a leading slash ('/mymodule/themes/mytheme') which maps directly to that path. |
||
74 | * - or a vendored theme path. (vendor/mymodule:mytheme) which maps to the nested 'theme' within |
||
75 | * that module. ('/mymodule/themes/mytheme'). |
||
76 | * - A vendored module with no nested theme (vendor/mymodule) which maps to the root directory |
||
77 | * of that module. ('/mymodule'). |
||
78 | * |
||
79 | * @param string $identifier Theme identifier. |
||
80 | * @return string Path from root, not including leading or trailing forward slash. E.g. themes/mytheme |
||
81 | */ |
||
82 | public function getPath($identifier) { |
||
117 | |||
118 | /** |
||
119 | * Attempts to find possible candidate templates from a set of template |
||
120 | * names from modules, current theme directory and finally the application |
||
121 | * folder. |
||
122 | * |
||
123 | * The template names can be passed in as plain strings, or be in the |
||
124 | * format "type/name", where type is the type of template to search for |
||
125 | * (e.g. Includes, Layout). |
||
126 | * |
||
127 | * @param string|array $template Template name, or template spec in array format with the keys |
||
128 | * 'type' (type string) and 'templates' (template hierarchy in order of precedence). |
||
129 | * If 'templates' is ommitted then any other item in the array will be treated as the template |
||
130 | * list, or list of templates each in the array spec given. |
||
131 | * Templates with an .ss extension will be treated as file paths, and will bypass |
||
132 | * theme-coupled resolution. |
||
133 | * @param array $themes List of themes to use to resolve themes. In most cases |
||
134 | * you should pass in {@see SSViewer::get_themes()} |
||
135 | * @return string Absolute path to resolved template file, or null if not resolved. |
||
136 | * File location will be in the format themes/<theme>/templates/<directories>/<type>/<basename>.ss |
||
137 | * Note that type (e.g. 'Layout') is not the root level directory under 'templates'. |
||
138 | */ |
||
139 | public function findTemplate($template, $themes) { |
||
191 | |||
192 | /** |
||
193 | * Resolve themed CSS path |
||
194 | * |
||
195 | * @param string $name Name of CSS file without extension |
||
196 | * @param array $themes List of themes |
||
197 | * @return string Path to resolved CSS file (relative to base dir) |
||
198 | */ |
||
199 | View Code Duplication | public function findThemedCSS($name, $themes) { |
|
209 | |||
210 | /** |
||
211 | * Resolve themed javascript path |
||
212 | * |
||
213 | * A javascript file in the current theme path name 'themename/javascript/$name.js' is first searched for, |
||
214 | * and it that doesn't exist and the module parameter is set then a javascript file with that name in |
||
215 | * the module is used. |
||
216 | * |
||
217 | * @param string $name The name of the file - eg '/js/File.js' would have the name 'File' |
||
218 | * @param array $themes List of themes |
||
219 | * @return string Path to resolved javascript file (relative to base dir) |
||
220 | */ |
||
221 | View Code Duplication | public function findThemedJavascript($name, $themes) { |
|
231 | |||
232 | /** |
||
233 | * Resolve a themed resource |
||
234 | * |
||
235 | * A themed resource and be any file that resides in a theme folder. |
||
236 | * |
||
237 | * @param string $resource A file path relative to the root folder of a theme |
||
238 | * @param array $themes An order listed of themes to search |
||
239 | */ |
||
240 | public function findThemedResource($resource, $themes) |
||
258 | |||
259 | /** |
||
260 | * Resolve all themes to the list of root folders relative to site root |
||
261 | * |
||
262 | * @param array $themes List of themes to resolve. Supports named theme sets. |
||
263 | * @return array List of root-relative folders in order of precendence. |
||
264 | */ |
||
265 | public function getThemePaths($themes) { |
||
279 | } |
||
280 |
This checks looks for assignemnts to variables using the
list(...)
function, where not all assigned variables are subsequently used.Consider the following code example.
Only the variables
$a
and$c
are used. There was no need to assign$b
.Instead, the list call could have been.