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 |
||
13 | class ThemeResourceLoader { |
||
14 | |||
15 | /** |
||
16 | * @var ThemeResourceLoader |
||
17 | */ |
||
18 | private static $instance; |
||
19 | |||
20 | protected $base; |
||
21 | |||
22 | /** |
||
23 | * List of template "sets" that contain a test manifest, and have an alias. |
||
24 | * E.g. '$default' |
||
25 | * |
||
26 | * @var ThemeList[] |
||
27 | */ |
||
28 | protected $sets = []; |
||
29 | |||
30 | /** |
||
31 | * @return ThemeResourceLoader |
||
32 | */ |
||
33 | public static function instance() { |
||
36 | |||
37 | /** |
||
38 | * Set instance |
||
39 | * |
||
40 | * @param ThemeResourceLoader $instance |
||
41 | */ |
||
42 | public static function set_instance(ThemeResourceLoader $instance) { |
||
45 | |||
46 | public function __construct($base = null) { |
||
49 | |||
50 | /** |
||
51 | * Add a new theme manifest for a given identifier. E.g. '$default' |
||
52 | * |
||
53 | * @param string $set |
||
54 | * @param ThemeList $manifest |
||
55 | */ |
||
56 | 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) { |
||
72 | |||
73 | /** |
||
74 | * Given a theme identifier, determine the path from the root directory |
||
75 | * |
||
76 | * The mapping from $identifier to path follows these rules: |
||
77 | * - A simple theme name ('mytheme') which maps to the standard themes dir (/themes/mytheme) |
||
78 | * - A theme path with a leading slash ('/mymodule/themes/mytheme') which maps directly to that path. |
||
79 | * - or a vendored theme path. (vendor/mymodule:mytheme) which maps to the nested 'theme' within |
||
80 | * that module. ('/mymodule/themes/mytheme'). |
||
81 | * - A vendored module with no nested theme (vendor/mymodule) which maps to the root directory |
||
82 | * of that module. ('/mymodule'). |
||
83 | * |
||
84 | * @param string $identifier Theme identifier. |
||
85 | * @return string Path from root, not including leading or trailing forward slash. E.g. themes/mytheme |
||
86 | */ |
||
87 | public function getPath($identifier) { |
||
122 | |||
123 | /** |
||
124 | * Attempts to find possible candidate templates from a set of template |
||
125 | * names from modules, current theme directory and finally the application |
||
126 | * folder. |
||
127 | * |
||
128 | * The template names can be passed in as plain strings, or be in the |
||
129 | * format "type/name", where type is the type of template to search for |
||
130 | * (e.g. Includes, Layout). |
||
131 | * |
||
132 | * @param string|array $template Template name, or template spec in array format with the keys |
||
133 | * 'type' (type string) and 'templates' (template hierarchy in order of precedence). |
||
134 | * If 'templates' is ommitted then any other item in the array will be treated as the template |
||
135 | * list. |
||
136 | * Templates with an .ss extension will be treated as file paths, and will bypass |
||
137 | * theme-coupled resolution. |
||
138 | * @param array $themes List of themes to use to resolve themes. In most cases |
||
139 | * you should pass in {@see SSViewer::get_themes()} |
||
140 | * @return string Absolute path to resolved template file, or null if not resolved. |
||
141 | * File location will be in the format themes/<theme>/templates/<directories>/<type>/<basename>.ss |
||
142 | * Note that type (e.g. 'Layout') is not the root level directory under 'templates'. |
||
143 | */ |
||
144 | public function findTemplate($template, $themes) { |
||
186 | |||
187 | /** |
||
188 | * Resolve themed CSS path |
||
189 | * |
||
190 | * @param string $name Name of CSS file without extension |
||
191 | * @param array $themes List of themes |
||
192 | * @return string Path to resolved CSS file (relative to base dir) |
||
193 | */ |
||
194 | View Code Duplication | public function findThemedCSS($name, $themes) |
|
209 | |||
210 | /** |
||
211 | * Registers the given themeable javascript as required. |
||
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) { |
|
235 | |||
236 | /** |
||
237 | * Resolve all themes to the list of root folders relative to site root |
||
238 | * |
||
239 | * @param array $themes List of themes to resolve. Supports named theme sets. |
||
240 | * @return array List of root-relative folders in order of precendence. |
||
241 | */ |
||
242 | public function getThemePaths($themes) { |
||
256 | } |
||
257 |
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.